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

来源:互联网 发布:淘宝消费总额查询 编辑:程序博客网 时间:2024/05/10 16:53

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

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

    }
}

原创粉丝点击