How to let Html Link (anchor) do a postback to be like LinkButton?

余生长醉 提交于 2019-11-29 03:58:42
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.

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 __doPostBack statement will fire its RaisePostBackEvent method automatically for you.

Use a server side html control, HtmlAnchor which is a server side a tag.

<asp:HtmlAnchor runat="server" onclick="RunServerSideMethod">Just a simple link</asp:HtmlAnchor>

Just add on anchor tag --> runat="server" onServerClick="Your function name", it solves your problem.

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.

Code:

<a id="ancLink" href="javascript:void(0);" >back</a>

<asp:Button ID="dummyRefresh" runat="server" OnClick="BtnRefreshOnclick" style="display:none"/>

Javascript:

ancLink.live("click", function () {
         callDummyButtonServerEvent();
    });

function callDummyButtonServerEvent() {

    $('input[id$=dummyRefresh]').click();   

}

Hope this helps.

Vardhini 

To do this without relying on ASP.NET, RunServerSideMethod() should be a javascript function that uses Ajax to send a request to the server.

Try this Ajax tutorial: http://www.w3schools.com/ajax/

You could also use ASP code from within the actual HTML code as following.

<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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!