可输入的DropDownList扩展控件(C#)
首先,建立一个名字为ExtendWebControls的C#用户自定义Web控件的工程
然后,添加一个DropDownList.cs文件,类型为Web自定义控件
添加如下代码到文件中即可,需要注意的是在引用中要添加System.Design和System.Drawing
然后,添加一个DropDownList.cs文件,类型为Web自定义控件
添加如下代码到文件中即可,需要注意的是在引用中要添加System.Design和System.Drawing
1
using System;
2
using System.Web.UI;
3
using System.Web.UI.WebControls;
4
using System.ComponentModel;
5
using System.Web.UI.Design;
6
using System.Collections;
7
8
namespace ExtendWebControls
9
{
10
/// <summary>
11
/// DropDownList 的摘要说明。
12
/// </summary>
13
[ToolboxData("<{0}:DropDownListExtend runat=\"server\" />")]
14
public class DropDownListExtend : System.Web.UI.WebControls.TextBox
15
{
16
private Hashtable _values;
17
public DropDownList _DropDownList;
18
19
public DropDownListExtend()
20
{
21
_values = new Hashtable();
22
_DropDownList = new DropDownList();
23
}
24
25
public Hashtable Values
26
{
27
get
28
{
29
return _values;
30
}
31
set
32
{
33
_values = value;
34
}
35
}
36
37
/// <summary>
38
/// 将此控件呈现给指定的输出参数。
39
/// </summary>
40
/// <param name="output"> 要写出到的 HTML 编写器 </param>
41
protected override void Render(HtmlTextWriter output)
42
{
43
int iWidth = Convert.ToInt32(base.Width.Value);
44
if(iWidth == 0)
45
{
46
iWidth = 102;
47
base.Width = Unit.Parse("102px");
48
}
49
50
int sWidth = iWidth + 16;
51
int spanWidth = sWidth - 18;
52
53
output.Write("<div style=\"POSITION:relative\">");
54
output.Write("<span style=\"MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px\">");
55
56
_DropDownList.Width = Unit.Parse(sWidth.ToString() + "px");
57
_DropDownList.Style.Add("MARGIN-LEFT", "-" + spanWidth.ToString() + "px");
58
_DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.value");
59
60
if(_values.Count > 0)
61
{
62
foreach(string key in _values.Keys)
63
{
64
ListItem item = new ListItem();
65
item.Value = key;
66
item.Text = _values[key].ToString();
67
_DropDownList.Items.Add(item);
68
}
69
}
70
71
if(_DropDownList.Items.Count == 1)
72
{
73
ListItem item = new ListItem();
74
item.Value = "";
75
item.Text = " ";
76
_DropDownList.Items.Add(item);
77
_DropDownList.SelectedIndex = 1;
78
}
79
80
_DropDownList.RenderControl(output);
81
82
output.Write("</span>");
83
84
base.Style.Clear();
85
base.Width = Unit.Parse(iWidth.ToString() + "px");
86
base.Style.Add("left", "0px");
87
base.Style.Add("POSITION", "absolute");
88
89
base.Render(output);
90
91
output.Write("</div>");
92
}
93
}
94
}
95
using System;2
using System.Web.UI;3
using System.Web.UI.WebControls;4
using System.ComponentModel;5
using System.Web.UI.Design;6
using System.Collections;7

8
namespace ExtendWebControls9
{10
/// <summary>11
/// DropDownList 的摘要说明。12
/// </summary>13
[ToolboxData("<{0}:DropDownListExtend runat=\"server\" />")] 14
public class DropDownListExtend : System.Web.UI.WebControls.TextBox15
{16
private Hashtable _values;17
public DropDownList _DropDownList;18

19
public DropDownListExtend()20
{21
_values = new Hashtable();22
_DropDownList = new DropDownList();23
}24

25
public Hashtable Values26
{27
get28
{29
return _values;30
}31
set32
{33
_values = value;34
}35
}36

37
/// <summary> 38
/// 将此控件呈现给指定的输出参数。39
/// </summary>40
/// <param name="output"> 要写出到的 HTML 编写器 </param>41
protected override void Render(HtmlTextWriter output)42
{43
int iWidth = Convert.ToInt32(base.Width.Value);44
if(iWidth == 0)45
{46
iWidth = 102;47
base.Width = Unit.Parse("102px");48
}49

50
int sWidth = iWidth + 16;51
int spanWidth = sWidth - 18;52

53
output.Write("<div style=\"POSITION:relative\">");54
output.Write("<span style=\"MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px\">");55

56
_DropDownList.Width = Unit.Parse(sWidth.ToString() + "px");57
_DropDownList.Style.Add("MARGIN-LEFT", "-" + spanWidth.ToString() + "px");58
_DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.value");59

60
if(_values.Count > 0)61
{62
foreach(string key in _values.Keys)63
{64
ListItem item = new ListItem();65
item.Value = key;66
item.Text = _values[key].ToString();67
_DropDownList.Items.Add(item);68
}69
}70

71
if(_DropDownList.Items.Count == 1)72
{73
ListItem item = new ListItem();74
item.Value = "";75
item.Text = " ";76
_DropDownList.Items.Add(item);77
_DropDownList.SelectedIndex = 1;78
}79

80
_DropDownList.RenderControl(output);81

82
output.Write("</span>");83

84
base.Style.Clear();85
base.Width = Unit.Parse(iWidth.ToString() + "px");86
base.Style.Add("left", "0px");87
base.Style.Add("POSITION", "absolute");88

89
base.Render(output);90

91
output.Write("</div>");92
}93
}94
}95




浙公网安备 33010602011771号