asp.net DropDownList 可输可选 下拉菜单

自定义Controls类

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Controls
{
    [ToolboxData("<{0}:DropDownListExtend runat=\"server\" />")]
    public class DropDownListExtend : System.Web.UI.WebControls.TextBox
    {
        private Hashtable _values;
        private DropDownList _DropDownList;
        public DropDownListExtend()
        {
            _values = new Hashtable();
            _DropDownList = new DropDownList();
        }

        public Hashtable Values
        {
            get { return _values; }
            set { _values = value; }
        }

        protected override void Render(HtmlTextWriter output)
        {

            int iWidth = Convert.ToInt32(base.Width.Value);
            if (iWidth == 0)
            {
                iWidth = 102;
                base.Width = Unit.Parse("102px");
            }
            int sWidth = iWidth + 20;
            int spanWidth = sWidth - 18;
            output.Write("<div style=\"POSITION:relative;\">");
            output.Write("<span style=\"MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:14px;\">");
            _DropDownList.Width = Unit.Parse(sWidth.ToString() + "px");
            _DropDownList.Height = Unit.Parse("21px");
            _DropDownList.Style.Add("MARGIN-LEFT", "-" + spanWidth.ToString() + "px");
            _DropDownList.ID = base.ID + "_Select";
            _DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.value");
            _DropDownList.Attributes.Add("onfocus", "" + this.getFocusScript() + "");

            if (_values.Count > 0)
            {
                foreach (string key in _values.Keys)
                {
                    ListItem item = new ListItem();
                    item.Value = key;
                    item.Text = _values[key].ToString();
                    _DropDownList.Items.Add(item);
                }
            }
            _DropDownList.RenderControl(output);
            output.Write("</span>");
            base.Style.Clear();
            base.Width = Unit.Parse(iWidth.ToString() + "px");
            base.Style.Add("left", "0px");
            base.Style.Add("POSITION", "absolute");
            base.Render(output);

            output.Write("</div>");
        }
        private string getFocusScript()
        {
            string strScript = "\n";
            strScript += "var isExist = -2;\n";
            strScript += "var obj = event.srcElement;\n";
            strScript += "var str = this.parentNode.nextSibling.value;\n";
            strScript += "var ary = obj.options;\n";
            strScript += "for(var i=0;i<ary.length;i++){\n";
            strScript += " if(str == ary[i].text){\n";
            strScript += "  isExist = i;\n";
            strScript += "  break;\n";
            strScript += " }\n";
            strScript += "}\n";
            strScript += "if(isExist != -2){\n";
            strScript += " obj.selectedIndex = isExist;\n";
            strScript += "}\n";
            strScript += "else{\n";
            strScript += " obj.selectedIndex = -1;\n";
            strScript += "}\n";

            return strScript;
        }
    }
}


在.aspx页面引用

页面最上边添加

<%@ Register Assembly="Controls" Namespace="Controls" TagPrefix="hc" %>

调用自定义的可输入可选择的下拉菜单

<hc:DropDownListExtend runat ="server" ID ="NewDropDownList"  Width="100px">
</hc:DropDownListExtend>

在后台.aspx.cs给控件添加数据

添加using Controls;并且在bin文件添加Controls引用

protected void Page_Load(object sender, EventArgs e)
{
	if (!IsPostBack)
        {
		NewDropDownList.Values.Add("1", "A");
		NewDropDownList.Values.Add("2", "B");
		NewDropDownList.Values.Add("3", "C");
		//用NewDropDownList.Text.ToString()取值
	}
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值