Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a webmethod like this :
(This WebMethod is called from other page's JavaScript Function)

C#
[WebMethod]
    public static string SetFileName(string FileName)
    {
        //Here I want to call JavaScript Method
    }


This is Javascript Method which I want to call from WebMethod :
(The above WebMethod & following JavaScript Function are in same page)

JavaScript
function Checking() {
           //Some Codes to display images
           }


Can you please help me???
Posted
Updated 22-Jun-16 9:16am
v3

try this.. :)

JavaScript
 function RunCode()
var FileName="anything";
 $.ajax({
        type: "POST",
        url: "Default.aspx/SetFileName",
        contentType: "application/json;charset=utf-8",
        data: "{'FileName':'" + FileName + "'}",
        dataType: "json",
        success: function (data) {

          Checking();

        },
        error: function (result) {
            //alert("Error login");

        }
    });
}

 function Checking() {
            //Some Codes to display images
            }


C#
[WebMethod]
    public static string SetFileName(string FileName)
    {
        //Here I want to call JavaScript Method
    }
 
Share this answer
 
v3
Comments
Rockstar_ 30-May-14 1:24am    
my 5!
Nirav Prabtani 30-May-14 2:00am    
thank you.. :)
Hi,

Use the below line to call a Javascript function from Code behind file.
ScriptManager.RegisterStartupScript(page, page.GetType(), key, functionNameWithBrackets + ";", true);

Also you can use with Key :
ScriptManager.RegisterStartupScript(page, page.GetType(), key, functionNameWithBrackets + ";", true);

Key value should be 'fun' for calling a function.
 
Share this answer
 
You need to read a basic book on how the internet works. A webmethod is called by AJAX. You can write javascript to run AFTER your AJAX call returns, but you can't call javascript inside your C#. That's just crazy. If you want to show images during the process of that call, break it in to several AJAX calls.
 
Share this answer
 
Comments
Peter Leow 24-Jan-14 3:57am    
Sorry, I only noticed your solution after posting.
Christian Graus 24-Jan-14 3:59am    
I don't care that you answered after me. I don't own the site. I just wonder why so many answers are SO links instead of actual answers on this site.
Ankur\m/ 24-Jan-14 4:47am    
"but you can't call javascript inside your C#"
Register your javascript using Page.ClientScript.RegisterClientScriptBlock() method.
But I see what you meant. I am countering the wrong vote.
Prateek Dalbehera 23-Jun-16 3:25am    
Basics of client server architecture needs to studied before starting coding directly.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900