locked
How do I find a label in the same row of a gridview as the button command I pressed? RRS feed

  • Question

  • User1239008133 posted

    I have a gridview that is populated from a database.  Each row has a number of button commands.  If I press the button command in row 3, I would like to grab the label information from that same row... help?

    aspx gridview (I want to access lblTheId.Text when command:editthisid is selected) - for the same row...:

                <asp:GridView ID="QuestionGridView" runat="server" CellPadding="2" ForeColor="#333333"
    GridLines="None" AutoGenerateColumns="False" HorizontalAlign="Left" Width="701px"
    EmptyDataText="No questions" OnRowCommand="QuestionGridView_RowCommand" OnRowCreated="rrowcreated"
    DataKeyNames="Id">
    <FooterStyle BackColor="Black" Font-Bold="True" ForeColor="Blue" />
    <Columns>

    <asp:TemplateField HeaderText="">
    <ItemTemplate>
    <asp:Label ID="lblTheId" runat="server" Text='<%# Eval("Id") %>' Visible="true" BorderWidth="0"></asp:Label>
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Left" Width="100%" BorderColor="Red" />
    </asp:TemplateField>


    <asp:ButtonField ButtonType="Button" CommandName="editthisid" HeaderText=""
    Text="EDIT" ItemStyle-HorizontalAlign="Center"
    HeaderStyle-HorizontalAlign="Center" ControlStyle-BackColor="red">
    <ControlStyle BackColor="Red"></ControlStyle>
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:ButtonField>

    ....................................................


    Tuesday, March 23, 2010 3:11 PM

Answers

  • User-577741185 posted

    C#

        if (e.CommandName == "editthisid")
            {
                GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                string Text = ((Label)row.FindControl("lblTheId")).Text;
            }

    VB

       If e.CommandName = "editthisid" Then
           Dim row As GridViewRow = DirectCast((DirectCast(e.CommandSource, Button).NamingContainer), GridViewRow)
           Dim Text As String = DirectCast(row.FindControl("lblTheId"), Label).Text
       End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 4:37 PM
  • User626880745 posted

    in the RowCommand event handler you can do this:

    //since you're using a ButtonField you can also get the RowIndex from the CommandArgument (refer):
    int RowIndex = Convert.ToInt32(e.CommandArgument);

    GridViewRow row = GridView1.Rows[RowIndex];

    //and now find the label on the row

    Label lbl = (Label)row.FindControl("LabelIDHere");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 4:40 PM

All replies

  • User-577741185 posted

    C#

        if (e.CommandName == "editthisid")
            {
                GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                string Text = ((Label)row.FindControl("lblTheId")).Text;
            }

    VB

       If e.CommandName = "editthisid" Then
           Dim row As GridViewRow = DirectCast((DirectCast(e.CommandSource, Button).NamingContainer), GridViewRow)
           Dim Text As String = DirectCast(row.FindControl("lblTheId"), Label).Text
       End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 4:37 PM
  • User626880745 posted

    in the RowCommand event handler you can do this:

    //since you're using a ButtonField you can also get the RowIndex from the CommandArgument (refer):
    int RowIndex = Convert.ToInt32(e.CommandArgument);

    GridViewRow row = GridView1.Rows[RowIndex];

    //and now find the label on the row

    Label lbl = (Label)row.FindControl("LabelIDHere");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 4:40 PM
  • User1239008133 posted

    thanks to all..


    Thursday, March 25, 2010 9:07 AM