JavaShuo
欄目
標籤
可輸入的DropDownList擴展控件(C#)
時間 2020-01-10
標籤
可輸入
dropdownlist
擴展
控件
c#
欄目
C#
简体版
原文
原文鏈接
首先,創建一個名字爲ExtendWebControls的C#用戶自定義Web控件的工程
而後,添加一個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
相關文章
1.
可輸入的 Combox(DropDownList)
2.
擴展DropDownList控件和ListBox控件(1) - 支持分組功能(optgroup標籤)
3.
(翻譯)可擴展式輸入(Expandable Input)
4.
C# windows 桌面控件的擴展
5.
C# Winform RichTextBox 控件簡單擴展
6.
C. 高可擴展架構
7.
▷擴展二進制文件的輸入輸出
8.
QML插件擴展2(基於C++的插件擴展)
9.
C# 會可能須要的擴展
10.
可擴展的 MySQL
更多相關文章...
•
C# 文件的輸入與輸出
-
C#教程
•
ASP.NET DropDownList 控件
-
ASP.NET 教程
•
C# 中 foreach 遍歷的用法
•
Flink 數據傳輸及反壓詳解
相關標籤/搜索
可擴展
dropdownlist
擴展
可輸入
輸入
可控
控件
擴展到
擴展性
擴展推薦
C#
C#教程
PHP教程
Hibernate教程
C#
插件
數據傳輸
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
windows下配置opencv
2.
HED神經網
3.
win 10+ annaconda+opencv
4.
ORB-SLAM3系列-多地圖管理
5.
opencv報錯——(mtype == CV_8U || mtype == CV_8S)
6.
OpenCV計算機視覺學習(9)——圖像直方圖 & 直方圖均衡化
7.
【超詳細】深度學習原理與算法第1篇---前饋神經網絡,感知機,BP神經網絡
8.
Python數據預處理
9.
ArcGIS網絡概述
10.
數據清洗(三)------檢查數據邏輯錯誤
本站公眾號
歡迎關注本站公眾號,獲取更多信息
相關文章
1.
可輸入的 Combox(DropDownList)
2.
擴展DropDownList控件和ListBox控件(1) - 支持分組功能(optgroup標籤)
3.
(翻譯)可擴展式輸入(Expandable Input)
4.
C# windows 桌面控件的擴展
5.
C# Winform RichTextBox 控件簡單擴展
6.
C. 高可擴展架構
7.
▷擴展二進制文件的輸入輸出
8.
QML插件擴展2(基於C++的插件擴展)
9.
C# 會可能須要的擴展
10.
可擴展的 MySQL
>>更多相關文章<<