在下拉列表中绑定数据 [英] bind data in dropdown

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

问题描述

如何在数据库的下拉列表中绑定数据

How to bind data in drop-down from the database

推荐答案

在发布很容易发生的问题之前,您应该先尝试自己的研究从你的课本,课程笔记或谷歌搜索中回答。



看看这里 http://stackoverflow.com/questions/6859248/binding-data-from-the-database-to-a-dropdown -list-in-net [ ^ ]
You should really attempt to do your own research first before posting questions that could very easily have been answered from your text book, course notes or a google search.

Have a look here http://stackoverflow.com/questions/6859248/binding-data-from-the-database-to-a-dropdown-list-in-net[^]


string SqlConnect = System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
                SqlConnection Sqlconn = new SqlConnection(SqlConnect);
                SqlCommand sqlcomm = new SqlCommand("select * from Category", Sqlconn);
                
                Sqlconn.Open();
                SqlDataReader dr = sqlcomm.ExecuteReader();
                if (dr.HasRows)
                {
                   
                    DropDownList1.DataSource = dr;
                    DropDownList1.DataTextField = "CatName";
                    DropDownList1.DataValueField = "Catid";
                    DropDownList1.DataBind();
                    ListItem c = new ListItem("Select Category", "0");
                    DropDownList1.Items.Insert(0, c);
                    
                    dr.Close();
                    Sqlconn.Close();
                }


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

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