Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 4 buttons on my form all i need is when i clicks first button then its backcolor should be changed.
when i clicks second button then first button should get its original color and now second button should change its color.
and same for the rest of buttons.
how can i achieve this??
Any Solution???
thanks in advance.
Posted
Comments
Guirec 13-Feb-13 3:23am    
Client side? javascript ? jquery?
Server side? .net? php?
Sachin Shinde11088 13-Feb-13 4:17am    
Asp.net
vinodkumarnie 13-Feb-13 3:56am    
How you want to achieve this..? through code behind or client side..?
Sachin Shinde11088 13-Feb-13 4:18am    
can you tell me both way of achiveing this

1 solution

1. Through code behind( C# )

XML
<style type="text/css">
      .button_color
      {
          background-color:Green;
      }
  </style>

XML
<div class="">
       <asp:Button ID="Button2" runat="server" class="button_color" Text="Button"
           onclick="Button2_Click" />
       <asp:Button ID="Button3" runat="server" class="button_color" Text="Button"
           onclick="Button3_Click" />
       <asp:Button ID="Button4" runat="server" class="button_color" Text="Button"
           onclick="Button4_Click" />
       <asp:Button ID="Button5" runat="server" class="button_color" Text="Button"
           onclick="Button5_Click" />
   </div>

code behind ( c# )
C#
protected void Button2_Click(object sender, EventArgs e)
    {
        Button2.Style.Add("background-color", "red");
        Button3.Style.Add("background-color", "Green");
        Button4.Style.Add("background-color", "Green");
        Button5.Style.Add("background-color", "Green");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Button3.Style.Add("background-color", "red");
        Button2.Style.Add("background-color", "Green");
        Button4.Style.Add("background-color", "Green");
        Button5.Style.Add("background-color", "Green");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Button4.Style.Add("background-color", "red");
        Button2.Style.Add("background-color", "Green");
        Button3.Style.Add("background-color", "Green");
        Button5.Style.Add("background-color", "Green");
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Button5.Style.Add("background-color", "red");
        Button2.Style.Add("background-color", "Green");
        Button3.Style.Add("background-color", "Green");
        Button4.Style.Add("background-color", "Green");
    }


2. Clent side(using Jquery )

JavaScript
<script type="text/javascript">

   $(document).ready(function()
   {
       $(".button_color").css("background-color","green");
       $(".button_color").click(function(e)
       {

           $(".button_color").css("background-color","green");
           $(this).css("background-color","red");
           e.preventDefault();
       });
   });
   </script>

XML
<div class="">
        <asp:Button ID="Button2" runat="server" class="button_color" Text="Button"
            onclick="Button2_Click" />
        <asp:Button ID="Button3" runat="server" class="button_color" Text="Button"
            onclick="Button3_Click" />
        <asp:Button ID="Button4" runat="server" class="button_color" Text="Button"
            onclick="Button4_Click" />
        <asp:Button ID="Button5" runat="server" class="button_color" Text="Button"
            onclick="Button5_Click" />
    </div>


In the 2nd method e.preventDefault(); used to avoid postback...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900