Accessing dynamically created controls (C #)

In my code (C #), I dynamically created several RadioButtonLists with lots of RadioButtons in each of them. I put all the controls in a specific panel. I need to know how to access these controls later because they are not created in the .aspx file (with drag and drop from the toolbar)?

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" do not appear on my page. This means that something is wrong with this logic. Any suggestions what can I do?

+4
source share
3 answers

You must recreate your controls after each postback.

ASP.NET has no status, that is, when you submit a page to the server, your dynamically created controls will no longer be part of the page.

Last week I had to overcome this situation again.

What I've done? I saved the data that I used to create the controls in the Session object. In the PageLoad method, I passed the same data to recreate the dynamic controls.

I suggest: Write a way to create dynamic controls.

In PageLoad test mode to check if this is a backlink ...

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

Very important: assign unique identifiers to your dynamically created controls so that ASP.NET can recreate the controls by binding their existing event handlers, restoring them to ViewState, etc.

 myControl.ID = "myId"; 

It was hard for me to find out how it works. As soon as you find out that you have the power in your hands. Dynamically created controls open up a new world of possibilities.

As Frank said: you can use the keyword "is" in such a way as to make your life easier ...

 if(child is RadioButtonList) 


Note: It is worth mentioning the ASP.NET page lifecycle overview page on MSDN for further reference.

+2
source

When do you do this in your code? Make sure you do this at the right time in the ASP life cycle, or your controls do not exist yet: http://msdn.microsoft.com/en-us/library/ms178472.aspx

+1
source

I don’t think that creating controls in PageLoad is right now, first of all, the asp.net life cycle comes from initialization, Load ViewState Data; Load PostData; Loading objects, etc.

if you create controls on the_Load page, you will lose ViewState, events, etc.

Immediately done in PageInit, or if it is a control (OnInit).

The next tricky one is that in PageInit you don’t have a ViewState available, if you need to restore the number of objects needed to store some context / information in a hidden ant field, then get this information in PageInit, Create objects and voila!

Example:

Imagine you need to create 1.NN TextBoxes, you create a hidden html field (not with runat = server), for example. NumberOfTextBoxes.

When you execute the PageInit code: you retrieve the value, for example. numberOfTextBoxes = Request.Form ["NumberOfTextBoxes"], then you create TextBoxes.

Remember that the most important thing is to combine the number and order of existing controls stored in ViewState.

0
source

Source: https://habr.com/ru/post/1311974/


All Articles