locked
How do I get the gridview row info when a LinkButton is clicked in the row RRS feed

  • Question

  • User251450640 posted

     I have a hidden field in a gridview that contains the Id (Not the DataKey) of field in a related table. In the same TemplateField I have a  LinkButton which has code to open a report based on the Id of the hidden field. I can get the value of the hidden field in the SelectedIndexChanged event using this line of code:

    Dim intId As Integer = DirectCast(gvRwpSummary.SelectedRow.Cells(1).FindControl("hfTaskID"), HiddenField).Value()

    This doesn't work in the LinkButton_Click event. I am getting close on this but need some help. Any ideas? Thanks.

    Wednesday, September 23, 2009 12:31 PM

Answers

  • User251450640 posted
           
     OK I solved it! I'll post the relevent code for others. Here is the Template field in my gridview with the hidden field:
    <asp:TemplateField HeaderText="Print Instuctions">
        <ItemTemplate>
             <asp:LinkButton ID="btnInstructions" runat="server" CausesValidation="false" onclick="btnInstructions_Click" Text="Instructions" />
             <asp:HiddenField ID="hfTaskID" runat="server" Value='<%# Eval("TaskID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
     
     
    Here is the code in the Click event of the LinkButton
     
     
            Dim LinkButton1 As LinkButton = DirectCast(sender, LinkButton)
            Dim gvr As GridViewRow = DirectCast(LinkButton1.NamingContainer, GridViewRow)
            Dim rowIndex As Integer = gvr.RowIndex
            Dim intId As Integer = DirectCast(gvr.Cells(1).FindControl("hfTaskID"), HiddenField).Value()


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 23, 2009 1:23 PM

All replies

  • User-69623574 posted

    I came across the similar problem before

    i didnt able to find why the hidden field wasn't recoginsed but found out a way to handle this.

    put this css file in the header

    <style type="text/css">
    .Hide{ display: none;}
    </style>

    call the css file for the column which you want to hide

    <asp:BoundField DataField="WSID" HeaderText="WSID">
                                            <HeaderStyle CssClass ="Hide" />
                                            <ItemStyle CssClass = "Hide" />
                                            </asp:BoundField>


    so hiding the column using a css.

    hope this helps.


    Wednesday, September 23, 2009 12:56 PM
  • User251450640 posted

     I don't believe my problem is the hidden field not giving me the data, as I said, I can retrieve the value of the field in the SelectedIndexChanged event using the code I posted above. I just can't seem to reference the field from the Click event of the LinkButton. I need to find a way of referencing the row that was clicked first.

    Wednesday, September 23, 2009 1:01 PM
  • User251450640 posted
           
     OK I solved it! I'll post the relevent code for others. Here is the Template field in my gridview with the hidden field:
    <asp:TemplateField HeaderText="Print Instuctions">
        <ItemTemplate>
             <asp:LinkButton ID="btnInstructions" runat="server" CausesValidation="false" onclick="btnInstructions_Click" Text="Instructions" />
             <asp:HiddenField ID="hfTaskID" runat="server" Value='<%# Eval("TaskID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
     
     
    Here is the code in the Click event of the LinkButton
     
     
            Dim LinkButton1 As LinkButton = DirectCast(sender, LinkButton)
            Dim gvr As GridViewRow = DirectCast(LinkButton1.NamingContainer, GridViewRow)
            Dim rowIndex As Integer = gvr.RowIndex
            Dim intId As Integer = DirectCast(gvr.Cells(1).FindControl("hfTaskID"), HiddenField).Value()


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 23, 2009 1:23 PM
  • User-232462845 posted

     Set Commnad name for Link button as "select"

    Don't Specify OnlicK for Link Button

    For GridView, set onselectedindexchanged ="GridView1_SelectedIndexChanged

    <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" Text ="Click" CommandName= "Select"></asp:LinkButton>
                </ItemTemplate>
                </asp:TemplateField>

    C# code:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write(sender.ToString());
           // or do something related to the clicked row
        }


     

     

     

    Wednesday, September 23, 2009 1:35 PM