locked
Disable a button outside of gridview when gridview has no rows RRS feed

  • Question

  • User-1767698477 posted

    I understand that I can use Gridview_Rowdatabound like so:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
    If GridView1.Rows.Count = 0 Then
    Dim btn As Button= ???
    End If

    So how do I locate an disable a button that is outside of the gridview on page load? Is that even possible? I want to disable a button outside the gridview if there are no rows in the gv.

    Tuesday, April 28, 2020 4:41 AM

Answers

  • User409696431 posted

    In the GridView DataBound event (not RowDataBound), you'd count the GridView rows, and conditionally disable the button.  You'd find the button using FindControl the same way you'd find any other control on the page, if it is nested inside other controls.  Depending on your page layout, it may be directly available in the GridView DataBound event without using FindControl.

    In a simple example with a GridView1, and a Button1 right below that, both inside the Form tag and not nested in anything else, the C# code is simply:

     protected void GridView1_DataBound(object sender, EventArgs e)
    {
    if (GridView1.Rows.Count <= 0)
    {
    Button1.Enabled = false;
    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 6:46 AM