I have a Asp page containing 2 radio buttons and other textbox and labels. 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. But as soon as I had them to the ControlCollection, they disapear from my web page. I have no idea why.
C# code :
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.
Thanks
Have you tried simply setting Visible=false
for each control in the radio button selection handler?
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> 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. 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. Read about ASP.NET page lifecycle .
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.