I am using .net v2.x to build a process where I need to dynamically add asp pic. button controls to a panel. The issue is when I post to the form, the pic. button controls disappear unless I re-add to the panel. Is there a way to retain all of the dynamically
added asp controls between updates?
Save the contents of the panel in one of the STATE vars? (I tried this, but I could not get it to work).
Move the process to a web control and use control state?
You could use Viewstate as well, IF the dropdown doesnt get populated too much.
The good thing about viewstate is that it doesnt touch the server memory, and it is page specific only, whilst cache is application oriented and will use server memory. Then again Viewstate will take up bandwidth :p
--- Imagination was given to man to compensate him for what he is not; a sense of humor to console him for what he is ---
I tried this approach with both session and viewstate, but I had issues with having to re-add all of the events for each of the controls in the panel. These are not kept in the state and have to be re-added each time you load the control from the state.
My main problem is the large # of controls in the pannel, eg. 200+ image controls. It is more efficient to use JS than to use server memory.
Thanks for the help.
P.S. You can not use Viewstate to store a panel as you can not serialize the panel.
If i am not mistaken, the viwestate is loaded just after the on_init and by the time on_load is hit, all viewstate is loaded into the memory. Problem by dynamicly adding controls in page_load is that after a postback, the viewstate is different.
First hit on the page, you add controls A, B and C in the on_load. After the page has rendered, the new controls are added to the viewstate by method SaveViewstate(). Now you perform a postback to the same page, and when you are in the On_Load method, your
controls A,B, and C are already in the viewstate, BUT not added to the page yet. There might be a viewstate error saying that the viewstate is different or something like that.
If you add the controls dynamicly in your On_Init instead, you are 100% sure that all controls are added BEFORE viewstate is loaded, and the controls will "remember" their state when you hit On_Load.
Although said and done, i have not testet this myself, but its a theory :)
--- Imagination was given to man to compensate him for what he is not; a sense of humor to console him for what he is ---
dm-fw
Member
8 Points
19 Posts
How to retain all of the dynamically added asp controls between updates?
Apr 03, 2008 06:53 PM|LINK
I am using .net v2.x to build a process where I need to dynamically add asp pic. button controls to a panel. The issue is when I post to the form, the pic. button controls disappear unless I re-add to the panel. Is there a way to retain all of the dynamically added asp controls between updates?
Save the contents of the panel in one of the STATE vars? (I tried this, but I could not get it to work).Move the process to a web control and use control state?
Thanks
ely79
Participant
1018 Points
188 Posts
Re: How to retain all of the dynamically added asp controls between updates?
Apr 03, 2008 07:39 PM|LINK
Dynamic controls have to be added each time the page is called, postback or otherwise.
Hua-Jun Li -...
All-Star
75950 Points
5608 Posts
Re: How to retain all of the dynamically added asp controls between updates?
Apr 07, 2008 09:26 AM|LINK
Hi dm-fw,
the asp.net do't keep control state.
So if you want to create control dynamicly, you should create the dynamic control every time in Page_Load function.
You can store the dynamic control into cache, and then read it at page_load function.
Here is a example, maybe help you.
<asp:placeholder runat="server" ID="PlaceHolder2"></asp:placeholder>
protected void Button4_Click(object sender, EventArgs e)
{
Panel tt = new Panel();
TextBox tb = new TextBox();
tb.ID = "txtName";
tb.Text = "hello";
tt.Controls.Add(tb);
PlaceHolder2.Controls.Add(tt);
Cache["tt"] = tt;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Cache["tt"]!=null)
{
Panel tt = Cache["tt"] as Panel;
PlaceHolder2.Controls.Add(tt);
}
}
Let me know if I have misunderstood what you mean.
Thanks.
Hope it helps,
Hua Jun
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Deleo
Participant
1887 Points
386 Posts
Re: How to retain all of the dynamically added asp controls between updates?
Apr 07, 2008 10:48 AM|LINK
You could use Viewstate as well, IF the dropdown doesnt get populated too much.
The good thing about viewstate is that it doesnt touch the server memory, and it is page specific only, whilst cache is application oriented and will use server memory. Then again Viewstate will take up bandwidth :p
dm-fw
Member
8 Points
19 Posts
Re: How to retain all of the dynamically added asp controls between updates?
Apr 07, 2008 09:48 PM|LINK
I tried this approach with both session and viewstate, but I had issues with having to re-add all of the events for each of the controls in the panel. These are not kept in the state and have to be re-added each time you load the control from the state.
My main problem is the large # of controls in the pannel, eg. 200+ image controls. It is more efficient to use JS than to use server memory.
Thanks for the help.
P.S. You can not use Viewstate to store a panel as you can not serialize the panel.
Deleo
Participant
1887 Points
386 Posts
Re: How to retain all of the dynamically added asp controls between updates?
Apr 16, 2008 12:09 PM|LINK
If i am not mistaken, the viwestate is loaded just after the on_init and by the time on_load is hit, all viewstate is loaded into the memory. Problem by dynamicly adding controls in page_load is that after a postback, the viewstate is different.
First hit on the page, you add controls A, B and C in the on_load. After the page has rendered, the new controls are added to the viewstate by method SaveViewstate(). Now you perform a postback to the same page, and when you are in the On_Load method, your controls A,B, and C are already in the viewstate, BUT not added to the page yet. There might be a viewstate error saying that the viewstate is different or something like that.
If you add the controls dynamicly in your On_Init instead, you are 100% sure that all controls are added BEFORE viewstate is loaded, and the controls will "remember" their state when you hit On_Load.
Although said and done, i have not testet this myself, but its a theory :)