I want people to click on a link (generated from an asp:HyperlinkField) and have it call a method on the server rather than redirect the user somewhere. Anyone know how to do this?
Thanks,
Matt
I want people to click on a link (generated from an asp:HyperlinkField) and have it call a method on the server rather than redirect the user somewhere. Anyone know how to do this?
Thanks,
Matt
Use an asp:LinkButton instead. Unless there's a particular reason you're attached to the asp:Hyperlink?
Why not use a ButtonField instead and set the ButtonType property to a Link? It will look just like a hyperlink.
I'd use an asp:CommandField or asp:ButtonField instead and use ButtonType=Link - that will look the same as your linkfield, and then you can handle the OnRowCommand event in your grid to run your code.
HyperLinkField is used for generating simple hyperlinks in databound controls. Instead you can use ButtonField. Or you can define your own link with TemplateField.
Here is a sample of generating link column which has a server side event :
<asp:templatefield headertext="Link Column">
<itemtemplate>
<asp:LinkButton ID="myLink"
CommandName="MyLinkCommand"
CommandArgument='<%#Bind("TableID") %>'
runat="server">My Link</asp:LinkButton>
</itemtemplate>
</asp:templatefield>
At code behind :
protected void YouGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyLinkCommand")
{
// Do stuff
}
}