可输入的下拉列表 支持dropdownlist的数据源绑定

来源:互联网 发布:网络思想政治教育故事 编辑:程序博客网 时间:2024/05/21 18:42

经过简单的开发,基本上能够使用了。网上看了个使用哈希表做数据源的,并且页面回发时,下拉表不能保持状态等。这些都不符合我的要求,于是我花了点时间修改了下,封装了dropdownlist。

控件支持dropdownlist的所有数据源绑定,属性具有基本的绑定属性,比如DataTextField、DataValueField等。控件具有值和文本,文本就是文本框中的值,值是特殊处理的,如果下拉表的项值没有匹配到,值就是文本框的值。

废话不多说。下面给出例子。我用的vs2008做的。

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestTextDrop._Default" %>

<%@ Register Assembly="Com.HanWeiKeJi.ExtendWebControls" Namespace="Com.HanWeiKeJi.ExtendWebControls"
    TagPrefix="zf" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <zf:DropDownListExtend ID="DropDownListExtend1" runat="server"
        style="top: 0px; left: 0px; width: 50px" ></zf:DropDownListExtend>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>

后台:

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 TestTextDrop
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("text", typeof(string));
                dt.Columns.Add("value", typeof(string));
                DataRow dr1=dt.NewRow();
                dr1[0] = "第一项";
                dr1[1] = "aa";
                DataRow dr2 = dt.NewRow();
                dr2[0] = "第二项";
                dr2[1] = "bb";
                dt.Rows.Add(dr1);
                dt.Rows.Add(dr2);

                DropDownListExtend1.DataSource = dt;
                DropDownListExtend1.DataTextField="text";
                DropDownListExtend1.DataValueField="value";
                DropDownListExtend1.DataBind();

                DropDownListExtend1.Text = "第一项";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string text = DropDownListExtend1.Text;
            string value = DropDownListExtend1.Value;
            Response.Write("<script>alert('文本:" + text + ",值" + value + "');</script>");
        }
    }
}

大家可以测试下效果,需要Com.HanWeiKeJi.ExtendWebControls.dll的请联系我.QQ 393216535。欢迎提要求,我再改控件。

原创粉丝点击