11

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="#" onclick="RunServerSideMethod()">Just a simple link button</a>
Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75

6 Answers6

14

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>
Oded
  • 489,969
  • 99
  • 883
  • 1,009
10

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.

Juan
  • 4,910
  • 3
  • 37
  • 46
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Thats good, but from where i can define the name of the method to run? or this should be in `IPostBackEventHandler` implementation? – Ahmed Magdy Mar 20 '10 at 21:28
  • You manually call the method you want to, or use reflection to dynamically call it. You have the full control over the implementation to do what you want to do. – Brian Mains Mar 21 '10 at 03:50
5

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

Community
  • 1
  • 1
Nitish
  • 59
  • 1
  • 1
1

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();   

}
Irf
  • 4,285
  • 3
  • 36
  • 49
Vardhini
  • 353
  • 3
  • 9
0

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.

Dean Martin
  • 1,223
  • 3
  • 18
  • 27
0

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/

Justin Emery
  • 1,644
  • 18
  • 25