可输入的DropDownList扩展控件(C#)

博客介绍了C#自定义Web控件的搭建过程。先建立名为ExtendWebControls的工程,接着添加类型为Web自定义控件的DropDownList.cs文件,最后添加代码到文件中,同时要在引用里添加System.Design和System.Drawing。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        首先,建立一个名字为ExtendWebControls的C#用户自定义Web控件的工程
        然后,添加一个DropDownList.cs文件,类型为Web自定义控件
        添加如下代码到文件中即可,需要注意的是在引用中要添加System.Design和System.Drawing

 1None.gifusing System;
 2None.gifusing System.Web.UI;
 3None.gifusing System.Web.UI.WebControls;
 4None.gifusing System.ComponentModel;
 5None.gifusing System.Web.UI.Design;
 6None.gifusing System.Collections;
 7None.gif
 8None.gifnamespace ExtendWebControls
 9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
11InBlock.gif    /// DropDownList 的摘要说明。
12ExpandedSubBlockEnd.gif    /// </summary>

13InBlock.gif    [ToolboxData("<{0}:DropDownListExtend runat=\"server\" />")] 
14InBlock.gif    public class DropDownListExtend : System.Web.UI.WebControls.TextBox
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        private Hashtable _values;
17InBlock.gif        public DropDownList _DropDownList;
18InBlock.gif
19InBlock.gif        public DropDownListExtend()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            _values = new Hashtable();
22InBlock.gif            _DropDownList = new DropDownList();
23ExpandedSubBlockEnd.gif        }

24InBlock.gif
25InBlock.gif        public Hashtable Values
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            get
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                return _values;
30ExpandedSubBlockEnd.gif            }

31InBlock.gif            set
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33InBlock.gif                _values = value;
34ExpandedSubBlockEnd.gif            }

35ExpandedSubBlockEnd.gif        }

36InBlock.gif
37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
38InBlock.gif        /// 将此控件呈现给指定的输出参数。
39InBlock.gif        /// </summary>
40ExpandedSubBlockEnd.gif        /// <param name="output"> 要写出到的 HTML 编写器 </param>

41InBlock.gif        protected override void Render(HtmlTextWriter output)
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43InBlock.gif            int iWidth = Convert.ToInt32(base.Width.Value);
44InBlock.gif            if(iWidth == 0)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                iWidth = 102;
47InBlock.gif                base.Width = Unit.Parse("102px");
48ExpandedSubBlockEnd.gif            }

49InBlock.gif
50InBlock.gif            int sWidth = iWidth + 16;
51InBlock.gif            int spanWidth = sWidth - 18;
52InBlock.gif
53InBlock.gif            output.Write("<div style=\"POSITION:relative\">");
54InBlock.gif            output.Write("<span style=\"MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px\">");
55InBlock.gif
56InBlock.gif            _DropDownList.Width = Unit.Parse(sWidth.ToString() + "px");
57InBlock.gif            _DropDownList.Style.Add("MARGIN-LEFT""-" + spanWidth.ToString() + "px");
58InBlock.gif            _DropDownList.Attributes.Add("onchange""this.parentNode.nextSibling.value=this.value");
59InBlock.gif
60InBlock.gif            if(_values.Count > 0)
61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
62InBlock.gif                foreach(string key in _values.Keys)
63ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
64InBlock.gif                    ListItem item = new ListItem();
65InBlock.gif                    item.Value = key;
66InBlock.gif                    item.Text = _values[key].ToString();
67InBlock.gif                    _DropDownList.Items.Add(item);
68ExpandedSubBlockEnd.gif                }

69ExpandedSubBlockEnd.gif            }

70InBlock.gif
71InBlock.gif            if(_DropDownList.Items.Count == 1)
72ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
73InBlock.gif                ListItem item = new ListItem();
74InBlock.gif                item.Value = "";
75InBlock.gif                item.Text = " ";
76InBlock.gif                _DropDownList.Items.Add(item);
77InBlock.gif                _DropDownList.SelectedIndex = 1;
78ExpandedSubBlockEnd.gif            }

79InBlock.gif
80InBlock.gif            _DropDownList.RenderControl(output);
81InBlock.gif
82InBlock.gif            output.Write("</span>");
83InBlock.gif
84InBlock.gif            base.Style.Clear();
85InBlock.gif            base.Width = Unit.Parse(iWidth.ToString() + "px");
86InBlock.gif            base.Style.Add("left""0px");
87InBlock.gif            base.Style.Add("POSITION""absolute");
88InBlock.gif
89InBlock.gif            base.Render(output);
90InBlock.gif
91InBlock.gif            output.Write("</div>");
92ExpandedSubBlockEnd.gif        }

93ExpandedSubBlockEnd.gif    }

94ExpandedBlockEnd.gif}

95None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值