views:

213

answers:

4

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

+1  A: 

Use an asp:LinkButton instead. Unless there's a particular reason you're attached to the asp:Hyperlink?

Aric TenEyck
Nope, no reason, just didn't want it to look like a button. Thanks.
Matt
A: 

Why not use a ButtonField instead and set the ButtonType property to a Link? It will look just like a hyperlink.

Matthew Jones
+1  A: 

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.

Scott Ivey
yes you're right, no need to add a template column (like my answer), this one is very easy :)
Canavar
A: 

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
    }
}
Canavar