Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 330
Ahmed Magdy Avatar asked Mar 19 '10 11:03

Ahmed Magdy


2 Answers

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.

like image 58
Brian Mains Avatar answered Sep 24 '22 12:09

Brian Mains


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

like image 22
Nitish Avatar answered Sep 23 '22 12:09

Nitish