可输入的下拉框(DropDownList)
2007-05-25 20:23
162 查看
1 新建ascx用户控件
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EditableSelect.ascx.cs" Inherits="EditableSelect" %>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<span style="width:18px;border:0px solid red;">
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</span>
<style type="text/css">
.<%=Client%>showarrow...{margin-left:-<% =Width%>px;width:<% =(Width+18)%>px;}
.<%=Client%>textboxwidth...{width:<% =(Width)%>px;border-right-width:0px;<% =TextStyle%>}
</style>
2 代码
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 EditableSelect : System.Web.UI.UserControl
...{
private int width = 200;
private string textstyle = "";
bool enable = true;
protected void Page_Load(object sender, EventArgs e)
...{
this.DropDownList1.Attributes.Add("onchange", "var obj=this.parentElement.parentElement.getElementsByTagName('input')[0];obj.focus();obj.value=this.value;");
if (!Page.IsPostBack)
...{
if (this.DropDownList1.Items.Count > 0)
...{
this.TextBox1.Text = this.DropDownList1.SelectedValue;
}
}
this.DropDownList1.CssClass = this.Client + "showarrow";
this.TextBox1.CssClass = this.Client + "textboxwidth";
}
/**//// <summary>
/// 下拉框对象
/// </summary>
public DropDownList Select
...{
get
...{
return this.DropDownList1;
}
}
/**//// <summary>
/// 文本框对象
/// </summary>
public TextBox InputText
...{
get
...{
return this.TextBox1;
}
}
/**//// <summary>
/// 选定文本
/// </summary>
public string Text
...{
get
...{
return this.TextBox1.Text;
}
set
...{
this.TextBox1.Text = value;
}
}
public int Width
...{
get
...{
return this.width;
}
set
...{
this.width = value;
}
}
public string Client
...{
get
...{
return this.ClientID;
}
}
public string TextStyle
...{
set
...{
this.textstyle = value;
}
get
...{
return this.textstyle;
}
}
public bool Enable
...{
set
...{
enable = value;
this.TextBox1.Enabled = enable;
this.DropDownList1.Enabled = enable;
}
get
...{
return enable;
}
}
}
3 测试页
<%@ Register TagPrefix="TDL" TagName="TextDropListShow" Src="~/EditableSelect.ascx" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BooksManager.aspx.cs" Inherits="InterFace_Books_BooksManager" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>下拉框测试</title>
</head>
<body>
<form id="formBooksManager" runat="server">
<div>
<TDL:TextDropListShow ID="txtDrp" runat="server" />
</div>
</form>
</body>
</html>
相关使用:
protected void Page_Load(object sender, EventArgs e)
{
txtDrp.Select.Items.Add("One");
txtDrp.Select.Items.Add("Two");
txtDrp.Select.Items.Add("Three");
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = this.txtDrp.Text;
Label1.Text = this.txtDrp.Select.SelectedValue.ToString();
}
在FireFox下可以,但是IE下不行
参考文献:http://bbs.51js.com/redirect.php?fid=22&tid=58823&goto=nextnewset
在IE下的运行可以修改代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EditableSelect.ascx.cs" Inherits="EditableSelect" %>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<span style="width:18px;border:0px solid red; position:relative; "><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList></span>
<style type="text/css">
.<%=Client%>textboxwidth{width:<% =(Width)%>px;border-right-width:0px;<% =TextStyle%>}
.<%=Client%>showarrow{margin-left:-<% =Width%>px;width:<% =(Width+18)%>px;}
</style>
运行结果:
测试通过!
相关文章推荐
- DropDownList实现可输入可选择(两种版本可选)
- AJAX基本应用之:根据输入内容自动填充下拉框
- 关于GridView RowUpdating事件中处理一些前台特殊控件,例如 下拉框DropDownList 等等
- 可输入的下拉框
- 可输入的下拉框(简易实现)
- yii select下拉框dropdownlist
- JQuery EasyUI+Struts2自动匹配输入下拉框(combobox)
- JQuery_JAVA_利用jQuery的AJAX和JSON实现可输入下拉框提示
- DropDownList 下拉框选择改变,促发事件和防全局刷新(记录)
- 可输入文字查找ajax下拉框控件 ComBox的实现方法
- 可输入的dropdownlist+服务器端的数据
- 可输入的下拉框
- 可输入的下拉框
- ASP.NET中如何让DropDownList控件既能做选择又能做输入用(转载)
- 每日总结(二)C#判断输入内容为数字、Sharepoint回收应用程序池错误、Asp.Net 会发或回调参数无效、DropDownList动态绑定且设置默认选择项
- select下拉框可输入值
- 可输入的下拉框
- 自定义控件 实现文本框输入模仿下拉框选择功能
- 可输入的下拉框
- 当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com