javascript 启用回发 onclick 在 asp.net 中的超链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15649105/
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
enable postback onclick of a hyperlink in asp.net
提问by user544079
I have a hyperlink as
我有一个超链接
<a href="" id="link1">Click</a>
I have do enable a post back action whenever this link is clicked.
每当单击此链接时,我都会启用回发操作。
We can do
我们能做的
<a href="" id="link1" onclick="javascript:location.reload()">Click</a>
<a href="" id="link1" onclick="javascript:location.reload()">Click</a>
However, I want to do a page postback and not a page reload.
但是,我想做一个页面回发而不是页面重新加载。
Is it possible?
是否可以?
回答by Tim Schmelter
Simple, use a LinkButton
instead.
很简单,用 aLinkButton
代替。
void LinkButton_Click(Object sender, EventArgs e)
{
Label1.Text="This is a postback";
}
on aspx:
在aspx上:
<asp:LinkButton id="LinkButton1"
Text="Click Me"
OnClick="LinkButton_Click"
runat="server"/>
You are using a html link, if you want to use serverside code i would suggest to use server controls. However, if you reallly insist you could call __doPostBackmanually via javascript.
您正在使用 html 链接,如果您想使用服务器端代码,我建议您使用服务器控件。但是,如果您真的坚持可以通过 javascript 手动调用__doPostBack。
<a id="linkId" href="javascript:void(0);" onclick="__doPostBack('linkId', '');">click me</a>
回答by Hamit YILDIRIM
<a href='void()' onclick="javascript:postBackFromLink('<%=LinkButton1.ClientID %>'); return false;">Refresh Me</a><br />
bu it just usefull for usercontrol based dopostbacks
但它只对基于用户控制的 dopostbacks 有用