Javascript 如何让 Html Link(锚点)像 LinkButton 一样进行回发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2476982/
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
How to let Html Link (anchor) do a postback to be like LinkButton?
提问by Ahmed Magdy
I would like to ask how can i make an html anchor (a element) or even any object to do a postback or to execute an server side method? I want to create a custom button (a wrapped with some divs to do some custom them) and i want to implement OnClick to be look like the ASP.NET LinkButton?
我想问一下如何制作 html 锚点(元素)甚至任何对象来进行回发或执行服务器端方法?我想创建一个自定义按钮(用一些 div 包裹起来做一些自定义),我想实现 OnClick 使其看起来像 ASP.NET LinkButton?
Like
喜欢
<a href="#" onclick="RunServerSideMethod()">Just a simple link button</a>
采纳答案by Brian Mains
By default, controls use __doPostBack to do the postback to the server. __doPostBack takes the UniqueID of the control (or in HTML, the name property of the HTML element). The second parameter is the name of the command to fire.
默认情况下,控件使用 __doPostBack 回发到服务器。__doPostBack 采用控件的 UniqueID(或在 HTML 中,HTML 元素的 name 属性)。第二个参数是要触发的命令的名称。
So for a custom button, render to the output stream:
所以对于自定义按钮,渲染到输出流:
<a id="someclientid" name="someuniqueid" href="javascript:void(0);" onclick="__doPostBack('someuniqueid', '');">val</a>
In your custom button, add the IPostBackEventHandler, and this __doPostBackstatement will fire its RaisePostBackEventmethod automatically for you.
在您的自定义按钮中,添加IPostBackEventHandler,此__doPostBack语句将RaisePostBackEvent自动为您触发其方法。
回答by Oded
Use a server side html control, HtmlAnchorwhich is a server side a tag.
使用服务器端html控件,HtmlAnchor也就是服务器端一个标签。
<asp:HtmlAnchor runat="server" onclick="RunServerSideMethod">Just a simple link</asp:HtmlAnchor>
回答by Nitish
Just add on anchor tag --> runat="server" onServerClick="Your function name", it solves your problem.
只需添加锚标记--> runat="server" onServerClick="您的函数名称",它就可以解决您的问题。
回答by Vardhini
One workaround could be :
invoke dummyButton click in client side event of anchor tag - which will call server side event of this dummy Button by default. so if u place ur server side code in this dummyButton server event - calling anchor tag client side event would invoke this server side dummy button event.
一种解决方法可能是:
在锚标记的客户端事件中调用 dummyButton 单击 - 默认情况下将调用此虚拟按钮的服务器端事件。所以如果你把你的服务器端代码放在这个 dummyButton 服务器事件中 - 调用锚标记客户端事件将调用这个服务器端虚拟按钮事件。
Code:
代码:
<a id="ancLink" href="javascript:void(0);" >back</a>
<asp:Button ID="dummyRefresh" runat="server" OnClick="BtnRefreshOnclick" style="display:none"/>
Javascript:
Javascript:
ancLink.live("click", function () {
callDummyButtonServerEvent();
});
function callDummyButtonServerEvent() {
$('input[id$=dummyRefresh]').click();
}
回答by Justin Emery
To do this without relying on ASP.NET, RunServerSideMethod() should be a javascript function that uses Ajax to send a request to the server.
要在不依赖 ASP.NET 的情况下做到这一点,RunServerSideMethod() 应该是一个使用 Ajax 向服务器发送请求的 javascript 函数。
Try this Ajax tutorial: http://www.w3schools.com/ajax/
试试这个 Ajax 教程:http: //www.w3schools.com/ajax/
回答by Dean Martin
You could also use ASP code from within the actual HTML code as following.
您还可以在实际 HTML 代码中使用 ASP 代码,如下所示。
<a id="someclientid" name="someuniqueid" href="javascript:void(0);" onclick="<% YourASPMethod(); %>">val</a>
This would execute a method called YourASPMethod() in the aspx.cs file.
这将执行 aspx.cs 文件中名为 YourASPMethod() 的方法。

