如何从Asp.net的下拉列表中检索数据? [英] How to retrieve data from dropdownlist in Asp.net ?
本文介绍了如何从Asp.net的下拉列表中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨专家!!
我的aspx页面上有一个下拉列表。
我想要从数据库中获取数据并放在此下拉列表中。无论表格中的数据是什么,它都必须显示在下拉列表中。
当我在表格中添加新项目时,应显示在下拉列表中。
请你帮我,怎么做。
谢谢。
Hi Experts !!
I have a dropdownlist in my aspx page.
I want to take the data from database and place in this dropdownlist. whatever the data is in table it must display in dropdown.
and when ever i add new item in table in should display in dropdownlist.
Please can you help me, how to do this.
Thanks.
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
If(!IsPostBack)
BindDropDownList();
}
private void BindDropDownList()
{
DataTable dtData = new DataTable();
dtData = //GetDataFromDb();
this.Ddl.DataSource = dtData;
this.Ddl.DataValueField = "ID";
this.Ddl.DataTextField = "Name";
this.Ddl.DataBind();
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
// Add data in DB
BindDropDownList();
}
请点击此链接
http://stackoverflow.com/questions/4242766 / binding-a-dropdownlist-to-a-database [ ^ ] [ ]
有几种方法:
你可以写sql命令从数据库中提取数据
SqlCommand cmd = new SqlCommand( 从表格中选择Value1,Value2;
然后按如下方式指定下拉列表的数据源:
dropdownlist1.datasource() cmd.executereader();
dropdownlist1.datatextfield =Value1 - 这意味着Value1是您想要在下拉列表中显示的内容
dropdownlist1.databind(); - 当你运行页面时,这将做什么最终绑定。
有更多的资源和差异方式。随意阅读有关不同做事方式的更多信息。
Several ways to do this:
you can write sql command that pull the data from the database as
SqlCommand cmd = new SqlCommand("Select Value1,Value2 from table");
And then specify the datasource of your dropdownlist as follows:
dropdownlist1.datasource() cmd.executereader();
dropdownlist1.datatextfield = "Value1" -- this means Value1 is what you want to show in the drop down
dropdownlist1.databind(); -- this what will do the final binding when you run the page.
There more resources and difference ways. Feel free to read more about different ways of doing things.
使用ASP.NET中的数据库绑定DropDownList [ ^ ]
使用asp.net的Ajax Cascading Dropdownlist示例与数据库 [ ^ ]
google [ ^ ]
Binding DropDownList With Database in ASP.NET[^]
Ajax Cascading Dropdownlist Sample with database using asp.net [^]
google[^]
这篇关于如何从Asp.net的下拉列表中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文