.net复合控件之 可输入的下拉控件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > .net复合控件之 可输入的下拉控件

.net复合控件之 可输入的下拉控件

 2011/8/10 17:21:39  applechong  http://weizhigan.iteye.com  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Text;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Collections;namespaceQC_Control{///<summary>///自定义可输入下拉框类///</summary>
  • 标签:.net net 控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace QC_Control
{
    /// <summary>
    /// 自定义可输入下拉框类
    /// </summary>
    [ToolboxData("<{0}:CustomInputDropdownControl runat=server></{0}:CustomInputDropdownControl>")]
    public class CustomInputDropdownControl : TextBox
    {
       
        private DropDownList ddlListItem = new DropDownList();       //下拉框
        private TextBox TxtBox = new TextBox();
        private Hashtable _values = new Hashtable();               //用于绑定下拉框框的值

        /// <summary>
        /// 键值引用变量set/get封装方法
        /// </summary>
        public Hashtable Item
        {
            get {
                if (ViewState["Values"] == null)
                {

                    ViewState["Values"] = new Hashtable();

                }
                else { }

                Hashtable s = (Hashtable)ViewState["Values"];
                return s;
            }
            set { ViewState["Values"] = value; }
        }


        /// <summary>
        /// default constructor
        /// initialization DropDownList and Hashtable instance variable
        /// </summary>
        public CustomInputDropdownControl()
        {
           // this._values = new Hashtable();
           // this.ddlListItem = new DropDownList();
        }

        /// <summary>
        /// 重写控件类Render方法体
        /// </summary>
        /// <param name="output"></param>
        protected override void Render(HtmlTextWriter output)
        {   ddlListItem.ID ="Ddl"+ base.ID;
            TxtBox.ID ="Txt"+ base.ID;
            // 框架的开始
            output.Write("<table cellspacing='0' cellpadding='0'  border='0'><tr><td align='left'><span style='position:absolute;border:1pt solid #c1c1c1;overflow:hidden;width:188px;height:19px;clip:rect(-1px 190px 190px 170px);'>");
          
            //此处之所以用clientID是因为当在卡片选项时,.net程序会将base.ID自动命名名字
            ddlListItem.Attributes.Add("onChange", getFocusValue(TxtBox.ID, ddlListItem.ID));
            ddlListItem.Attributes.Add("style", "width:190px;height:23px;margin-top:-2px;");
            TxtBox.Attributes.Add("style", "width:170px;height:15px;border:0pt;");
            //遍历数据到下拉框对象中
            if (this.Item.Count > 0)
            {
                foreach (string key in Item.Keys)
                {
                    ListItem item = new ListItem();
                    item.Value = key;
                    item.Text = Item[key].ToString();
                    ddlListItem.Items.Add(item);
                }
            }
         ddlListItem.RenderControl(output);
         output.Write("</span><span style='position:absolute;border-top:1pt solid #c1c1c1;border-left:1pt solid #c1c1c1;border-bottom:1pt solid #c1c1c1;width:170px;height:19px;'>");
         TxtBox.RenderControl(output);
            // 框架的结尾
            output.Write("</span> </td></t></table>");
        }

      //响应事件方法
        public string getFocusValue(string TxtId,string ddlId)
        {
            return "javas"+"cript:"+"document.getElementById('" + TxtId + "').value=document.getElementById('" + ddlId + "').options[document.getElementById('" + ddlId + "').selectedIndex].value;".ToString();
          
        }
    }
}
发表评论
用户名: 匿名