绑定下拉列表 [英] Binding DropDown List

查看:84
本文介绍了绑定下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背后的代码:

Code Behind :

private void BindDropDown()
    {
        using (SqlConnection con = new SqlConnection(@"Data Source=maj-056\sqlexpress;Initial Catalog=Sample;Integrated Security=True;Pooling=False"))
        {
            SqlCommand cmd = new SqlCommand("sp_select", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DropDownList1.DataSource = ds.Tables[0];
                DropDownList1.DataTextField = "";
                DropDownList1.DataValueField = "";
                DropDownList1.DataBind();

                DropDownList2.DataSource = ds.Tables[1];
                DropDownList2.DataTextField = "";
                DropDownList2.DataValueField = "";
                DropDownList2.DataBind();

                DropDownList3.DataSource = ds.Tables[2];
                DropDownList3.DataTextField = "";
                DropDownList3.DataValueField = "";
                DropDownList3.DataBind();
            }
        }
    }




SQL查询:





SQL Query :


create proc sp_select
as
begin
select * from Table1
end
begin
select * from Table2
end
begin
select * from Table3
end

推荐答案

那将行不通.您必须至少指定DataText字段.

而且绝对不要在查询中使用Select *.
That ain''t gonna work. You have to at least specify the DataText fields.

And NEVER use Select * in a query.


阅读以下文章并相应地修改您的过程:
http://blog.sqlauthority .com/2008/10/15/sql-server-introduction-and-example-of-union-and-union-all/ [
Read the following article and modify your procedure accordingly:
http://blog.sqlauthority.com/2008/10/15/sql-server-introduction-and-example-of-union-and-union-all/[^]


您将必须为每个下拉列表提及Datavalue和DataText字段.


datavalue和datatext字段应该是您要绑定到的相应表中相应表的现有列名.


希望对您有帮助!!!
You will have to mention the Datavalue and DataText fields for each of your drop down list.


The datavalue and datatext fileds should be the existing column names in your respective tables to which you are binding you repective drop down.


Hope this will help you!!!!


这篇关于绑定下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆