Good Afternoon.
I have a list of panel that are dynamically created on Page_Load. Controls Are added via the Control.Add(p); method. code Follows
Panel p = new Panel();
p.ID = "cell_" + i + "_" + j;
p.Attributes.Add("runat", "server");
p.TabIndex = -1;
p.Attributes.Add("class", "box");
p.Attributes.Add("onClick", "clicked(this);");
p.Attributes.Add("onKeypress", "changeValue(event, this);");
p.Style.Add(HtmlTextWriterStyle.Top, top + "%");
p.Style.Add(HtmlTextWriterStyle.Left, left + "%");
p.Attributes.Add("Text", value);
PanelList.Add(p);
this block of code is in a for loop and the i, j, left, right, and value variables are altered every loop.
Now, When i call this function BEFORE Page_Load Finishes:
foreach (Control c in clientGrid.Controls)
{
Response.Write(c.ClientID.ToString());
}
It sucessfully writes all the client ID’s of all the dynamically created panels.
However, When i run the foreach function on postback, the Response.write displays nothing. Its as if asp has forgotten these panels exist!
Now i hear that on Page_Init(), the controls need to be re-rendered!
Im not sure How to do this and after many googles, cant seem to find an answer.
Ive tried using recurtion to iterate through all Page.Controls and pull out the
System.Web.UI.WebControls.Panel
But still no luck 🙁
If my question in unclear, I cant provide more code and details 🙂
Thankyou
Alex
The dynamically added controls must be re-added to the page upon each postback. I don’t know if this will meet your logical requirements but make sure you add your controls in the page_load method outside of your (!isPostback) condition. Then make sure your logic for writing out the client ids is called after the controls are added to the page.
If you need to store state information about these controls between postback, you will probably have to use some javascript and hiddenfields.
All of your event handlers should be called after Page_Load so if you add the controls in the Page_Load, you should be fine. OnPreRender is sometimes useful for this kind of thing. It gets called after Page_Load but before the post back finishes.