Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6339151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:35:31+00:00 2026-05-24T19:35:31+00:00

Does anyone know how to call a server-side c# method using javascript? What i

  • 0

Does anyone know how to call a server-side c# method using javascript? What i need to do is to stop imports if Cancel is chosen or to continue importing if ok is chosen. I am using visual studio 2010 and c# as my programming lanaguage

This is my code:

private void AlertWithConfirmation()            
{                 
    Response.Write(
        "<script type=\"text/javascript\">" +     
            "if (window.confirm('Import is currently in progress. Do you want to continue with importation? If yes choose OK, If no choose CANCEL')) {" +     
                "window.alert('Imports have been cancelled!');" +     
            "} else {" +   
                "window.alert('Imports are still in progress');" +     
            "}" +      
        "</script>");   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:35:33+00:00Added an answer on May 24, 2026 at 7:35 pm

    PageMethod an easier and faster approach for Asp.Net AJAX
    We can easily improve user experience and performance of web applications by unleashing the power of AJAX. One of the best things which I like in AJAX is PageMethod.

    PageMethod is a way through which we can expose server side page’s method in java script. This brings so many opportunities we can perform lots of operations without using slow and annoying post backs.

    In this post I am showing the basic use of ScriptManager and PageMethod. In this example I am creating a User Registration form, in which user can register against his email address and password. Here is the markup of the page which I am going to develop:

    <body>
        <form id="form1" runat="server">
        <div>
            <fieldset style="width: 200px;">
                <asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"></asp:Label>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
                <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
            </fieldset>
            <div>
            </div>
            <asp:Button ID="btnCreateAccount" runat="server" Text="Signup"  />
        </div>
        </form>
    </body>
    </html>
    

    To setup page method, first you have to drag a script manager on your page.

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    

    Also notice that I have changed EnablePageMethods="true".
    This will tell ScriptManager that I am going to call PageMethods from client side.

    Now next step is to create a Server Side function.
    Here is the function which I created, this function validates user’s input:

    [WebMethod]
    public static string RegisterUser(string email, string password)
    {
        string result = "Congratulations!!! your account has been created.";
        if (email.Length == 0)//Zero length check
        {
            result = "Email Address cannot be blank";
        }
        else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
        {
            result = "Not a valid email address";
        }
        else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
        {
            result = "Not a valid email address";
        }
    
        else if (password.Length == 0)
        {
            result = "Password cannot be blank";
        }
        else if (password.Length < 5)
        {
            result = "Password cannot be less than 5 chars";
        }
    
        return result;
    }
    

    To tell script manager that this method is accessible through javascript we need to ensure two things:
    First: This method should be ‘public static’.
    Second: There should be a [WebMethod] tag above method as written in above code.

    Now I have created server side function which creates account. Now we have to call it from client side. Here is how we can call that function from client side:

    <script type="text/javascript">
        function Signup() {
            var email = document.getElementById('<%=txtEmail.ClientID %>').value;
            var password = document.getElementById('<%=txtPassword.ClientID %>').value;
    
            PageMethods.RegisterUser(email, password, onSucess, onError);
    
            function onSucess(result) {
                alert(result);
            }
    
            function onError(result) {
                alert('Cannot process your request at the moment, please try later.');
            }
        }
    </script>
    

    To call my server side method Register user, ScriptManager generates a proxy function which is available in PageMethods.
    My server side function has two paramaters i.e. email and password, after that parameters we have to give two more function names which will be run if method is successfully executed (first parameter i.e. onSucess) or method is failed (second parameter i.e. result).

    Now every thing seems ready, and now I have added OnClientClick="Signup();return false;" on my Signup button. So here complete code of my aspx page :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            </asp:ScriptManager>
            <fieldset style="width: 200px;">
                <asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"></asp:Label>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
                <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
            </fieldset>
            <div>
            </div>
            <asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="Signup();return false;" />
        </div>
        </form>
    </body>
    </html>
    
    <script type="text/javascript">
        function Signup() {
            var email = document.getElementById('<%=txtEmail.ClientID %>').value;
            var password = document.getElementById('<%=txtPassword.ClientID %>').value;
    
            PageMethods.RegisterUser(email, password, onSucess, onError);
    
            function onSucess(result) {
                alert(result);
            }
    
            function onError(result) {
                alert('Cannot process your request at the moment, please try later.');
            }
        }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does anyone know a way to call a generic method of a base class
Does anyone know why I get undefined method `my_method' for #<MyController:0x1043a7410> when I call
Does anyone know of a URL to get your Skype status using JSONP? I've
Does anyone know if there exist a dll in windows (2003 server) which I
Does anyone know how I can determine the server and share name of a
Does anyone know how can I default/focus a button by using jquery? If I
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone know how to transform a enum value to a human readable value?
Does anyone know of a good Command Prompt replacement? I've tried bash/Cygwin, but that
Does anyone know of a FOSS Python lib for generating Identicons ? I've looked,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.