xRed
xRed

Reputation: 1997

ASP.NET Gridview rowdatabound Stackoverflow exception

I have a gridview being filled from a SQL data source.

Whenever I open the page I get a stackoverflow exception in the gridview rowdatabound.

What is causing the problem?

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>

Code-Behind:

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;
            }
        }
    }

Upvotes: 1

Views: 21167

Answers (1)

PrzemG
PrzemG

Reputation: 795

I've noticed that if you assign NavigateUrl and Text to hyperlink by referencing HyperLinkFiled like you do (GridView1.Columns[4]) it isn't assigned to current row but to the next row which doesn't seem to be what you expect.

Rebuild your RowDataBound method like that:

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;
            }
        }
    }

and remove visible="false" from HyperLinkField and last BoundField in grid markup.

You can remove Text and NavigateUrl properties from HyperLinkField so you only show content in the cell if there is correct link.

Try it and see if you still getting error.

Upvotes: 3

Related Questions