Wednesday, October 20, 2010

Ajax Call in ASP.NET

Ajax Call in ASP.NET :

what is Ajax:

             Ajax is shorthand for Asynchronous JavaScript and XML is a group of interrelated web development techniques used on the client-side to create interactive web applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page

Example :

              In  design page we have to add the following things

<asp:TextBox ID="txtname" runat="server" Enabled="False"
                    Width="171px" SkinID="txt"></asp:TextBox>

<asp:Button ID="button1" Text="Click" runat="server" onclick="welcome()"  />
                         
 then you have to add the following script inside the script portion

<script type="text/javascript">

function welcome_Result(ResultString) {
         
                alert(ResultString);
                       }


        function welcome() {

               var str = getElementById('<%= txtname.ClientID %>');

            PageMethods.WelcomeMessage(str.value, welcome_Result);


        }

</script>

 Inside the Codebhind page we have to add the WebMethod for that we have to add the following namespace

 using System.Web.Services;

In side the web method we have to add a method . From Client side we call this method only .This method should be a static method. Then define the proper retun type .

[WebMethod]
    public static string WelcomeMessage(string UserName)
    {

        return "Hai Welcome"+UserName ;
      

    }

When you Click the the button it will show one alert message with the text whatever you enter in the Textbox.