How to give inline css to any page element in c# code behind.

In this asp.net tutorial you will learn how to give inline css to any desired page element in c# code behind. Sometimes you may got requirement to give inline css when certain condition meets in your if/else condition or switch statement. In this tutorial I will let you you how to handle this situation.

How to give inline css to any page element in c# code behind.

.apsx

<asp:Label ID="lblName" runat="server"></asp:Label>


.aspx.cs

if (lblName.Text== "Muhammad Ali")
                {
                    lblName.Style.Add("color", "green");
                }
                else
                {
                    lblName.Style.Add("color", "red");
                }

The above lines of code segment are self explanatory. I am checking that if name is Muhammad Ali then asp:Label color will be green else color will be red. Simply I use built-in Add function of Style class to give inline css to asp:Label in c# code behind.


So that’s it. Hope it will work fine for you.

0 comments: