Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 16, 2008 12:09 PM by Deleo
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?
Move the process to a web control and use control state?
Apr 03, 2008 07:39 PM|LINK
Dynamic controls have to be added each time the page is called, postback or otherwise.
Apr 07, 2008 09:26 AM|LINK
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";
Cache["tt"] = tt;
protected void Page_Load(object sender, EventArgs e)
Panel tt = Cache["tt"] as Panel;
Let me know if I have misunderstood what you mean.
Hope it helps,
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
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.
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 :)