applechong 发表于 2013-2-5 08:49:07

.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>
   
    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.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.value;".ToString();
         
      }
    }
}
页: [1]
查看完整版本: .net复合控件之 可输入的下拉控件