How to call server side function for client side JavaScript?

G

Guest

I want to call a server-side function, like a button's click event, from a
client-side JavaScript function. How do I do that? Thanks.
 
J

JV

In order to call server-side code you have to post back to the web server.
If you examine the HTML that is generated on a simple web page with a
button, you will get a pretty good idea how to do that. It's just calling a
javascript that ASP.NET automatically generates in your web page.
 
K

Karl Seguin

Lanem:
Your question is a little vague..if you just want to dynamically trigger a
postback, you can use the __doPostback function:

<asp:button id="x" runat="server" />
<a href="javascript:__doPostBack('x', '');">hahah I'm sooo clever!</a>

If you only have Buttons and no LinkButtons or other controls which require
__doPostBack, you'll have to trick the page into generating the Javascript
for you (button's don't need javascript)...you can use
Page.GetPostBackClientEvent(x, ""); where x is the id of the button
control.

the above <a tag will cause x's click event to fire in codebehind.

If you want to fire server-side code without generating a postback, you'll
need to look at XmlHTTP, here's a good link dump:
http://www.quirksmode.org/blog/archives/2005/02/xmlhttp_linkdum.html


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top