twitter-bootstrap 锚标签的 OnClick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30605874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnClick Event for Anchor Tags
提问by codeBoy
Is there an onClickevent for anchor tags? I have the following line of code:
是否有onClick锚标签事件?我有以下代码行:
<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?
在锚标记中执行此操作的最佳实践是什么?
回答by Claudio Redi
Instead of standard html anchor tag, use the LinkButtonfor this. It provides the functionality you're looking for.
而不是标准的 html 锚标记,LinkButton为此使用。它提供了您正在寻找的功能。
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.
它呈现为 HTML 锚点,因此在视觉上与您的代码相同。
回答by Yuri
yes, you can, but you need to add onserverclick attribute or use asp Hyperlink control
可以,但是需要添加onserverclick属性或者使用asp超链接控件
<a id="AnchorButton"
onserverclick="AnchorButton_Click"
runat="server">
回答by Kynginthenorth
You can do one thing, provide ID to the link button:
您可以做一件事,为链接按钮提供 ID:
<a runat="server" id="anchorTagButton">Click Me!!</a>
In the asp.net page load
在asp.net页面加载
anchorTagButton.ServerClick += new EventHandler(anchorTag_Click);
and finally
最后
protected void anchorTag_Click(object sender, EventArgs e)
{
/*do stuff here*/
}

