[英]I want to bind data from SQL Server table column at page load in ASP.NET, but it is not working
I want to bind data from a SQL Server table column at page load in ASP.NET, but it is not working; 我想在ASP.NET中页面加载时绑定来自SQL Server表列的数据,但是它不起作用; code is working in a button click. 单击按钮即可运行代码。
This is my code - can anyone help please? 这是我的代码-有人可以帮忙吗?
if (!IsPostBack)
{
DataTable subjects = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [name] FROM [supplier]", con);
adapter.Fill(subjects);
con.Open();
DropDownreturn.DataSource = subjects;
DropDownreturn.DataTextField = "name";
DropDownreturn.DataValueField = "name";
DropDownreturn.DataBind();
DropDownreturn.Items.Insert(0, new ListItem("Select", "NA"));
con.Close();
}
but it is not working; 但它不起作用; code is working in a button click. 单击按钮即可运行代码。
When user press a button then it create a postback if Postback is enabled on button and you have placed !IsPostBack
check that may be preventing the code block to bind the dropdown. 当用户按下按钮时,如果在按钮上启用了Postback,并且您已放置!IsPostBack
检查,则它可能会创建一个回发,该检查可能阻止代码块绑定下拉列表。
To solve this issue.. At Page_Load, check if your button raised the postback then execute the code to bind the dropdown by following below suggested approach: 解决此问题的方法。在Page_Load处,按照以下建议的方法检查按钮是否引发了回发,然后执行代码以绑定下拉列表:
Which control caused the postback? 哪个控件导致了回发?
Way To Know Which Control Has Raised PostBack 知道哪个控件提高了回发的方法
if(IsPostBack)
{
if(postbackcontrol is button1)
{
//Bind dropdown
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.