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");
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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" rel="nofollow noreferrer noopener">Go somewhere else</a>
Method 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");
}
Method 4
The onclick server side handler can be added to achieve this.
<asp:LinkButton ID="LinkEditLine" runat="server" Text="Edit" onclick="lnkEdit_Click"/>
Method 5
You can determine this with the Click event of the LinkButton
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0