C# 绑定前数据网格冻结

C# 绑定前数据网格冻结,c#,asp.net,sql,C#,Asp.net,Sql,我有一个带有旧datagrid的简单ASP.net页面。当用户单击按钮时,将运行select语句并将数据绑定到网格。问题是页面加载后,屏幕就挂起了。表中只有一条记录 ASP页面 <asp:Button ID="buttonclick" OnClick="clickit" runat="server" Text="GO" /> <asp:DataGrid ID="mygrid" runat="server" AutoGenerateColumns="true">&l

我有一个带有旧datagrid的简单ASP.net页面。当用户单击按钮时,将运行select语句并将数据绑定到网格。问题是页面加载后,屏幕就挂起了。表中只有一条记录

ASP页面

<asp:Button ID="buttonclick" OnClick="clickit" runat="server" Text="GO" /> 

<asp:DataGrid ID="mygrid"  runat="server" 
AutoGenerateColumns="true"></asp:DataGrid> 

分配数据源后是否需要调用mygrid.DataBind()?它可能不会挂起来。它可能只是没有显示带有数据绑定的网格。

我已经重构了您的代码,现在应该可以工作了

public void clickit(Object sender, EventArgs e)
{
    //call the function
    this.bindGrid();
}

 //function to populate the datagrid with the data from the datasource
   private void bindGrid()
   {
    string sql = "SELECT a from table1";
    SqlConnection connection = new SqlConnection(connectionstring);
    SqlDataAdapter adap= new SqlDataAdapter(sql, connection);
    DataTable table = new DataTable();
    connection.Open();
    adap.Fill(table); //page reloads here, but hangs
    mygrid.DataSource = table;
    //bind the control with the data in the datasource
    mygrid.DataBind();
   connection.Close();
   }

您是否在不同的浏览器上尝试过该页面,如果没有,您是否会体验到相同的行为?您是否可以从家用计算机连接并填充?这不重要,但您是否应该调用mygrid.DataBind()?
public void clickit(Object sender, EventArgs e)
{
    //call the function
    this.bindGrid();
}

 //function to populate the datagrid with the data from the datasource
   private void bindGrid()
   {
    string sql = "SELECT a from table1";
    SqlConnection connection = new SqlConnection(connectionstring);
    SqlDataAdapter adap= new SqlDataAdapter(sql, connection);
    DataTable table = new DataTable();
    connection.Open();
    adap.Fill(table); //page reloads here, but hangs
    mygrid.DataSource = table;
    //bind the control with the data in the datasource
    mygrid.DataBind();
   connection.Close();
   }