【问题标题】:DataBinding DataSet to GridView yields infinite loopDataBinding DataSet 到 GridView 产生无限循环
【发布时间】: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() 方法是什么样的?它很有可能再次调用你的方法。

标签: c# asp.net


【解决方案1】:

这似乎是个问题

OnDataBinding="RebindGrid"

每次绑定数据时,都会重新绑定。我们将不得不查看 RebindGrid 的代码。

【讨论】:

  • 正确。我犯了一个非常愚蠢的错误。我之前删除了一条阻止它在 Rebind 方法中再次进行数据绑定的行。完全忘记了。谢谢
【解决方案2】:

这取决于 RebindGrid 方法的实际作用,但看起来您重新绑定网格作为绑定它。

删除OnDataBinding="RebindGrid"

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 1970-01-01
    • 2013-06-05
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2011-12-08
    • 1970-01-01
    相关资源
    最近更新 更多