I have a textbox with a Css class called 'required'. When a user click a button, I'd like to add additional Css Class to the textbox called 'error' without removing the 'required' class. I want to accomplish this from code-behind.
Add additional Css class programmatically
Asked Answered
This would be much easier with client side code. Is there a specific reason for wanting to do this in the codebehind? –
Involved
It's not really that difficult (from a development perspective) from code-behind. But you're right about it being easy on the client side and not requiring a round-trip to the server. –
Carinacarinate
@Ken he wants to do this onclick. –
Involved
the purpose for this is server side validation –
Neoclassic
Ah that clears things up considerable. The best way to do this is to validate the whole form on submit. As a side note you should validate on server and client side. –
Involved
You can set the CssClass property of the ASP.NET Textbox control. To add more than one CSS class for an element, just separate it with a space:
MyTextBox.CssClass = "class1 class2";
You can put this in your OnClick event handler:
<asp:TextBox ID="MyTextBox" runat="server" OnClick="MyTextBox_Click" />
Then in code-behind:
void MyTextBox_Click(Object sender, EventArgs e) {
MyTextBox.CssClass = "class1 class2";
}
hah i did not know you could do that. And what if I want to delete the second css class? MyTextBox.CssClass = "class1";?? –
Neoclassic
okay but what if I don't know what the other CssClass is. I'll be done with the coding and give the back-end to the designers.. in that case I'll hafta know what the name of the CssClass is. I dont prefer working like this since it's hard coded. Can't i do textbox.cssclas += "class2"; –
Neoclassic
Yes, just set the property to the single class. You're really just setting a string value of what will appear in the class attribute for the control. –
Carinacarinate
If you do
textbox.CssClass += "class2"
, you'll end up with class1class2
in your class attribute. I would write an AddClass method and a RemoveClass method if you want to make this cleaner. Perhaps as extension methods to the WebControl class. –
Carinacarinate I decided to create extension methods for WebControl to have a generic solution. Here's my code:
public static class WebControlExtensions
{
public static void AddCssClass(this WebControl control, string cssClass)
{
if (string.IsNullOrEmpty(control.CssClass))
{
control.CssClass = cssClass;
}
else
{
string[] cssClasses = control.CssClass.Split(' ');
bool classExists = cssClasses.Any(@class => @class == cssClass);
if (!classExists)
{
control.CssClass += " " + cssClass;
}
}
}
public static void RemoveCssClass(this WebControl control, string cssClass)
{
if (!string.IsNullOrEmpty(control.CssClass))
{
string[] cssClasses = control.CssClass.Split(' ');
control.CssClass = string.Join(" ", cssClasses.Where(@class => @class != cssClass).ToArray());
}
}
}
You can set the CssClass property of the ASP.NET Textbox control. To add more than one CSS class for an element, just separate it with a space:
MyTextBox.CssClass = "class1 class2";
You can put this in your OnClick event handler:
<asp:TextBox ID="MyTextBox" runat="server" OnClick="MyTextBox_Click" />
Then in code-behind:
void MyTextBox_Click(Object sender, EventArgs e) {
MyTextBox.CssClass = "class1 class2";
}
hah i did not know you could do that. And what if I want to delete the second css class? MyTextBox.CssClass = "class1";?? –
Neoclassic
okay but what if I don't know what the other CssClass is. I'll be done with the coding and give the back-end to the designers.. in that case I'll hafta know what the name of the CssClass is. I dont prefer working like this since it's hard coded. Can't i do textbox.cssclas += "class2"; –
Neoclassic
Yes, just set the property to the single class. You're really just setting a string value of what will appear in the class attribute for the control. –
Carinacarinate
If you do
textbox.CssClass += "class2"
, you'll end up with class1class2
in your class attribute. I would write an AddClass method and a RemoveClass method if you want to make this cleaner. Perhaps as extension methods to the WebControl class. –
Carinacarinate Here is a way to remove css class using a function. Adding a class would be very similar.
public void RemoveCssClass(string className)
{
string[] splitClasses = TextButton.CssClass.Split(' ');
string separator = "";
foreach (string _class in splitClasses)
{
if (_class != className)
{
TextButton.CssClass += separator + _class;
separator = " ";
}
}
if (TextButton.CssClass == className)
TextButton.CssClass = "";
}
That's pretty bad code. How about something like var lsc = splitClasses.ToList(); lsc.Remove(className); TextButton.CssCLass = String.Join(" ", lsc); –
Dibbrun
Here's a simple C# method to add or remove a CssClass into a WebControl...
public static void SetOrRemoveCssClass( WebControl control, string className, bool adding )
{
string[] splitClasses = control.CssClass.Split(' ');
bool hasNow = splitClasses.Contains( className );
if ( adding && !hasNow )
{
control.CssClass += " " + className;
}
else if ( !adding && hasNow ) // remove the CssClass attribute
{
control.CssClass = control.CssClass.Replace( className, "");
}
control.CssClass = control.CssClass.Replace(" "," ").Trim();
}
© 2022 - 2024 — McMap. All rights reserved.