ALOToverflow
ALOToverflow

Reputation: 2699

No control created on page_load

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

Upvotes: 0

Views: 178

Answers (3)

jball
jball

Reputation: 25014

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... 
}

Upvotes: 2

KevnRoberts
KevnRoberts

Reputation: 446

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
         }
     }
  }

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Dynamic controls should be created in the PreInit event. Read about ASP.NET page lifecycle.

Upvotes: 0

Related Questions