Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClick Event for Anchor Tags

Is there an onClick event for anchor tags? I have the following line of code:

<li><a href="#" runat="server">Logout</a></li>

When the user clicks the logout text I want it to fire some code that would be in a method like this:

protected void btnLogout_Click(object sender, EventArgs e)
{
    Session.RemoveAll();
    Session.Abandon();
}

What is the best practice at doing this in an anchor tag?

like image 845
codeBoy Avatar asked Oct 24 '25 20:10

codeBoy


2 Answers

Instead of standard html anchor tag, use the LinkButton for this. It provides the functionality you're looking for.

Here you have a sample

<asp:LinkButton id="btnLogout" Text="Logout" OnClick="btnLogout_Click" runat="server"/>

It renders to an HTML anchor so visually it's the same as your code.

like image 200
Claudio Redi Avatar answered Oct 27 '25 11:10

Claudio Redi


yes, you can, but you need to add onserverclick attribute or use asp Hyperlink control

  <a id="AnchorButton"
     onserverclick="AnchorButton_Click"
     runat="server">
like image 39
Yuri Avatar answered Oct 27 '25 09:10

Yuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!