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");
I would generally recommend using element. attachEvent (IE) or element. addEventListener (other browsers) over setting the onclick event directly as the latter will replace any existing event handlers for that element. attachEvent / addEventListening allow multiple event handlers to be created.
This is a type of JavaScript link - the onclick attribute defines a JavaScript action when the 'onclick' event for the link is triggered (i.e. when a user clicks the link) - and there is a URL present itself in the onclick attribute.
The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button). Raising an event invokes the event handler through a delegate.
onclick Event 1 Definition and Usage. The onclick event occurs when the user clicks on an element. 2 Browser Support 3 Syntax. Note: The addEventListener () method is not supported in Internet Explorer 8 and earlier versions. 4 Technical Details 5 More Examples
System. Windows. Documents Hyperlink. Click Event System. Windows. Documents Occurs when the left mouse button is clicked on a Hyperlink. Detect when the Enter key is selected on the keyboard in Windows Presentation Foundation. This example consists of XAML and a code-behind file.
The default behavior of the <a> tag’s onclick and href properties are to execute the onclick, then follow the href as long as the onclick doesn’t return false, canceling the event (or the event hasn’t been prevented). Example of code make this a tag working with href and onClick (Add onclick event to hyperlink):-
$ ("p a").click ( function (e) { e.preventDefault (); $ (this).attr ("href"); //do something with this } ); This will add a onclick listener to every link within the page which is a child of a paragraph. If the link is really important, you should probably give it an id, so that it can be identified uniquely.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With