Click here to Skip to main content
15,885,117 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i need a code to call a javascript function from my static webmethod.
plz help.......
thanks in advance

C#
[System.Web.Services.WebMethod]
       public static string password(string UserId, string UserName, string Password, string ConPassword, string EmailId, string Country, string date)
       {
           TextBox TxtUserId = new TextBox();
           TextBox TxtPassword = new TextBox();
           //TxtUserId.Text = "";
           //TxtPassword.Text = "";
           string flag = "";
           date = DateTime.Now.ToString("dd/MM/yyyy");
           ScriptManager MyScriptManager = new ScriptManager();
           MyScriptManager = (ScriptManager)HttpContext.Current.Session["Script"];
           Page page1 = HttpContext.Current.CurrentHandler as Page;
           TxtUserId = (TextBox)HttpContext.Current.Session["TxtUserId"];
           TxtPassword = (TextBox)HttpContext.Current.Session["TxtPassword"];
           try
           {
              if (Password.Trim().Replace(" ", "") != ConPassword.Trim().Replace(" ", ""))
               {
                   flag = "false1";
                   //Password.Text = "";
                   return flag;
               }

               else
               {
                   page1.ClientScript.RegisterStartupScript(page1.GetType(), "id", "GetUserIdMessage()", true);     //Message("Hi")  should be printed
                   MyFunctions Functions = new MyFunctions();
                   Functions.insertLogin("insert into UserLogin values('" + UserId.Trim().Replace(" ", "") + "','" + UserName.Trim().Replace(" ", "") + "','" + Password.Trim().Replace(" ", "") + "','" + EmailId.Trim().Replace(" ", "") + "','" + Country.Trim().Replace(" ", "") + "','" + date.Trim().Replace(" ", "") + "')");
                   flag = "true";
                   return flag;
               }
           }

           catch (Exception ex)
           {
            return ex.Message.ToString();
           }
       }
Posted
Updated 11-Aug-15 23:13pm
v2
Comments
F-ES Sitecore 12-Aug-15 4:32am    
You can't call javascript from your server code, you'll need to explain what you're trying to do as it might be possible some other way.
harshil gandhi 12-Aug-15 4:51am    
ya...i am posting a question in detail
F-ES Sitecore 12-Aug-15 5:14am    
So what's the problem?
harshil gandhi 12-Aug-15 5:19am    
page1.ClientScript.RegisterStartupScript(page1.GetType(), "id", "GetUserIdMessage()", true); //Message("Hi") should be printed

but it is giving error... and how to do that...i need to alert some message in between a webmethod then after that alert, again it should continue the code in webmethod....how to do that?
thank u in advance....
F-ES Sitecore 12-Aug-15 5:25am    
Web methods can't interact with the page they have been called from. Your web method should return the id\text that you want to show and the js that calls your web method will get that result and show it in an alert, or whatever. Google "call asp.net web method jquery" for examples of how you interact with web methods.

1 solution

Don't call a JavaScript function inside a WebMethod. What you can do is, just return from the method with any values if needed and then inside Ajax success, call the other JavaScript function. This is very easy.
 
Share this answer
 
Comments
harshil gandhi 12-Aug-15 5:15am    
ya.. but i need to alert some message in between a webmethod then after that alert, again it should continue the code in webmethod
I would suggest you to take that checking of passwords to client side and show message there and then do a Ajax call.
F-ES Sitecore 12-Aug-15 5:34am    
This is architecturally impossible. It would help you a lot if you gained more of an understanding of the asp.net and http frameworks and how they work. There are some examples of various ways that this can be done here, but it's not trivial and does demand a basic understanding of the web framework.
harshil gandhi 12-Aug-15 5:40am    
thank u ...@F-ES-Sitecore...
i need one help.... do u have online sites which can provide me more information of using web methods with javascript...plz help
harshil gandhi 12-Aug-15 5:35am    
thanks...@Tadit Dash...

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