【发布时间】:2011-05-19 15:41:37
【问题描述】:
我正在尝试将 GridView 绑定到 DataSet,但它会引发堆栈溢出错误。当我调试它时,它运行到 DataBind 行就好了(它似乎从服务器和所有东西中获取了正确的记录),但是在执行 DataBind 之后,它跳转到方法的顶部并重新运行整个方法,这导致堆栈溢出。
我不明白为什么这不起作用。我之前用 DataTable 做过非常相似的事情,而且效果很好。
这是我的绑定方式
public void CreateGrid(String str)
{
try
{
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MY_CONNECTION_STRING"].ConnectionString;
sqlConnection.Open();
DataSet dt = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(str, sqlConnection);
adapter.Fill(dt);
sqlConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ":" + ex.Message);
}
if (dt.Tables.Count > 0)
{
Grid.DataSource = dt;
Grid.DataBind();
}
}
这是我的 HTML 部分
<asp:GridView runat="server" ID="Grid" AutoGenerateColumns="false"
OnDataBinding="RebindGrid" AllowPaging="True" PageSize="10" AllowSorting="True" CellPadding="5"
OnPageIndexChanging="Grid_PageIndexChanging"
OnSorting="Grid_Sorting"
Width="100%" CssClass="mGrid">
<Columns>
<asp:BoundField DataField="ID" ItemStyle-Width="0%"
HeaderText="" Visible="false" SortExpression="ID"/>
</Columns>
</asp:GridView>
【问题讨论】:
-
你的 RebindGrid() 方法是什么样的?它很有可能再次调用你的方法。