Search code examples
c#asp.netgridviewfindcontrol

GridView Button reference on rowdatabound


I feel like I'm missing something obvious here but I'm just not getting it. I have a gridview with a template field column where i have a button and a hidden field. I'm trying to get the reference to both the button and the hidden field on row databound, as both the button's label and commandargument etc change depending on the other row data.

If i place a breakpoint in the area indicated in the code below, I can see that the hidden field is being assigned to properly, but the button is not. What am I missing here?

GridView.aspx

<asp:GridView ID="gvCurrentQueueStatus" runat="server" AutoGenerateColumns="False"
    OnRowDataBound="gvCurrentQueueStatus_RowDataBound">
    <Columns>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Service Controls">
                <ItemTemplate>
                    <asp:Button ID="btnSubmitCommand" runat="server" Text="Control" OnClick="btnSubmitCommand_Click" />
                    <asp:HiddenField ID="hdnQueueNumber" runat="server" Value='<%# Eval("ReportQueueNumber") %>' />
                </ItemTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>

GridView.aspx.cs

protected void gvCurrentQueueStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        Button controlButton = (Button)e.Row.FindControl("btnSubmitCommand"); // FindControl fails
        HiddenField hdnQueueNumber = (HiddenField)e.Row.FindControl("hdnQueueNumber"); // FindControl succeeds

        // Other stuff
    } // breakpoint here, successfully finds htnQueueNumber, but not btnSubmitCommand
}

Solution

  • So apparently my button was in fact getting properly assigned to, it was just not reporting as such when mousing over the variable in my debugger.

    I'm not actually crazy, Visual Studio's debugger is however and needed the old "have you tried turning it off and on again".

    Sorry about that those that helped look into my issue :3