how i get the clicked row in gridview with link button add by code asp.net?

HOUSSEM MAHJOUBI 286 Reputation points
2023-01-14T09:12:42.42+00:00

Hi members

i want to get the row of the gridview when i click the linkbutton column

this is my try

 Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
        Dim lnkSubmit As LinkButton = TryCast(sender, LinkButton)
        Dim row As GridViewRow = TryCast(lnkSubmit.NamingContainer, GridViewRow)
        Dim message As String = "CustomerId: " & row.Cells(0).Text & "\n Name: " + row.Cells(1).Text & "\n Value: " + row.Cells(2).Text
        ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)


    End Sub
    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound





        If e.Row.RowType = DataControlRowType.DataRow Then
                If Not e.Row.Cells(4).Text = " " Then


                    Dim value As Integer = Integer.Parse(e.Row.Cells(4).Text)

                    If value > 0 Then
                    Dim lnkSubmit = New LinkButton()
                    lnkSubmit.ID = "lnkSubmit"
                    lnkSubmit.Text = "Ajout_AU_DEVIS"
                    lnkSubmit.CommandName = "Select"
                    lnkSubmit.CommandArgument = (e.Row.RowIndex + 1).ToString()
                    AddHandler lnkSubmit.Click, AddressOf OnSubmit
                    e.Row.Cells(9).Controls.Add(New Label() With {.Text = " "})
                    e.Row.Cells(9).Controls.Add(lnkSubmit)
                End If
                End If
            End If




    End Sub

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,289 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,581 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,876 Reputation points Microsoft Vendor
    2023-01-16T02:51:09.9533333+00:00

    Hi @HOUSSEM MAHJOUBI,

    I think you can put the linkbutton on the front.The LinkButton Click event will be implemented using the RowCommand

    event, so when the LinkButton is clicked, the RowCommand event will fire to get the cell value of the ASP.Net GridView.

    [https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowcommand?view=netframework-4.8

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150px" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton Text="Select" runat="server" CommandName="Select" CommandArgument="<%# Container.DataItemIndex %>" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Dim dt As New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
                dt.Rows.Add(1, "John Hammond", "United States")
                dt.Rows.Add(2, "Mudassar Khan", "India")
                dt.Rows.Add(3, "Suzanne Mathews", "France")
                dt.Rows.Add(4, "Robert Schidner", "Russia")
                GridView1.DataSource = dt
                GridView1.DataBind()
            End If
        End Sub
        Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
            If e.CommandName = "Select" Then
                Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
                Dim row As GridViewRow = GridView1.Rows(rowIndex)
                Dim name As String = TryCast(row.FindControl("txtName"), TextBox).Text
                Dim country As String = row.Cells(1).Text
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Name: " & name & "\nCountry: " & country + "');", True)
            End If
        End Sub
    

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2023-01-14T11:50:39.57+00:00

    Use Gridview.SelectedIndexChanged event https://learn.microsoft.com/pt-br/dotnet/api/system.web.ui.webcontrols.gridview.selectedindexchanged?view=netframework-4.8.1

    <asp:GridView ID="GridView1" runat="server" DataSourceID="MyData">
      <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" SortExpression="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Value" HeaderText="Value" SortExpression="Value" />
       </Columns>
     </asp:GridView>
    
    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
    Dim row As GridViewRow = GridView1.SelectedRow
    Dim message As String = "CustomerId: " & row.Cells(1).Text & "\n Name: " & row.Cells(2).Text & "\n Value: " & row.Cells(3).Text
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
      End Sub
    
    0 comments No comments