简体   繁体   中英

dynamically created controls and postback

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 ListItem("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

您总是可以将控件放到页面上的asp:Panel中,然后将其隐藏直到需要它们为止,然后在回发时保留一个布尔值并更改其可见性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM