c# - Add multiple controls to panel in webforms -
i able add multiple label controls panel , display them on onlick event. code have want first time, label simple replaced new label on second onlick event , on.
here have far:
private void createtasklabel(guid goalid, string goal) { label tasklabel = new label(); tasklabel.id = goalid.tostring(); tasklabel.text = goal.tostring(); taskpanel.controls.add(tasklabel); }
so, instance, creates new label uniqueid (handled elsewhere) , places within panel , displays it. when onlick fires again, same label replaced instead of new 1 appearing below it.
dynamically created controls not persisted after postback. need keep track of how many controls have generated , regenerate of them each time work how want. basic implementation:
list<string> labeidlist = new list<string>(); override saveviewstate(..) { if (labelidlist.count>0) { viewstate["labeldilist"]=labelidlist; } base.saveviewstate(..) } override loadviewstate() { base.loadviewstate(); if (viewstate["labelidlist"]!=null) { labelidlist = (list<string>)viewstate["labelidlist"]; } } override void onload(..) { foreach (string id in labelidlist) { // make function code in createtasklabel, // can use there recreatecontrol(id); } } private void createtasklabel(guid goalid, string goal) { ... // save id created labelidlist.add(tasklabel.id); }
i realized these labels you're creating - should keeping track of text
instead of id. if actual id important reason keep track of them both, use list<tuple<string,string>>
that. more typical situation creating input controls, though, in case asp.net persist data user entered long re-create them on or before onload (assuming viewstate enabled).
Comments
Post a Comment