【问题标题】:ASP.NET Gridview rowdatabound Stackoverflow exceptionASP.NET Gridview rowdatabound Stackoverflow 异常
【发布时间】:2014-05-30 03:46:53
【问题描述】:

我有一个从 SQL 数据源填充的 gridview。

每当我打开页面时,我都会在 gridview rowdatabound 中收到 stackoverflow 异常。

是什么导致了问题?

Search.aspx

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ConsultsSQLDataSource" ForeColor="#333333" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
     <AlternatingRowStyle BackColor="White" />
     <Columns>
         <asp:BoundField DataField="DATA" HeaderText="Data" SortExpression="DATA" />
         <asp:BoundField DataField="LOCAL" HeaderText="Local" SortExpression="LOCAL" />
         <asp:BoundField DataField="URGENCIA" HeaderText="Urgencia" SortExpression="URGENCIA" />
         <asp:BoundField DataField="ESTADO" HeaderText="Estado" SortExpression="ESTADO" />
         <asp:HyperLinkField HeaderText="Pagamento" NavigateUrl="a" Text="Link" Visible="False" />
         <asp:BoundField DataField="IDPAGAMENTO" SortExpression="IDPAGAMENTO" Visible="False" />
     </Columns>
</asp:GridView>

代码隐藏

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string value = e.Row.Cells[2].Text;
 switch(value)
            {
                case "3":
                    e.Row.Cells[3].Text = "Waiting Payment";
                    HyperLinkField hp = (HyperLinkField)GridView1.Columns[4];
                    GridView1.Columns[4].Visible = true;
                    GridView1.Columns[5].Visible = true;
                    hp.NavigateUrl = "~/Account/Payments/Payment?PaymentID=" + e.Row.Cells[5].Text; //Exception occurs here
                    hp.Text = "Pay";
                    e.Row.Cells[4].Visible = true;
                    break;
            }
        }
    }

【问题讨论】:

  • 我得到的唯一信息是:System.Data.dll 中发生了“System.StackOverflowException”类型的未处理异常
  • 是的,我从 e.Row.Cells[5].Text 中正确获取了值
  • 如果您遵循以下操作,它会改变什么: hp.NavigateUrl = string.Format("~/Account/Payments/Payment?PaymentID={0}", + e.Row.Cells[5].文字;)
  • 只需在您的 GridView1_RowDataBound 方法上放置一个调试器,然后查看它在哪一行给出错误。

标签: c# asp.net gridview stack-overflow


【解决方案1】:

我注意到,如果您像您一样通过引用 HyperLinkFiled (GridView1.Columns[4]) 将 NavigateUrl 和 Text 分配给超链接,则它不会分配给当前行,而是分配给似乎不是的下一行你所期望的。

像这样重建你的 RowDataBound 方法:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string value = e.Row.Cells[2].Text;
            switch (value)
            {
                case "3":
                    e.Row.Cells[3].Text = "Waiting Payment";
                    HyperLink hp = e.Row.Cells[4].Controls[0] as HyperLink;
                    hp.NavigateUrl = "~/Account/Payments/Payment?PaymentID=" + e.Row.Cells[5].Text;
                    hp.Text = "Pay";
                    break;
            }
        }
    }

并从 HyperLinkField 和网格标记中的最后一个 BoundField 中删除 visible="false"

您可以从 HyperLinkField 中删除 Text 和 NavigateUrl 属性,这样只有在存在正确链接时才在单元格中显示内容。

尝试一下,看看是否仍然出现错误。

【讨论】:

    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    相关资源
    最近更新 更多