开发者

dynamically created controls and postback

开发者 https://www.devze.com 2022-12-31 14:26 出处:网络
I have created dynamic controls (Radiobuttonlists) in an asp.net page (c#). I create them after a button click like this.

I have created dynamic controls (Radiobuttonlists) in an asp.net page (c#). I create them after a button click like this.

RadioButtonList rbl = new RadioButtonList();
c2.Controls.Add(rbl);
//Set properties of rbl 
rbl.RepeatLayout = RepeatLayout.Flow;
rbl.ID = string.Format("rbl{0}", item.QuestionID);
rbl.RepeatDirection = RepeatDirection.Horizontal;
rbl.Items.Add(new ListI开发者_开发问答tem("True", "1"));
rbl.Items.Add(new ListItem("False", "0"));
rbl.Items.Add(new ListItem("?", "-1"));

Now the problem arises when I click the submit button, the controls are lost. I know it's better to actually put the controls in page_init event. but is there no workaround so I can still initiate my controls after button click?

And is it good to first create button, then add it to control collection and then set its properties?

Thankd in advance Kind regards, Mark


You could always put the controls into an asp:Panel on the page and then hide them until you need them, and then on postback persist a boolean and change it's visibility

0

精彩评论

暂无评论...
验证码 换一张
取 消