Yogesh
Yogesh

Reputation: 3482

Click Event of Hyperlink

How to find Whether a hyperlink is clicked or not in ASP.net C# in runtime? I want to write code on like that

Response.Redirect("Default.aspx");

Upvotes: 26

Views: 109794

Answers (5)

Pabitra Dash
Pabitra Dash

Reputation: 1513

The onclick server side handler can be added to achieve this.

<asp:LinkButton ID="LinkEditLine" runat="server" Text="Edit" onclick="lnkEdit_Click"/>

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 191056

You can determine this with the Click event of the LinkButton

Upvotes: 1

Amir Ismail
Amir Ismail

Reputation: 3883

if this HyperLink you can do it using javascript but if it is LinkButton you can do it inside onclick event

<asp:LinkButton ID="MyLnkButton" runat="server" onClick="MyLnkButton_Click" Text="Click Me!">

protected void MyLnkButton_Click(Object sender,EventArgs e)
{
   Response.Redirect("Default.aspx");
}

Upvotes: 4

Tejs
Tejs

Reputation: 41266

You would attach either the event in the code behind, or in the ASPX / ASCX of your link in question like so:

 <asp:LinkButton ID="linkGoSomewhere" runat="server" Click="linkGoSomewhere_Click" />

OR

 linkGoSomewhere.Click += (linkGoSomewhere_Click);

With an event handler looking like so in your code:

 public void linkGoSomewhere_Click(object sender, EventArgs e)
 {
      Response.Redirect("Default.aspx");
 }

HOWEVER

In this situation, you don't need a server side control to just send the user somewhere else. You just need a simple hyperlink:

 <a href="Default.aspx">Go somewhere else</a>

Upvotes: 10

balexandre
balexandre

Reputation: 75133

If you want to execute server code upon a click in a link, then you should use the ASP.NET control <asp:LinkButton>

This is just like a button and will allow you to hook up Server Side Events and at the end you can just redirect the viewer to any page.

Upvotes: 42

Related Questions