views:

414

answers:

3

In my code behind (c#) I dynamically created some RadioButtonLists with more RadioButtons in each of them. I put all controls to a specific Panel. What I need to know is how to access those controls later as they are not created in .aspx file (with drag and drop from toolbox)?

I tried this:

    foreach (Control child in panel.Controls)
    {
        Response.Write("test1");
        if (child.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButtonList"))
        {
            RadioButtonList r = (RadioButtonList)child;
            Response.Write("test2");
        }   
    }

"test1" and "test2" dont show up in my page. That means something is wrong with this logic. Any suggestions what could I do?

+1  A: 

When are you doing this in your code? Be sure you do this at the right time in the ASP life cycle or your controls don't exist yet: http://msdn.microsoft.com/en-us/library/ms178472.aspx

Mee
+1  A: 

You must recreate your controls after each postback.

ASP.NET is stateless, that is, when you postback a page to the server, your dynamically created controls won't be part of the page anymore.

Last week I had to overcome this situation once more.

What did I do? I saved the data that I used to create the controls inside Session object. On PageLoad method I passed that same data to recreate the dynamic controls.

What I suggest is: Write a method to create the dynamic controls.

On PageLoad method check to see if it's a postback...

if(Page.IsPostBack)
{
   // Recreate your controls here.
}

A really important thing: assign unique IDs to your dynamically created controls so that ASP.NET can recreate the controls binding their existing event handlers, restoring their ViewState, etc.

myControl.ID = "myId";

I had a hard time to learn how this thing works. Once you learn you have power in your hands. Dynamically created controls open up a new world of possibilities.

As Frank mentioned: you can use the "is" keyword this way to facilitate your life...

if(child is RadioButtonList)


Note: it's worth to mention the ASP.NET Page Life Cycle Overview page on MSDN for further reference.

Leniel Macaferi
Ok i will follow your advice.
Aliens
Tell me something.If I save a textbox to session (do I need to save every single one to different session?), how can I get a text from that Textbox on a next page (because I save textbox object to Session without text written by user)? I put textbox as example, but it is similar with RadioButtons.
Aliens
@Aliens: the Session object is unique in ASP.NET for each user so there's no different session. You use Session.Add("objectName", objectValue). If the controls are recreated after each postback they'll hold their values (because of ViewState) given you follow my advices.
Leniel Macaferi
I recreated some textboxes and I only get their IDs (textBox.ID) but I cannot get the text (textBox.Text is empty string).But if I write a statement textBox.Text ="something" in a method that creates dynamic objects I get that text later on.What am I missing (sorry but I am new to asp.net)?
Aliens
@Aliens: I'm a bit confused about what you're trying to do now. textBox.Text is empty because probably you didn't enter/input any data to it. If you write something in textBox.Text you get the same value when textBox.Text is recreated. It appears to be the correct behavior.
Leniel Macaferi
Look there are 2 situations. Let's assume I create 3 textboxes.1st situation: I assign property textbox.Text in code like sotextbox1.Text = "first text";textbox2.Text = "second text";textbox3.Text = "third text";2nd situation: I do not assign property textbox.Text in code because I want to get text from user. When the user clicks submit his texts are not saved in textbox.Text properties (they are empty). I suppose I should do something like when user is typing a text, the text must be saving in that property. But I dont know how to do that.
Aliens
@Aliens: sorry for not getting back early. What you describe should be handled by ASP.NET ViewState. Each TextBox has a property called EnableViewState. When creating your dynamic controls assign this property to true. When the control is recreated after each postback it should redisplay the value entered by the user. What makes it possible is the TextBox ID property. ASP.NET knows that a TextBox with that ID was part of the page before it was posted back. So now it knows that it should get the value back for that same TextBox that is being recreated (matching the ID).
Leniel Macaferi
Hello, thanks for your reply. Anyway, I solved my problem using cookies. I saved values from textboxes and radiobuttonlists to a cookie using javascript. After postback I retrieve those values back. I am not sure if this is the best solution but at least it is working.
Aliens
Great to know you solved the problem. The way you did it isn't the most important thing. The thing is that you have it working! :)
Leniel Macaferi
A: 

I don't think creating controls in the PageLoad is the right away of doing, first the asp.net life cycle goes from Initialization;Load ViewState Data;Load PostData; Object Load etc.

if you create controls at the Page_Load you'll lose the ViewState, events etc.

The right away is doing at PageInit, or if is a control (OnInit).

The next difficult is that at PageInit, you don't have the ViewState Available, if you need to reconstruct the number of objects you need to store some context/info in a hidden field ant then retrieve that information at PageInit, Create the objects and voila!

Example:

imagine that you need to create 1..N TextBoxes, you create html hidden field (not with runat=server) e.g. NumberOfTextBoxes.

When you are executing PageInit Code: you retrieve the value e.g. numberOfTextBoxes = Request.Form["NumberOfTextBoxes"], then you create the TextBoxes.

Remember the most important thing is to match the number and the order of existent Controls stored the ViewState.

Pedro Tiago Pereira
I dont have a problem with number of textboxes and their IDs.But I cant get texts from the textboxes (please look my comments in Leniel Macaferi answer). If you have any solution I will be thankful.
Aliens