既能输入又能选择的DropDownList控件
2009-01-09 11:14
253 查看
早就想做一个这样的控件了,最近在一个项目中遇到了这样的问题,所以就写了一个。
我遇到的需求是既能输入又能选择的DropDownList,其中输入的是课程成绩,选择的是补考、缓考、缺考。
此控件支持取值和赋值。支持IE6、IE7,其他的没测。
SelectTextBox.ascx
<style type="text/css">
.cls{
POSITION:absolute;
}
</style>
<div class="cls" style="WIDTH: 85px;z-index:100" align="center">
<iframe style="position:absolute;z-index:-1;width:expression(this.nextSibling.offsetWidth);height:expression(this.nextSibling.offsetHeight);top:expression(this.nextSibling.offsetTop);left:expression(this.nextSibling.offsetLeft);" frameborder="0" ></iframe>
<asp:TextBox ID="TextBox1" runat="server" Width="85px" Height="15px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</div>
<asp:DropDownList ID="DropDownList1" runat ="server" Width="107px" Height="20px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" ></asp:DropDownList>
SelectTextBox.ascx.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;
namespace JKCollegeManage.WebPage.Score
{
public partial class SelectTextBox : System.Web.UI.UserControl
{
private string _text;
protected void Page_Load(object sender, EventArgs e)
{
}
// Text 重写了TextBox的Text属性
public String Text
{
get
{
_text = this.TextBox1.Text;
return _text;
}
set
{
this.TextBox1.Text = value;
_text = this.TextBox1.Text;
}
}
//为控件增加一个可选择的项
public void AddItem(String item)
{
this.DropDownList1.Items.Add(item);
}
//为控件增加一个可选择的项
public void AddItem(ListItem item)
{
this.DropDownList1.Items.Add(item);
}
//为控件增删除一个可选择的项
public void RemoveItem(ListItem item)
{
this.DropDownList1.Items.Remove(item);
}
//为控件增删除一个可选择的项
public void RemoveItem(String item)
{
this.DropDownList1.Items.Remove(item);
}
//清除控件数据
public void Clear()
{
this.DropDownList1.Items.Clear();
this.TextBox1.Text = "";
}
//返回选项数量
public int Count
{
get
{
return this.DropDownList1.Items.Count;
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
//选择某项
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox1.Text = this.DropDownList1.SelectedItem.Text;
}
}
}
我遇到的需求是既能输入又能选择的DropDownList,其中输入的是课程成绩,选择的是补考、缓考、缺考。
此控件支持取值和赋值。支持IE6、IE7,其他的没测。
SelectTextBox.ascx
<style type="text/css">
.cls{
POSITION:absolute;
}
</style>
<div class="cls" style="WIDTH: 85px;z-index:100" align="center">
<iframe style="position:absolute;z-index:-1;width:expression(this.nextSibling.offsetWidth);height:expression(this.nextSibling.offsetHeight);top:expression(this.nextSibling.offsetTop);left:expression(this.nextSibling.offsetLeft);" frameborder="0" ></iframe>
<asp:TextBox ID="TextBox1" runat="server" Width="85px" Height="15px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</div>
<asp:DropDownList ID="DropDownList1" runat ="server" Width="107px" Height="20px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" ></asp:DropDownList>
SelectTextBox.ascx.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;
namespace JKCollegeManage.WebPage.Score
{
public partial class SelectTextBox : System.Web.UI.UserControl
{
private string _text;
protected void Page_Load(object sender, EventArgs e)
{
}
// Text 重写了TextBox的Text属性
public String Text
{
get
{
_text = this.TextBox1.Text;
return _text;
}
set
{
this.TextBox1.Text = value;
_text = this.TextBox1.Text;
}
}
//为控件增加一个可选择的项
public void AddItem(String item)
{
this.DropDownList1.Items.Add(item);
}
//为控件增加一个可选择的项
public void AddItem(ListItem item)
{
this.DropDownList1.Items.Add(item);
}
//为控件增删除一个可选择的项
public void RemoveItem(ListItem item)
{
this.DropDownList1.Items.Remove(item);
}
//为控件增删除一个可选择的项
public void RemoveItem(String item)
{
this.DropDownList1.Items.Remove(item);
}
//清除控件数据
public void Clear()
{
this.DropDownList1.Items.Clear();
this.TextBox1.Text = "";
}
//返回选项数量
public int Count
{
get
{
return this.DropDownList1.Items.Count;
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
//选择某项
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox1.Text = this.DropDownList1.SelectedItem.Text;
}
}
}
相关文章推荐
- ASP.NET中如何让DropDownList控件既能做选择又能做输入用?
- ASP.NET中如何让DropDownList控件既能做选择又能做输入用(转载)
- 表格单元格内嵌IP地址输入选择控件
- js 获取asp 控件DropDownList的选择值,及所有的text和value
- DropDownList既能选择又能输入
- winForm ComboBox 控件默认值绑定及只可选择不可输入设定处理
- ASP.NET中用三个DropDownList控件方便的选择年月日
- 偶写的第一个控件,一个用选择代替输入的Edit控件
- ASP.NET:DataList控件的选择输入功能
- 选择输入日期控件源代码
- DropDownList既能选择又能输入
- MFC 控件下拉选择和输入的 ComboBox使用
- DropDownList既能选择又能输入
- HTML5新控件 - 日期和时间选择输入
- 能够选择时间的 JS 日历时间输入控件
- DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 转:zTree树控件扩展篇:巧用zTree控件实现文本框输入关键词自动模糊查找zTree树节点实现模糊匹配下拉选择效果
- ASP.NET中DropDownList控件的简单使用--DDL绑定数据库表中值,选择DDL中值直接检索数据
- 在ASP.NET中用三个DropDownList控件方便的选择年月日【原创】