简体   繁体   English

动态创建的控件和回发

[英]dynamically created controls and postback

I have created dynamic controls (Radiobuttonlists) in an asp.net page (c#). 我已经在asp.net页面(c#)中创建了动态控件(Radiobuttonlists)。 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. 我知道最好将控件放在page_init事件中。 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中,然后将其隐藏直到需要它们为止,然后在回发时保留一个布尔值并更改其可见性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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