views:

366

answers:

3

Hello, 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>
+2  A: 

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
A: 

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/

polarisation
+3  A: 

Hey,

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.

Brian
Thats good, but from where i can define the name of the method to run? or this should be in `IPostBackEventHandler` implementation?
Pr0fess0rX
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
thanks Brain :)
Pr0fess0rX