1

I wanted to know if you can call server side method using JavaScript or jQuery whilst doing a postback?

I have created a anchor button and want it to work same way as a linkbutton i.e. do postback and call a event. So i thought it could be possible of using JavaScript?

How would i do this? I have done alot of searches on net but could not find anything..

<a href="javascript:void(0);" id="SOMEID" onclick="DOPOSTBACK CALL SERVER SIDE METHOD();"></a>;
2

4 Answers 4

2

Have you tried searching for ajax?

http://api.jquery.com/category/ajax/

There's lots of tutorials online. If you're using dot net then there's also a JQuery plugin called dot net ajax which I find really useful. However ajax is a huge topic and your question is really broad so it's hard to be more specific!

Sign up to request clarification or add additional context in comments.

Comments

2

What you could do is send an AJAX call to the server. But it's a really big topic, you can find a lot of information on the internet. The simplest form with jQuery is

<a href="javascript:void(0);" id="SOMEID" >Call the server</a>;

$('#SOMEID').click(function(){
    $.get('url/on/server', function(data){
        //do something with data returned from server
    });
});

This makes an Asynchronous call to the url url/on/server and then waits for an answer (since the call is Asynchronous the user can keep on doing things on the page)

Comments

0

Okay you need to postback. Take a look at this article --> http://weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx

The only catch is you have to call the GetPostbackReference every time a postback happens.

Comments

0

If you want to do postback from anchor tag using javascript, you can do

document.forms[0].submit(); //This will post your form

OR

__doPostBack();

OR

If you want to know which control caused postback.

JavaScript

function __doPostBack(eventTarget, eventArgument) {
       var theform;
       if (window.navigator.appName.toLowerCase().indexOf("n etscape") > -1) {
           theform = document.forms["Form1"];
       }
       else {
           theform = document.Form1;
       }
       theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
       theform.__EVENTARGUMENT.value = eventArgument;
       alert(theform.__EVENTARGUMENT.value);
       theform.submit();
}

With following ASPX code to make it work

<input type="hidden" name="__EVENTTARGET" />
<input type="hidden" name="__EVENTARGUMENT" />

Hope this is what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.