Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2011, 08:48 AM
Authorized User
 
Join Date: Jun 2009
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can not access dynamically created control.

I am creating a web site, in that I have created a textbox in a placeholder dynamically. But the problem is I can not access the text of the textbox in another method.

I have one button for create textbox and another button for reading the text of the textbox.

Event handler method of the create control button.

Code:
protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox myText = new TextBox();
        myText.Text = "Hello world";
        PlaceHolder1.Controls.Add(myText);
    }
and Event handler method of the read text button

Code:
protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write(((TextBox)PlaceHolder1.FindControl("myText")).Text);
    }
In this code when i am trying to access the textbox inside placeholder, i am getting null reference exception. That means
placeholder has no control in it.

When i debug it, i get PlaceHolder1.Controls.Count is 0.

Why the control collection is empty? Is viewstate is not working? How to solve the problem?

Please help...
__________________
Happy coding...

Arnab Roy Chowdhury.

Last edited by arnab-jit; June 8th, 2011 at 08:54 AM..
 
Old June 8th, 2011, 04:30 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Dynamically created controls are not peristed in ViewState, only their data.

To make this work you need to create the controls in PreInit, on both a normal request as well as during a post back.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with Dynamically created control Click_event Gayathri79 ASP.NET 3.5 Basics 1 June 17th, 2010 02:29 PM
Issue with Dynamically created control Click_event Gayathri79 ASP.NET 3.5 Basics 2 May 19th, 2010 12:32 AM
Accessing dynamically created content in a literal control ericburnley C# 2005 4 March 4th, 2010 05:09 PM
Saving Data to DB in dynamically created control ashokkumar ASP.NET 1.0 and 1.1 Basics 2 June 23rd, 2006 01:18 PM
Dynamically created code Puck312 Pro VB 6 3 September 7th, 2005 01:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.