简体   繁体   English

我想在ASP.NET中的页面加载时绑定来自SQL Server表列的数据,但是它不起作用

[英]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.

相关问题 我想根据用户ID将数据从SQL Server显示到ASP.NET文本框 - I want to display data from SQL Server to an ASP.NET textbox based on session of users id 我想使用asp.net更新我的SQL Server表,但是它不起作用 - I want to update my SQL Server table using asp.net but it doesn't work 使用asp.net表单将列添加到sql server表 - Add a column to a sql server table with a asp.net form 将Excel数据导入SQL Server 2014表的Asp.net Web页面(剃须刀)页面 - Asp.net web pages (razor) page to import Excel Data to SQL Server 2014 table ASP.NET MVC显示SQL Server表数据 - ASP.NET MVC Display SQL Server table data 从SQL Server或asp.net/C#(mvc 3)页面将数据导入excel - importing data to excel from SQL Server or asp.net/C# (mvc 3) page 使用ASP.NET MVC从记事本文件中读取数据并存储到SQL Server中的表中 - Read Data from Notepad File and Store into a Table in SQL Server using ASP.NET MVC 使用Enterprise Library从ASP.NET传递(数据表)到SQL Server - Pass (Data Table) to SQL Server From ASP.NET using Enterprise Library 如何在C#ASP.Net中将表格数据从Excel上传到SQL Server 2008 - How to upload table data from Excel to SQL Server 2008 in C# ASP.Net 如何从正在运行的 asp.net 网站上的 SQL 表中更改列名? - How can I change a column name from a SQL table on a running asp.net website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM