Reputation: 419
How to use a hyperlink in a gridview to execute a function in aspx
code behind when clicked passing a variable to that function which is some value of a cell within the row the hyperlink exits in.
Example: HTML Code:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="HeaderText" SortExpression="HeaderText">
<ItemTemplate>
<asp:LinkButton ID="Hyperlink1" runat="server" Text="Hyperlink1"
OnClick="SomeFunction(GridView1.Row.Cells(0).Text)" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
ASP Code Behind:
Sub SomeFunction(ByVal sVariable As Object)
'Do some stuff
End Sub
Upvotes: 0
Views: 2880
Reputation: 419
I figure it out:
1. Just call the code behind function without passing a data for this function like this 'OnClick="SomeFunction"
'.
2. In the code behind the function should appear as the following:
Sub SomeFunction(ByVal sender as object, byval e As EnventArgs)
dim lBtn as LinkButton=directcast(sender, LinkButton)
sender.PostBackUrl = "http://www.microsoft.com"
'Do some other stuff
End Sub
Upvotes: 2