-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebControlsExtensions.cs
More file actions
69 lines (57 loc) · 1.95 KB
/
Copy pathWebControlsExtensions.cs
File metadata and controls
69 lines (57 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public static class WebControlsExtensions
{
public static void AddCssClass(this WebControl control, string cssClass)
{
List<string> classes = control.CssClass.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (!classes.Contains(cssClass.ToLower().Trim()))
classes.Add(cssClass);
control.CssClass = classes.ToDelimitedString(" ");
}
public static void RemoveCssClass(this WebControl control, string cssClass)
{
List<string> classes = control.CssClass.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
classes.Remove(cssClass);
control.CssClass = classes.ToDelimitedString(" ");
}
public static void AddCssClass(this HtmlControl control, string cssClass)
{
List<string> classes = new List<string>();
if(control.Attributes["class"] != null)
{
classes = control.Attributes["class"].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
if (!classes.Contains(cssClass.ToLower().Trim()))
classes.Add(cssClass);
control.Attributes.Add("class", classes.ToDelimitedString(" "));
}
public static void RemoveCssClass(this HtmlControl control, string cssClass)
{
List<string> classes = new List<string>();
if (control.Attributes["class"] != null)
{
classes = control.Attributes["class"].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
classes.Remove(cssClass);
control.Attributes.Add("class", classes.ToDelimitedString(" "));
}
}
static class StringExtensions
{
public static string ToDelimitedString(this IEnumerable<string> list, string delimiter)
{
StringBuilder sb = new StringBuilder();
foreach (string item in list)
{
if (sb.Length > 0)
sb.Append(delimiter);
sb.Append(item);
}
return sb.ToString();
}
}