[英]No control created on page_load
我有一個Asp頁面,其中包含2個單選按鈕以及其他文本框和標簽。 問題是,當選中單選按鈕時,我必須使其中一些消失(不可見)。
我考慮過使用ControlCollection並添加需要使其不可見的控件。 但是,一旦我將它們帶入ControlCollection,它們就會從我的網頁上消失。 我不知道為什么。
C#代碼:
private void createGroup()
{
ControlCollection cc = CreateControlCollection();
cc.Add(txt1);
cc.Add(txt2);
// and so on...
}
如果我在Page_Load()事件上調用此函數,則該頁面上沒有控件。
謝謝
您是否嘗試過為單選按鈕選擇處理程序中的每個控件簡單地設置Visible=false
?
void YourRadioButton_CheckChanged(Object sender, EventArgs e)
{
txt1.Visible = !YourRadioButton.Checked;
txt2.Visible = !YourRadioButton.Checked;
// and so on...
}
如果要在頁面加載中創建控件集合以簡化操作,只需創建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...
}
Page對象已經具有稱為控件的控件集合。 您可以執行以下操作:
void YourRadioButton_CheckChanged(Object sender, EventArgs e)
{
foreach(Control control in this.Controls)
{
if(control is Textbox)
{
// do something
}
}
}
動態控件應在PreInit
事件中創建。 閱讀有關ASP.NET頁面生命周期的信息 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.