1

I am kind of newbie and I have an issue with ASP:Button controls.

There is about 60 buttons on the page, typical XAML looks like this:

<asp:Button class="tile" ID="Button1" runat="server" Text="Domains"/>

I need to iterate through all of the buttons on the page to change properties and I don't want to do it one by one.

I've found many suggestions here and there, but nothing works. My code behind is:

for (int i = 1; i < 59; i++)
{ 
    String butt = String.Format("Button{0}", i);
    var btn = FindControl(butt);
    btn.Visible = false;
}

Error is that there is no object reference. btn is null. I tried to inspect element in running application and it says ID of element is "MainContent_Button1" - tried that too, does not work. Other thing I tried is

foreach(var button in this.Controls.OfType<Button>())
{
    button.Visible = false;
}

I came to conclusion that asp:button is a) not a control of button type b) its ID is somehow generated when the application is run and therefore there is no control with id Button1 to be found.

Can anyone please explain that to me? I'd really like to understand why is it behaving like that and what exactly is the purpose of this behavior.

Thanks

Edit: I even tried to remove the loop completely and modify one specific button using FindControl method. Does not work either.

 var btn = FindControl("Button1");
 btn.Visible = false;

result: System.NullReferenceException: 'Object reference not set to an instance of an object.'

  • Try declaring var btn outside of the for loop, and modifying it only inside. – beefoak Oct 03 '18 at 12:50
  • Thanks for answer, beefoak. I just tried it and I even removed the loop completely leaving just this var btn = FindControl("Button1"); btn.Visible = false; and got the same error: System.NullReferenceException: 'Object reference not set to an instance of an object.' all buttons are in container and row (not sure if it can be related somehow) – FilllanTroop Oct 03 '18 at 13:02

1 Answers1

3

It looks like you are using a Master Page. Using FindControl on a Master Page works slightly different than on a normal page.You first need to find the correct ContentPlaceHolder in which the Buttons are located and use FindControl on that ContentPlaceHolder.

ContentPlaceHolder cph = Master.FindControl("MainContent") as ContentPlaceHolder;

for (int i = 1; i < 9; i++)
{
    String butt = String.Format("Button{0}", i);
    var btn = cph.FindControl(butt);
    btn.Visible = false;
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • 1
    You are genius!!! Thanks! It wooooorks... (after few hours) Can I ask you why is that? Does it mean that Page and Master Page are combined at the time of compilation and every page is actually master page, only inner content differs? I am going to print your answer and stick it on the wall here so it will not be forgotten. P.S: My reputation is low, so my clicks on upvote are not visible. – FilllanTroop Oct 03 '18 at 13:12
  • A Master page is basically the same as a UserControl. That is why it it loaded AFTER the page. Then the contents of the Master and the Page using the Master are combined for output to the client. – VDWWD Oct 03 '18 at 13:23
  • Understood. Thanks for explanation. And sorry for newbie question. I only started learning C# when my senior colleague decided to leave and ... I am just alone here now :-) Have a nice day. – FilllanTroop Oct 03 '18 at 13:26