Sagotharan
Sagotharan

Reputation: 2626

How to set a button property for multiple buttons using foreach?

I have 12 buttons in ASP.NET page. I want to color the button; But My code is not work. I will check with break point but button.BackColor = Color.DarkGreen; code is not executed.

ASP Design Coding -

  <form id="form1" runat="server" style="background-color:Silver; height:100%; min-height:300px">   
       <div style="height:100%;">
            <div style="height:20%;">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>    
        </div>
        <div id="content" runat="server" class="content" style="height:70%; min-height:180px">    
            <div >               
                <asp:Button ID="Button1" runat="server" Text="Button"  style="width:32%" 
                    Enabled="False" />
                <asp:Button ID="Button2" runat="server" Text="Button" style="width:32%" 
                    Enabled="False"/>               
                <asp:Button ID="btnUp" runat="server" Text="UP" onclick="btnUp_Click" style="width:32%" /> <br /><br />
                <asp:Button ID="Button3" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" />
                <asp:Button ID="Button4" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" />
                <asp:Button ID="Button5" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" /> <br /><br />
                <asp:Button ID="Button6" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" />
                <asp:Button ID="Button7" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" />
                <asp:Button ID="Button8" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" /> <br /><br />
                <asp:Button ID="Button9" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" />
                <asp:Button ID="Button10" runat="server" Text="Button" style="width:32%" 
                    Enabled="False" />            
                <asp:Button ID="btnDown" runat="server" Text="DOWN" onclick="btnDown_Click" style="width:32%" /> <br /><br />
            </div><!-- /grid-b -->        
        </div>       
          <div id="footer" class="footer" style="height:10%;">
             <a href="OutLet.aspx" >Home</a>
             <a href="Selection.aspx">Selection</a>
        </div>
        </div>
    </form>

*C# Coding - *

var buttons = this.form1.Controls.OfType<Button>();
        foreach (var button in buttons)
        {
           button.BackColor = Color.DarkGreen;
        }

Upvotes: 0

Views: 1580

Answers (4)

Zo Has
Zo Has

Reputation: 13018

Apart from any need to specifically use codebehind for setting background color of your buttons, I recommend you to use javascript to achieve this as it is really fast. Eample here

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460068

Replace one of your divs with an ASP.NET Panel(which is rendered as div but can be referenced from codebehind):

<asp:Panel id="ButtonPanel" runat="server">               
    <asp:Button ID="Button1" runat="server" Text="Button"  style="width:32%" 
        Enabled="False" />
    <asp:Button ID="Button2" runat="server" Text="Button" style="width:32%" 
        Enabled="False"/>               
    <asp:Button ID="btnUp" runat="server" Text="UP" onclick="btnUp_Click" style="width:32%" /> <br /><br />
    <asp:Button ID="Button3" runat="server" Text="Button" style="width:32%" 
        Enabled="False" />
    <asp:Button ID="Button4" runat="server" Text="Button" style="width:32%" 
        Enabled="False" />
    <asp:Button ID="Button5" runat="server" Text="Button" style="width:32%" 
        Enabled="False" /> <br /><br />
    <asp:Button ID="Button6" runat="server" Text="Button" style="width:32%" 
        Enabled="False" />
    <asp:Button ID="Button7" runat="server" Text="Button" style="width:32%" 
        Enabled="False" />
    <asp:Button ID="Button8" runat="server" Text="Button" style="width:32%" 
        Enabled="False" /> <br /><br />
    <asp:Button ID="Button9" runat="server" Text="Button" style="width:32%" 
        Enabled="False" />
    <asp:Button ID="Button10" runat="server" Text="Button" style="width:32%" 
        Enabled="False" />            
    <asp:Button ID="btnDown" runat="server" Text="DOWN" onclick="btnDown_Click" style="width:32%" /> <br /><br />
</asp:Panel>

Then you can loop all controls in this panel, take every button and do what you need to do:

foreach(Control c in ButtonPanel.Controls){
    if(c.GetType()==typeof(Button)){
        Button btn = (Button)c;
        btn.BackColor = Color.DarkGreen;
    }
}

Edit: as mentioned by @Abbas, if using at least .NET framework 3.5 you can use the Enumerable.OfType method:

foreach(Button btn in ButtonPanel.Controls.OfType<Button>()) {
        btn.BackColor = System.Drawing.Color.DarkGreen;
}

Upvotes: 3

Carl Sharman
Carl Sharman

Reputation: 4845

To answer your question, the problem here is that the Controls property will only give you direct child controls of form1. To get your buttons, use:

var buttons = this.content.Controls.OfType<Button>();

However I would also agree with Jayanga, CSS would do the job better.

Upvotes: 1

Tim M.
Tim M.

Reputation: 54358

The controls collection only contains immediate children. My guess is that var buttons has zero items in it.

foreach( Control c in this.[immediate parent ID].Controls )
{
    Button b = c as Button;
    if( b != null )
    {
        b.BackColor = Color.DarkGreen;
    }
}

As @Jayanga's comment points out, you can (and should) set the styles of the buttons using CSS in the page.

Upvotes: 0

Related Questions