Asp.net General enumeration methods such as drop-down list to bind

June 4th, 2009

To do a project, which spent a lot of enumeration. Wrote in a separate drawing page list, so thought that it was very uniform. So you google the enumeration method of binding the drop-down list and found that the method is not universal. Then modified the line of code written in a generic way to enumerate the list binding. Paste the following code.

1 /// <summary>
4 /// </summary>
5 /// <param name=”col”>Control Name</param>
6 /// <param name=”eumeName”>E
num Name</param>
7 public static void BindData(Control col, string enumName)
8 {
9 System.Reflection.Assembly asm = Assembly.Load(App_Code);
10 Type type = asm.GetType(enumName);
11 FieldInfo[] fields = type.GetFields();
12 int count = fields.Length;
13
14 if (col is DropDownList)
15 {
16 DropDownList ddl = (DropDownList)col;
17 ddl.Items.Clear();
18 for (int i = 1; i < count; i++)
19 {
20 FieldInfo field = fields[i];
21 ListItem item = new ListItem(field.Name);
22 ddl.Items.Add(item);
23 }

24 }

25 else if (col is HtmlSelect)
26 {
27 HtmlSelect ddl = (HtmlSelect)col;
28 ddl.Items.Clear();
29 for (int i = 1; i < count; i++)
30 {
31 FieldInfo field = fields[i];
32 ListItem item = new ListItem(field.Name);
33 ddl.Items.Add(item);
34 }

35 }

36 else if (col is CheckBoxList)
37 {
38 CheckBoxList ddl = (CheckBoxList)col;
39 ddl.Items.Clear();
40 for (int i = 1; i < count; i++)
41 {
42 FieldInfo field = fields[i];
43 ListItem item = new ListItem(field.Name);
44 ddl.Items.Add(item);
45 }

46 }

47 else if (col is RadioButtonList)
48 {
49 RadioButtonList ddl = (RadioButtonList)col;
50 ddl.Items.Clear();
51 for (int i = 1; i < count; i++)
52 {
53 FieldInfo field = fields[i];
54 ListItem item = new ListItem(field.Name);
55 ddl.Items.Add(item);
56 }

57 }

58 else if (col is ListBox)
59 {
60 ListBox ddl = (ListBox)col;
61 ddl.Items.Clear();
62 for (int i = 1; i < count; i++)
63 {
64 FieldInfo field = fields[i];
65 ListItem item = new ListItem(field.Name);
66 ddl.Items.Add(item);
67 }

68 }

69 }

As enumerated in the project which is written above the words System.Reflection.Assembly asm = Assembly.Load ( “App_Code”); read System.Reflection.Assembly asm = Assembly.Load ( “into-way set”);
Call for PublicFun.BindData ( “Control ID”, “namespace. Enumerated”);
Posted about the way other types of data binding is as follows ….
TABLE binding table from the drop-down cases of the general method table
Code

1 /// <summary>
4 /// </summary>
5 /// <param name=”col”></param>
6 /// <param name=”dt”></param>
7 /// <param name=”Name”></param>
8 /// <param name=”value”></param>

9 public static void BindData(Control col, DataTable dt, string Name, string Value)
10 {
11 if (col is DropDownList)
12 {
13 DropDownList ddl = (DropDownList)col;
14 ddl.Items.Clear();
15 ddl.DataSource = dt;
16 ddl.DataTextField = Name;
17 ddl.DataValueField = Value;
18 ddl.DataBind();
19 }

20 else if (col is CheckBoxList)
21 {
22 CheckBoxList ddl = (CheckBoxList)col;
23 ddl.Items.Clear();
24 ddl.DataSource = dt;
25 ddl.DataTextField = Name;
26 ddl.DataValueField = Value;
27 ddl.DataBind();
28 }

29 else if (col is RadioButtonList)
30 {
31 RadioButtonList ddl = (RadioButtonList)col;
32 ddl.Items.Clear();
33 ddl.DataSource = dt;
34 ddl.DataTextField = Name;
35 ddl.DataValueField = Value;
36 ddl.DataBind();
37 }

38 else if (col is ListBox)
39 {
40 ListBox ddl = (ListBox)col;
41 ddl.Items.Clear();
42 ddl.DataSource = dt;
43 ddl.DataTextField = Name;
44 ddl.DataValueField = Value;
45 ddl.DataBind();
46 }

47 else if (col is HtmlSelect)
48 {
49 HtmlSelect ddl = (HtmlSelect)col;
50 ddl.Items.Clear();
51 ddl.DataSource = dt;
52 ddl.DataTextField = Name;
53 ddl.DataValueField = Value;
54 ddl.DataBind();
55 }

56 }

Bind Hashtable table drop-down cases of a general method


7 public static void BindData(Control col, Hashtable ht, string name, string value)
8 {
9 if (col is HtmlSelect)
10 {
11 HtmlSelect ddl = (HtmlSelect)col;
12 ddl.Items.Clear();
13 ddl.DataSource = ht;
14 ddl.DataTextField = name;
15 ddl.DataValueField = value;
16 ddl.DataBind();
17 }

18 else if (col is CheckBoxList)
19 {
20 CheckBoxList ddl = (CheckBoxList)col;
21 ddl.Items.Clear();
22 ddl.DataSource = ht;
23 ddl.DataTextField = name;
24 ddl.DataValueField = value;
25 ddl.DataBind();
26 }

27 else if (col is RadioButtonList)
28 {
29 RadioButtonList ddl = (RadioButtonList)col;
30 ddl.Items.Clear();
31 ddl.DataSource = ht;
32 ddl.DataTextField = name;
33 ddl.DataValueField = value;
34 ddl.DataBind();
35 }

36 else if (col is ListBox)
37 {
38 ListBox ddl = (ListBox)col;
39 ddl.Items.Clear();
40 ddl.DataSource = ht;
41 ddl.DataTextField = name;
42 ddl.DataValueField = value;
43 ddl.DataBind();
44 }

45 else if (col is DropDownList)
46 {
47 DropDownList ddl = (DropDownList)col;
48 ddl.Items.Clear();
49 ddl.DataSource = ht;
50 ddl.DataTextField = name;
51 ddl.DataValueField = value;
52 ddl.DataBind();
53 }

54 }

Asp.net

Asp.net article to achieve the replacement of Keywords

June 4th, 2009

Now many sites have articles with keywords replace features such as:

.Net中的Asp.net

Replaced by

.Net中的<a href=”asp.net”>Asp.net</a>

This article is teach you how to achieve this feature, as well as the realization of the functional problems encountered, as well as how to solve this.

Continue the example of the beginning of this article, you can see, if only to replace the words asp.net with. Net is very easy to achieve, are as follows

string test = test.Replace(”Asp.net”,”<a href=\”asp.net\”>Asp.net</a>”);

However, in actual use, and will not be as simple as, for example, we add another Key words. Net, now we will be using the above statement the following results:

<a href=”.net”>.Net</a>.Net中的<a href=”asp.net”>Asp<a href=”.net”>.net</a></a>

The results of the above is not what we want, but why is it so? Because Asp.net included. Net of the words, so to achieve the replacement of the above statement when the issue of duplication of replacement.

The solution is not difficult, it is actually very simple, first of all, we declare a temporary variable to save the contents of the article, as

string txt=”.Net中的Asp.net”;

string content=txt;

Then we put the keyword in accordance with the length of the short and long sort. Then loop

string txt = ".Net中的Asp.net";

List<string> keywordlist = new List<string>();

keywordlist.Add("Asp.net");

keywordlist.Add(".net");

string content = txt;

foreach (string keyword in keywordlist)
{

...

}

In the circulation in vivo, we used to find regular Keywords:

Regex r = new Regex(keyword.name, RegexOptions.IgnoreCase);
                   MatchCollection mc = r.Matches(content);
                   if (mc.Count > 0)
                   {
                       int i = 0;
                       foreach (Match m in mc)
                       {
                           if (m.Success)
                           {
...
                           }
                       }
                   }

Every time we find out after the temporary variables will be the key word in content, such as replacing the * length, such as Find Asp.net, is replaced

.Net中的*******

In this way, to wait until the next cycle. Net when the words. Temporary variables where there is no word ASP.NET, and there would be no repeat of the problem of replacement ~

Asp.net

Hello world!

June 4th, 2009

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Uncategorized