.NET Daily

Access public methods from code-behind with jQuery

Introduction

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.


LATEST POSTS

How to Deliver Accurate Project Status Reports 21st April, 2018

JIRA Workflow for Web Development Example: Simple and Effective 17th April, 2018

ASP.net

Access public methods from code-behind with jQuery

Posted on .

Have you ever wanted to check the availability for an email in real-time on client-side? Or check the strength of a password and display the status next to the textbox?
You can achieve the above scenarios very easy with a public method and some jQuery. I will describe the scenario when you will have to check the strength of a password:
Step1. Define a public static method that has the functionality for your action. Ex:

public static string VerifyPasswordStrength(string password)
{
    string result = "Weak";
 
    if (password.Length > 8 && password.Length < 10)
    {
        result = "Medium";
    }
    else if (password.Length >= 10)
    {
        result = "Strong";
    }
 
    return result;
}

Step2. Decorate the public method with [WebMethod] attribute;

Step3. Add Javascript function that calls the public method from code behind. Ex:


The markup in the page will look like this:

Verify the strength of your password!

 
Download a demo of the application:[download id=”6″ format=”1″]

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

Comments
  • user

    AUTHOR Kamran

    Posted on 1:13 pm February 29, 2012.
    Reply

    how about passing 2 value to code behind ?

  • user

    AUTHOR darius

    Posted on 4:17 pm March 1, 2012.
    Reply

    Hi Kamran,

    You simply pass the second parameter to the data field. Ex:

    data: “{‘password’:'” + password +”‘,’username’:”+ username+ “‘}”

    I’m not sure I have written correctly the code but I think you get the picture.

  • View Comments (0) ...
    Navigation

    Privacy Preference Center