[英]No control created on page_load
I have a Asp page containing 2 radio buttons and other textbox and labels. 我有一个Asp页面,其中包含2个单选按钮以及其他文本框和标签。 The things is that I have to make some of them disapear (not visible) when a radio button is selected.
问题是,当选中单选按钮时,我必须使其中一些消失(不可见)。
I thought about using a ControlCollection and adding the control I need to make invisible to it. 我考虑过使用ControlCollection并添加需要使其不可见的控件。 But as soon as I had them to the ControlCollection, they disapear from my web page.
但是,一旦我将它们带入ControlCollection,它们就会从我的网页上消失。 I have no idea why.
我不知道为什么。
C# code : C#代码:
private void createGroup()
{
ControlCollection cc = CreateControlCollection();
cc.Add(txt1);
cc.Add(txt2);
// and so on...
}
If I call this function on the Page_Load() event, no control are on the page. 如果我在Page_Load()事件上调用此函数,则该页面上没有控件。
Thanks 谢谢
Have you tried simply setting Visible=false
for each control in the radio button selection handler? 您是否尝试过为单选按钮选择处理程序中的每个控件简单地设置
Visible=false
?
void YourRadioButton_CheckChanged(Object sender, EventArgs e)
{
txt1.Visible = !YourRadioButton.Checked;
txt2.Visible = !YourRadioButton.Checked;
// and so on...
}
If you want to create collections of controls in your page load to ease manipulation, just create a List<WebControl>
. 如果要在页面加载中创建控件集合以简化操作,只需创建
List<WebControl>
。
List<WebControl> yourControls = new List<WebControl>();
//...
protected void Page_Load(object sender, EventArgs e)
{
yourControls.Add(txt1);
yourControls.Add(txt2);
// and so on...
}
The Page object already has a collection of controls called Controls. Page对象已经具有称为控件的控件集合。 You could do something like this:
您可以执行以下操作:
void YourRadioButton_CheckChanged(Object sender, EventArgs e)
{
foreach(Control control in this.Controls)
{
if(control is Textbox)
{
// do something
}
}
}
Dynamic controls should be created in the PreInit
event. 动态控件应在
PreInit
事件中创建。 Read about ASP.NET page lifecycle . 阅读有关ASP.NET页面生命周期的信息 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.