0

As a newbie I have tried several Google searches and have found a few confusing answers. What I am trying to achieve is:

  1. click on a button (one of many),

  2. extract that button's text value, then

  3. use that value to make the relevant placeholder visible.

So far I have done the first 2 steps but how do I accomplish step 3? My code so far, which works if I click on the Asia button, is:

protected void btnArea_Click(object sender, EventArgs e)
{
    string ar = (sender as Button).Text;
    //ar = "Asia";
    phdasia.Visible = true;
}

In simple, newbie friendly terms, what do I have to insert in place of phdasia?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
BigGeoff
  • 11
  • 1

1 Answers1

1

If your placeholder controls share the same name format you may be able to reach them by name:

protected void btnArea_Click(object sender, EventArgs e)
{
    string ar = (sender as Button).Text;
    //ar = "Asia";
    string name = "phd" + ar.ToLower(); // The naming format comes here
    Control[] controls = this.Controls.Find(name, true); //find the control(s) by name

    foreach(Control control in controls) // mow loop and make them visible
        control.Visible = true;
    //phdasia.Visible = true;
}

Edit: alternatively you can use FindControl method to locate a control with an ID property of "phdasia" on the containing page:

Control control = FindControl(name);
if(control!=null)
    control.Visible = true;
roozbeh S
  • 1,084
  • 1
  • 9
  • 16