.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

My View on the Life cycle of a Fixed Cost Web Project 01st July, 2018

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

jQuery

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.

  • This site uses Akismet to reduce spam. Learn how your comment data is processed.

    View Comments (0) ...
    Navigation