asp.net - Adding multiple custom controls -
i have custom control (customcontainer) can hold multiple custom controls of type conditioncontrol. when click button in customcontainer control want add customcontrol container.
protected void imagebutton1_click(object sender, imageclickeventargs e) { customcontrolspanel.controls.add(new literalcontrol("<br />")); conditioncontrol mycc = (conditioncontrol)loadcontrol(@"~/conditioncontrol.ascx"); customcontrolspanel.controls.add(mycc); }
this works once. click once, adds condition control not work anymore. how can fix ?
edit: tried saving control collection of panel session variable , using in order restore controls so:
protected void page_load(object sender, eventargs e) { if (!page.ispostback) { session["controls"] = conditioncontrolspanel.controls; } else { controlcollection temp = (controlcollection)session["controls"]; conditioncontrolspanel.controls.clear(); foreach (control ctrl in temp) { conditioncontrolspanel.controls.add(ctrl); } } }
i error when try add new control saying collection modified; enumeration operation may not execute.
when try foreach
that because everytime click on button, postbacks, removes first 1 , adds new one. overcome this, can either prevent postback(by updatepanel) or keep count of added conditioncontrols (like in session) , add controls many counter keep in session says
Comments
Post a Comment