Reputation: 2489
I have a page of dynamic controls including textboxes, radiobuttons, checkboxes (all bind to a Panel).
I run my create control function on Page_init function.
So i have something like:
protected void Page_Init(object sender, EventArgs e)
{
PopulateControls();
}
protected void PopulateControls()
{
....
for (int j = 0; j < dt.Rows.Count; j++)
{
...create dynamic controls
}
Panel1.Controls.Add(dynamic controls);
}
On postback (e.g. when i uncheck a checkbox) the screen jumps to the top.
Normally, when I am not using dynamic controls, I just put UpdatePanel/ContentTemplate around each of the control. But since I am not able to do that now, is there a way to stop the page from jumping to the top on postback?
Thanks!
Upvotes: 1
Views: 3059
Reputation: 367
Your code
Panel1.Controls.Add(dynamic controls);
shows that you are using Panel rather than UpdatePanel. Use UpdatePanel in the same way as you are using Panel, the only exception would be that you will be adding controls to the ItemTemplate rather than the Panel. In the case of Panel, it holds the controls directly since it is a container while in case of UpdatePanel, it is it's ContentTemplateContainer that contains the properties or controls. Therefore you can use something like below instead of the above line.
UpdatePanel1.ContentTemplateContainer.Controls.Add(button1);
Please also make sure to include this line of code inside the body of your for loop if you are creating more than one controls.
Hope this helps.
Upvotes: 2
Reputation: 460058
You can add controls dynamically to an UpdatePanel
.
You have to add them to the ContentTemplateContainer.Controls
.
for (int j = 0; j < dt.Rows.Count; j++)
{
...create dynamic controls
}
UpdatePanel1.ContentTemplateContainer.Controls.Add(dynamic controls);
Upvotes: 3