Posts

Showing posts with the label Jquery/Javascript

Pass C# object to webmthod through ajax

Image
Ajax is frequently used in ASP.NET to pass data from javascript to c# methods known as WebMethod in webforms. It is easy to pass common data like string or int values to webmethod through ajax. Sometimes we need to pass whole object of some C# class thorugh ajax    to avoid sending and receiving large number of parameters. Lets take an example where we will send UserInfo object from ajax to webmethod. This is our class whose object will be sent through ajax public class UserInfo {     public int UserId { get ; set ; }     public string UserName { get ; set ; }     public string Address { get ; set ; } } And here is webmthod in which we will receive the UserInfo object [System.Web.Services. WebMethod ] public static void GetUserInfo( UserInfo User) {    //Your Code } And Here is our ajax method var objUser = new Object();       ...

Check if array contains a specific element

Arrays are used to store multiple data. If we need to check that if a specific element exists in array or not and than perform rest of operations. Let's  take an example. We have an array of names and we need to check a specific name if it exists in array or not. var names = [ "Arsenij" , "Vadimster" , "Charlie" ]; function checkName ( ) { if ( names . indexOf ( " Charlie" ) > - 1 ) alert ( "name exists" ); else alert ( "name doesn't exists" ); } Here "Charlie" exists in array so it will show alert("name exists"). If an item doesn't exist in array indexOf will return -1 otherwise it will return the index of item in our case it will return 2; Index of array starts from 0;

Calling Page Method

Today I will tell you how to call a page method using ajax request. AjaxRequest.aspx < script type ="text/javascript" src ="Scripts/jquery-1.7.2.js"></ script > < script type ="text/javascript">    $(document).ready( function ()    {        $( "#btnGetData" ).click( function ()     {          $.ajax({       type: "POST" ,       url: "AjaxRequest.aspx/GetData" ,       data: "{}" ,       contentType: "application/json; charset=utf-8" ,       dataType: "json" ,       success: function (msg) {       $( "#GetData" ).append(msg.d);       }     });   });   });     </ script > < div id ="Data">     To get data usin...

How to use BlockUI

Image
Here I will tell you how can you block user interface.Sometimes you need to block user activity with page until a certain process is completed. Jquery provides us a very easy way to do that.All you need to do is download jquery library and BlockUI plugin file.Here is an example how to block user activity . < script src ="Scripts/jquery-1.7.2.js" type ="text/javascript"></ script >     < script src ="Scripts/BlockUI.js" type ="text/javascript"></ script >        < script type ="text/javascript">    $(document).ready( function () {     $( '#btnBlock' ).click( function () {         $.blockUI({         message: $( '#Block_div' ),         css:         {         padding: 0,         mar...