使用自定义控件创建可编辑下拉框

 

目的:创建可编辑(可输入)下拉框

步骤一:在VS2005中 新建WEB 控件库 WebControlLibrary1 

步骤二:建立自定义控件类DropDownListExtend

 

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

namespace  WebControlLibrary1
{

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

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




        
public DropDownListExtend()
        
{
            _values 
= new Hashtable();
            _DropDownList 
= new DropDownList();
        }





        
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 + 16;
            
int spanWidth = sWidth - 18;

            output.Write(
"<div style="POSITION:relative">");
            output.Write(
"<span style="MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px">");

            _DropDownList.Width 
= Unit.Parse(sWidth.ToString() + "px");
            _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 
= _values[key].ToString();
                    item.Text 
= key;

                    _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 = " ";
            strScript 
+= "var isExist = -2; ";
            strScript 
+= "var obj = event.srcElement; ";
            strScript 
+= "var str = this.parentNode.nextSibling.value; ";
            strScript 
+= "var ary = obj.options; ";
            strScript 
+= "for(var i=0;i<ary.length;i++){ ";
            strScript 
+= " if(str == ary[i].text){ ";
            strScript 
+= "  isExist = i; ";
            strScript 
+= "  break; ";
            strScript 
+= " } ";
            strScript 
+= "} ";
            strScript 
+= "if(isExist != -2){ ";
            strScript 
+= " obj.selectedIndex = isExist; ";
            strScript 
+= "} ";
            strScript 
+= "else{ ";
            strScript 
+= " obj.selectedIndex = -1; ";
            strScript 
+= "} ";

            
return strScript;
        }

    }

}


 步骤三: 编译工程,生成跟WEB控件库工程名字一样的DLL,如WebControlLibrary1.dll

步骤四: 在需要使用下拉框的工程添加应用(就是在其BIN目录里添加WebControlLibrary1.dll)

步骤五:具体应用:

            页面代码如下:

 

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " MasterPage.aspx.cs "  Inherits = " Module_DataManage_MasterPage "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN "   >
< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
    
         
< EditDDLPreFix:DropDownListExtend ID = " EditableDDL "  runat = " server "  Text = " text "  Width = " 93px " ></ EditDDLPreFix:DropDownListExtend >      
      
    
</ form >
</ body >
</ html >

 

CS文件如下:

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  Module_DataManage_MasterPage : System.Web.UI.Page

{
    
public Hashtable values;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        values 
= new Hashtable();
        values.Add(
"a""a");
        values.Add(
"b""b");
        values.Add(
"c""c");
        
this.EditableDDL.Values=values;
    }


    
public Hashtable Values {

        
get return values; }
        
set { values = value; }
    }


   

}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值