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?
Like
<a href="#" rel="nofollow noreferrer noopener" onclick="RunServerSideMethod()">Just a simple link button</a>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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>
Method 2
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);" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" onclick="__doPostBack('someuniqueid', '');">val</a>
In your custom button, add the IPostBackEventHandler, and this __doPostBack statement will fire its RaisePostBackEvent method automatically for you.
Method 3
Just add on anchor tag –> runat=”server” onServerClick=”Your function name”, it solves your problem.
Method 4
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);" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" >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();
}
Method 5
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/
Method 6
You could also use ASP code from within the actual HTML code as following.
<a id="someclientid" name="someuniqueid" href="javascript:void(0);" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" onclick="<% YourASPMethod(); %>">val</a>
This would execute a method called YourASPMethod() in the aspx.cs file.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0