Last post Feb 20, 2017 11:58 AM by Khuram.Shahzad
Feb 20, 2017 11:10 AM|JAYHAWKER|LINK
I have a control:
<asp:Label id="lbl1DailyTotal" runat="server" CssClass="lblSpecial present daily"></asp:Label>
I want to switch out the CssClass "daily" with "monthly".
I have tried: lbl1DailyTotal.CssClass.Replace("daily", "monthly");
but it doesn't work. What am I doing wrong?
Feb 20, 2017 11:39 AM|ehsansajjad465|LINK
String class is immutable which means modifying a string returns a new String, so
Replace is returning a new string not updating the value of CssClass property of control, you would need to do it like:
lbl1DailyTotal.CssClass = lbl1DailyTotal.CssClass.Replace("daily","monthly");
Please have a look at MSDN documentation of Replace() :
Feb 20, 2017 11:43 AM|kaushikmaheta|LINK
lbl1DailyTotal.Attributes["class"] = "monthly";
Feb 20, 2017 11:47 AM|Khuram.Shahzad|LINK
From client side:
Feb 20, 2017 11:50 AM|JAYHAWKER|LINK
Thanks, the .CssClass worked. I think the ["class"] would only work with html control because I tried it before and it wouldn't take on an aspnet control.
When I have three different classes referenced, i.e. CssClass="daily lblSpecial red", how do I replace only one of them with another class. The .CssClass replaces all the classes and not just one I want to switch out.
Feb 20, 2017 11:58 AM|Khuram.Shahzad|LINK
Get string to css classes array,in this case you get 3 items of array, update class which you want,when done again convert array of words to string (concatenate like S=A+B), assign css class string to label control through replace function.