将表值绑定到下拉列表 [英] binding the table values to dropdown list

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

问题描述

 <   tr  >  
< td align = center class = < span class =code-keyword> style1 >
< asp:标签 runat = 服务器 ID = 角色 文本 = 角色 >
< < span class =code-leadattribute> / asp:Label >
< / td >
< ; td >
< asp:DropDownList ID = DropDownList1 runat = server DataTextField = 角色

DataValueField = ID onselectedindexchanged = DropDownList1_SelectedIndexChanged >

< / asp:DropDownList >
< / td >
< / tr >









在我的设计中,我保留了一个下拉角色和即时消息将数据库表维护为该表中的ROLEID包含两列ID,ROLE。默认情况下,我给了一个角色,1个管理员,2个领导者3经理,那么我的问题是如何将表绑定到下拉列表

解决方案

 受保护  void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
bindDropDownList();
}
protected void bindDropDownList()
{
string constr = ConfigurationManager.ConnectionStrings [ yourConnectionString< /跨度>]的ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand( select *来自ROLEID,con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = ds.Tables [ 0 ]。列[ 作用]的ToString(); DropDownList1.DataValueField = ds.Tables [ 0 ]。列[ ID]的ToString();
DropDownList1.DataSource = ds.Tables [ 0 ];
DropDownList1.DataBind();
}





请记住,您应该使用存储过程代替直接查询。


< blockquote>您好,



请查看此链接这可能对您有所帮助...



使用ASP.NET中的列表集合,枚举和数据集绑定DropDownList [ ^ ]

http:/ /www.programcall.com/7/aspnet/binding-dropdownlist-with-dataset-values-in-aspnet.aspx [ ^ ]


http://shawpnendu.blogspot.in/2009/05/how-to-bind-or-populate-data-into.html [<一个href =http://shawpnendu.blogspot.in/2009/05/how-to-bind-or-populate-data-into.html\"target =_ blanktitle =新窗口> ^ ]

<tr>
                <td align="center" class="style1">
                    <asp:Label runat="server" ID="Role" Text="Role">
                    </asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Role"

                        DataValueField="ID" onselectedindexchanged="DropDownList1_SelectedIndexChanged">

                    </asp:DropDownList>
                </td>
            </tr>





In my deisgn i kept one drop down for role and i m maintaining a database table as ROLEID in that table contains two columns ID, ROLE. by default i gaved some roles as 1 Admin, 2 Leader 3 Manager so what is my question is how to bind the table to drop down list

解决方案

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
bindDropDownList();
}
protected void bindDropDownList()
{
string constr = ConfigurationManager.ConnectionStrings["yourConnectionString"].ToString();         
SqlConnection con =new SqlConnection (constr);
con.Open();
SqlCommand com=new SqlCommand ("select * from ROLEID",con); 
SqlDataAdapter da=new SqlDataAdapter(com);
DataSet ds = new DataSet (); 
da.Fill(ds);  
DropDownList1.DataTextField = ds.Tables[0].Columns["ROLE"].ToString();         DropDownList1.DataValueField=ds.Tables[0].Columns["ID"].ToString();
DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataBind();      
}



Remember that you should use stored procedure in place of direct query.


Hi,

Please check with this links this might be help for you...

Binding DropDownList Using List Collection, Enum and DataSet in ASP.NET[^]
http://www.programcall.com/7/aspnet/binding-dropdownlist-with-dataset-values-in-aspnet.aspx[^]


http://shawpnendu.blogspot.in/2009/05/how-to-bind-or-populate-data-into.html[^]


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

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