DropDownList绑定数据库

//获取文本
this.DropDownList1.SelectedItem.Text;

本类内使用
protected void Page_Load(object sender, EventArgs e)
    {
        this.DropDownList1.Items.Clear();
        this.DropDownList1.DataSource = DbHelperSQL.Query("select * from authors").Tables[0];
        this.DropDownList1.DataTextField = "au_id";
        this.DropDownList1.DataValueField = "au_lname";
        this.DropDownList1.DataBind();               text value
        this.DropDownList1.Items.Insert(0,new ListItem("",""));//插入空项,此举必须放到数据绑定之后

    }
别忘了要判断isPostBack
//事件selectIndexChangeing
DropDownList的Items包括ListItem对象,ListItem有Value和Text两个属性,
取值取的是value不是text  this.DropDownList1.SelectValue;//value
this.DropDownList1.SelectValue://取当前文本框中所显示的键值
//公共方法
/// <summary>
    /// DropDownList绑定数据库
    /// </summary>
    /// <param name="dropDownList">本窗体上的dropDownList控件</param>
    /// <param name="ds">如:Db.Query("select * from tbtype")</param>
    /// <param name="text">dropDownList上显示的text文本值(数据库中的name)</param>
    /// <param name="Value">value值(对应数据库中的id)</param>
    /// <param name="firstText">dropdownlist首先显示的文本值(数据库中的name)</param>
    /// <param name="firstValue">firstText对应的键(数据库中的id)</param>
    public static void BindDropDownList(object dropDownList, DataSet ds, string text, string Value, string firstText, string firstValue)
    {
        if (dropDownList == null || ds == null)
        {
            return;
        }

        if (dropDownList is System.Web.UI.WebControls.DropDownList)
        {
            ((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Clear();

            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataTextField = text;
            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataValueField = Value;
            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataSource = ds.Tables[0];
            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataBind();
            ((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Insert(0, new System.Web.UI.WebControls.ListItem(firstText, firstValue));//dropdownlist首次显示的text,value
            //if (((DropDownList)dropDownList).Items.Count > 0)
            //{
            //    ((DropDownList)dropDownList).SelectedIndex = 0;//首先显示第一项
            //}
        }

    }
-------------------------
上面方法重载,参数中没有“全部”,“全部”,即首次显示的不是”全部“
/// <summary>
    /// DropDownList绑定数据库
    /// </summary>
    /// <param name="dropDownList">本窗体上的dropDownList控件</param>
    /// <param name="ds">如:Db.Query("select * from tbtype")</param>
    /// <param name="text">dropDownList上显示的text文本值(数据库中的name)</param>
    /// <param name="Value">value值(对应数据库中的id)</param>
    /// <param name="firstText">dropdownlist首先显示的文本值(数据库中的name)</param>
    /// <param name="firstValue">firstText对应的键(数据库中的id)</param>
    public static void BindDropDownList(object dropDownList, DataSet ds, string text, string Value)
    {
        if (dropDownList == null || ds == null)
        {
            return;
        }

        if (dropDownList is System.Web.UI.WebControls.DropDownList)
        {
            ((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Clear();

            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataTextField = text;
            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataValueField = Value;
            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataSource = ds.Tables[0];
            ((System.Web.UI.WebControls.DropDownList)dropDownList).DataBind();
            //((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Insert//(0, new System.Web.UI.WebControls.ListItem(firstText, firstValue));//dropdownlist首次显示的text,value
            //if (((DropDownList)DropDownList).Items.Count > 0)
            //{
            //    ((DropDownList)DropDownList).SelectedIndex = 0;
            //}
        }

    }
//调用时要判断isPostBack
---------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
          
            PublicClass.BindDropDownList(this.DropDownList1, Db.Query("select * from BookType"), "BookTypeName", "BookTypeId", "全部", "全部");
        }
    }
--------------------------------------------------------------------
//添加项
DataTable dt = Spbase.GreatDs("Select * from Spclass").Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem Dr = new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i][0].ToString());
            DropDownList1.Items.Add(Dr);

        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要为dropdownlist绑定数据库中的数据,可以按照以下步骤操作: 1. 在页面中添加一个dropdownlist控件。 2. 在页面的代码-behind中编写代码来连接数据库并查询需要绑定的数据。比如使用ADO.NET中的SqlConnection和SqlCommand类来连接数据库并执行查询操作。 3. 将查询结果绑定dropdownlist控件上。可以使用dropdownlist控件的DataSource属性来指定数据源,使用DataTextField和DataValueField属性来指定要显示的文本和值的字段名,最后调用DataBind方法来完成绑定。 以下是一个简单的示例代码: ```c# protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string connectionString = "Data Source=your_server;Initial Catalog=your_database;User ID=your_username;Password=your_password"; string query = "SELECT id, name FROM your_table"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(query, connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "your_table"); dropdownlist.DataSource = dataSet.Tables["your_table"]; dropdownlist.DataTextField = "name"; dropdownlist.DataValueField = "id"; dropdownlist.DataBind(); } } ``` 在这个示例中,我们从名为your_table的表中查询了id和name两个字段的数据,并将其绑定到了名为dropdownlistdropdownlist控件上。其中,数据库连接字符串、查询语句、数据源字段名和值字段名需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值