如何绑定绑定下拉列表 [英] how to bind bind Dropdown List
本文介绍了如何绑定绑定下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataTable ds = new DataTable();
ds = select.SelectData("Sp_Erp_Pur_PRQuotationMaster_Select");
ddl_Show.DataSource = ds;
ddl_Show.DataTextField = "Quotation_ID";
ddl_Show.DataValueField = "Quotation_ID";
ddl_Show.DataBind();
这是我的绑定下拉列表代码select.selectData是我使用的我的Dll名称从sql server获取数据
此代码适用于同一个下拉列表,而不是asp.net中的其他页面
this is my code of binding dropdownlist select.selectData is my Dll Name that i used for fetching data from sql server
this code is work for same dropdownlist in one page and not for anther page in asp.net
推荐答案
DataTable dtTable = new DataTable();
try
{
using (SqlConnection sqlConnection = new SqlConnection("Your connection"))
{
using (SqlCommand sqlCommand = new SqlCommand("Your Selection Query", sqlConnection))
{
sqlConnection.Open();
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
dtTable.Load(sqlDataReader);
sqlDataReader.Close();
}
}
}
}
catch (Exception error)
{
throw error;
}
ddlList.DataSource = dtTable;
ddlList.DataValueField = "id";
ddlList.DataTextField = "id";
ddlList.DataBind();
1.在ASP.NET中,用于初始化控件的事件很重要,通常是Page_Load
使用了事件,但在某些情况下也可以Page_Init
。有关详细信息,请参阅 ASP.NET页面生命周期概述 [< a href =http://msdn.microsoft.com/en-us/library/ms178472(v=vs.80).ASPX\"target =_ blanktitle =New Window> ^ ]
2.在您的情况下,您应该将您的代码初始化并绑定到Page_Load
事件中的下拉列表但是仅当页面没有回发时,如下一个示例所示:
1.In ASP.NET is important the event that you use to init your controls, in generally thePage_Load
event is used but could be alsoPage_Init
in some situations. For more details see ASP.NET Page Life Cycle Overview[^]
2.In your case you should place your code that init and bind your drop down list inPage_Load
event but only when the page is not post back, like in the next example:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Put your code for init and binding here!
}
}
SqlCommand cmd = new SqlCommand("SPC", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();
SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();
DropDownList1.DataSource = ddlValues;
DropDownList1.DataValueField = "theName";
DropDownList1.DataTextField = "theName";
DropDownList1.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
这篇关于如何绑定绑定下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文