↑ Grab this Headline Animator

Friday, November 21, 2008

How to call code behide function in OnClick of LinkButton (in gridview column) and get the selected GridView Row.

Generally For binding the data into Gridview we are using two technique

    1.Autogenerate property of gridview True

    2.Autogenerate property of gridview False and use Template Field.

When Autogenerate=False ,then we are using ItemTemplate for binding the gridview like below sample: GridView with LinkButton Column.

<asp:GridView ID="grdPaymentHistory" runat="server"AutoGenerateColumns="False">
  <asp:TemplateField HeaderText="Select">
    <asp:LinkButton runat="server" ID="lblConShed" OnClick="DataGridClick" Text='<%#Eval("DataBaseField")%>'> </asp:LinkButton>
 <asp:TemplateField HeaderText="First Name">
   <asp:Label ID="lblFirstName" runat="server"    Text='<%#Eval("FirstName")%>'></asp:Label>
 <asp:TemplateField HeaderText="Last Name">
    <asp:Label ID="lblLasttName" runat="server" Text='<%#Eval("LastName") %>'></asp:Label>

we are calling code behind procedure on the Onclick event of Linkbutton as shown above in the BOLD

When we click on the Linkbutton I want to get the selected row and get each cell value of the row.

Below is the function in VB.NET to Get the selected row.

Public Sub DataGridClick(ByVal sender As Object, ByVal e As EventArgs)
  Dim firstname As String
  Dim Conscd As Int32
  Dim lnkConfirm As LinkButton = TryCast(sender, LinkButton)
  Dim row As GridViewRow = TryCast(lnkConfirm.NamingContainer, GridViewRow)
  Dim lblFirstname As Label = TryCast(row.FindControl("lblFirstName"), Label)
  Dim lnkConshd As LinkButton=TryCast(row.FindControl("lblConShed"),LinkButton)

  If lblFirstname Is Not Nothing Then
    lblFirstname = lblFirstname.Text
  End If

  If lnkConshd Is Not Nothing Then
    Conscd = lnkConshd .Text
  End If

End Sub

Happy Coding...

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles