26

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");
3
  • 2
    if clicked... it will go to NavigateUrl specified url... what do you really want to do?
    – balexandre
    Aug 29, 2011 at 14:37
  • In run time I changed Text of Hyperlink now if to that changed Text is clicked I want to Redirect to other Page !
    – Yogesh
    Aug 29, 2011 at 14:40
  • 1
    Now, you didn't make any sense :) can you rephrase it plz?
    – balexandre
    Aug 29, 2011 at 14:41

5 Answers 5

42

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.

10

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>
3

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");
}
2

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

<asp:LinkButton ID="LinkEditLine" runat="server" Text="Edit" onclick="lnkEdit_Click"/>
1
  • 1
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – andreas
    Sep 30, 2016 at 9:25
1

You can determine this with the Click event of the LinkButton

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.