• How to bind DropDownList using WebMethod and jQuery AJAX in ASP.NET

    by  • April 28, 2014 • jQuery and Microsoft .NET, Microsoft .NET AJAX, Microsoft .NET C# (C-Sharp), Microsoft ASP.NET • 0 Comments

    WebMethods in code behind files are used for AJAX calls. They are server side functions that can be invoked from aspx file using AJAX functionality. If you want to call a method with AJAX functionality using jQuery then you need to create a method in your code behind file with WebMethod attribute. You must have to make this method public in order to call it using jQuery AJAX. WebMethods are used in Web Services to expose the data to other applications and websites. I have explained earlier in detail about web service and how to consume web service using jQuery and AJAX.

    AJAX and jQuery are common now for better user experience with asynchronous requests. WebMethod with jQuery AJAX is a good option to bind your data with DropDownList or other controls. Previously, I have also written few articles about the use of jQuery is ASP.NET. You can see these articles on GetCodeSnippet.com. In this article, I am bind data to DropDownList from a WebMethod using jQuery AJAX call. Now let’s see the code. You need to download jQuery and add it to your website. I have added it in Scripts folder in my website.

    Create a web site in Visual Studio and add a file into it

    Now add following tag for jQuery reference.

    Add a DropDownList control

    Now add following namespaces

    Write following WebMethod to get data

    I have simply created an ArrayList and returned it. You can get data from database or any data source in this method. You can see that I have added [WebMethod] just before the method and I have also declared this method as static because we have to do both these things to call this method using jQuery AJAX.

    Add following code in between head tag of aspx page

    Now see it in your browser. You can also download complete code sample from below link.




    Namecheap.com
    Avatar of GetCodeSnippet.com

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply