既能输入又能选择的DropDownList控件

这是一个自定义控件SelectTextBox,它结合了TextBox和DropDownList的功能,允许用户既能输入数据,也能从下拉列表中选择。主要用于输入课程成绩,并支持补考、缓考、缺考等选项。控件支持赋值和取值操作,已验证适用于IE6和IE7浏览器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

早就想做一个这样的控件了,最近在一个项目中遇到了这样的问题,所以就写了一个。

我遇到的需求是既能输入又能选择的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;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值