0

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
}
Kritner
  • 13,557
  • 10
  • 46
  • 72
  • do a google search but in the mean time this should help point you in the right direction.. also e.Row.FindControl should be `(Button).GridView.row.FindControl("your control to find")` http://stackoverflow.com/questions/9915086/how-can-i-get-the-value-of-hidden-field-in-grid-view – MethodMan Oct 23 '14 at 15:23
  • 1
    @DJKRAZE GridView.row isn't valid in this context so i'm not sure how that would work. Additionally in that SO article linked there is an equivalent answer for hiddenField (which as i stated in my question i have working), but for whatever reason it is not working for button. – Kritner Oct 23 '14 at 16:56

2 Answers2

1

Try this:

Button controlButton = e.Row.FindControl("btnSubmitCommand") as Button ;

Or try this way:

     foreach (GridViewRow row in gvCurrentQueueStatus.Rows) {
   Button controlButton = (Button)gvCurrentQueueStatus.Rows[row.RowIndex].FindControl("btnSubmitCommand");
  }
Rex
  • 1,134
  • 15
  • 26
  • that's pretty much the same thing I was doing just a slightly different syntax isn't it? I'm not sure what the wording is, but isn't it just two methods of casting to a specific object? Either way I tried it, and no success :( – Kritner Oct 23 '14 at 16:51
  • Unfortunately this does not work either. It really seems like for whatever reason FindControl does not seem to work in GridViewRow for buttons – Kritner Oct 23 '14 at 18:12
0

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

Kritner
  • 13,557
  • 10
  • 46
  • 72