c# - adding listbox control programmatically and retaining Viewstate -
my page has button control.
when button clicked listbox added page's panel.
the problem is, @ first click listbox appears, @ sec or more clicks nil happens, first list box displayed.
i know issue viewstate, i've no thought prepare code?
public partial class _default : system.web.ui.page { list<listbox> mylist; protected void page_load(object sender, eventargs e) { mylist = new list<listbox>(); button1.click += add_listbox1; } public void add_listbox1(object sender, eventargs e) { listbox temp_listbox = new listbox(); mylist.add(temp_listbox); panel1.controls.add(temp_listbox); } }
you need understand asp.net page lifecyle http://msdn.microsoft.com/en-us/library/ms178472(v=vs.85).aspx. on each button click whole page recreated panel empty. button click event handler processed , listbox added in panel.
a possible solution problem defined here http://www.codeproject.com/articles/502251/how-to-create-controls-dynamically-in-asp-net-and.
c# asp.net viewstate
No comments:
Post a Comment