This is a migrated thread and some comments may be shown as answers.

Passing Variable from web method (aspx) to javascript

4 Answers 552 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Kal Pag
Top achievements
Rank 1
Kal Pag asked on 29 Jan 2010, 09:36 PM
Hi,
I am trying to execute a code behind web method in javascript using PageMethods. The problem I am facing is as below:
<WebMethod()> _ 
        Public Shared Function Insert(ByVal charge As StringByVal clientid As StringAs String 
        Dim value as string 
        'Do The insert and retun value 
        Return value 
End Function 
 
 

Below is the javascript code and the insert method gets executed based on a confirm window:

 
 
function DoSomething1(sender, eventargs){  
    var txtRa = document.getElementById(sender.get_attributes().getAttribute("AssID"));  
    txtRa.value="";  
    var charge = sender.get_text();  
    if (trim(charge)!=""){  
charge = charge.replace("'""\'");  
        var message = '<%= Resources.NoExist%>'  
        if(confirm(message)) {  
            var clientid = sender.get_attributes().getAttribute("ClientID");  
            var ret = PageMethods.Insert(charge,index);  
            alert(ret);  
               DoSomething2(ret);  
              
       } 
 

The javascript code doesnt wait for the function to return a value and hence is executing the alert even before the insert function is complete. So, I end up getting an 'undefined' value for ret parameter.

Is there any way we can synchronously wait for the method to complete, get the return variable, set it in a hidden variable and use it in the rest of the javascript.
the webmethod does not have access to the UI controls on the page, as it is a shared web method.
I am open to moving this webmethod to a web service but I am not sure how the javascript would wait for the codebehind function to complete.
I have tried setTimeOut method in javascript to see if that will cause the wait but that dint work. The alert got fired with 'undefined'.
The webmethod does not take a long time to execute either. It only takes a few seconds. I just cant figure out how I can wait on the client side for the return parameter.

Please help me with any ideas and I am pretty much open to trying out anything at this point.

thanks in advance,
Kal.

4 Answers, 1 is accepted

Sort by
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 30 Jan 2010, 01:54 AM
If you're going the webservice route, what about the RadXmlHttpPanel?...it fires an event once the callback is completed
0
Kal Pag
Top achievements
Rank 1
answered on 01 Feb 2010, 05:59 PM
Thanks Steve for the response. I will look into that.

In the mean while, I was able to find a temporary solution using Microsoft.XMLHTTP from the link below:
http://www.codeproject.com/KB/ajax/SynchronousAJAX.aspx

I am not sure how safe it is to use the public web method in this way within javascript. This seems to me like a major security issue. I am assuming that the validation/authentication needs to be done in the webmethod. I would like to know if there are any other ways of authenticating the request.

Thanks,
Kal.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 01 Feb 2010, 06:03 PM
You could also just use jQuery

Here's something I did recently
if (!visible) { //Animate if not visible 
        $telerik.$('.feedbackpanel_outer').hide();  //Hide other panels 
        $telerik.$(panelID).slideToggle(200, function() { 
            $telerik.$(panelID + " .feedbackpanel_inner").fadeIn(200, function() { 
               $telerik.$(panelID + " .feedbackpanel_inner textarea").focus(); //Set focus to the textbox 
            }); 
        }); 
    } 

So it runs slideToggle for 200ms, then once completed, runs the next nested function...etc etc

The nested ones don't run until the parent completes


0
Eliza Sahoo
Top achievements
Rank 1
answered on 22 Mar 2010, 02:56 PM
In the above example, I have a web method called “HelloUser(string) defined in “WebserviceTest” web service. It takes a string variable as parameter named as name. So when I want to invoke the web method for testing then I have to provide the parameter value in the text box as shown above.

    But with a little modification to the WebConfig file we can also pass the parameter in the url itself. The process is described as follow:

  • We have to add the following line of code inside the <system.web> tag of the WebConfig file.

    <webServices>

          <protocols>

            <add name="HttpGet"/>

         </protocols>

    </webServices>

Then we can pass the parameter value by appending it to the url.

[Hope this will help you with your web method testing.]

Tags
Ajax
Asked by
Kal Pag
Top achievements
Rank 1
Answers by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Kal Pag
Top achievements
Rank 1
Eliza Sahoo
Top achievements
Rank 1
Share this question
or