目的:创建可编辑(可输入)下拉框
步骤一:在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;
}
}
}
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 >
<! 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; }
}
}
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; }
}
}