Dynamically apply CSS in asp.net 2.0

I have a scrolling div with three keys and three different divs. I need to apply CSS to an active link as soon as a button is clicked. The codes I use are:

protected void btnNetwork_Click(object sender, EventArgs e)
    {
        this.btnForecast.CssClass = "li_1";
        this.btnBlog.CssClass = "li_2";
        this.btnNetwork.CssClass = "li_3_active";
        this.btnNetwork.ForeColor = System.Drawing.Color.White;
        lblMsg.Visible = false;
        BindGW("-----------------------------------");
        Forecast.Visible = false;
        Blog.Visible = false;
        Network.Visible = true;
    }

      

Thanks and regards,

Khushi

-1


source to share


3 answers


Instead of using the server side event, use the client side javascript event. Try:



$get('btnId').setAttribute("class", "some_class_name");

      

+3


source


You will not be able to dynamically change CSS properties of elements using post-back, which refreshes the page. Javascript must be used if you want changes to happen immediately.



+1


source


Simple example:

  • take one button and one label
  • create one stylesheet and add style1 class like:

body 
{   
}

.style1
{
  color: #000080;
}

      

  • write this simple code in button click event

protected void Button1_Click(object sender, EventArgs e)
{
  this.Label1.CssClass = "style1";
}

      

0


source







All Articles