Tuesday, February 14, 2012

Add two number in ASP.NET using AJAX

Add two number using AJAX




Step 1)Open a ASP.Net AJAX enable website in your visual Studio 

Step 2)Add below code in your ASP.net page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                First number:- &nbsp; &nbsp;&nbsp;
                <asp:TextBox ID="TextBox1" runat="server" Width="31px"></asp:TextBox>&nbsp;<br />
                Second Number:-<asp:TextBox ID="TextBox2" runat="server" Width="32px"></asp:TextBox>
                &nbsp;<br />
                <asp:Label ID="Label1" runat="server" Width="59px"></asp:Label>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" Width="40px" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <div>
        </div>
    </form>
</body>
</html>

Step 3) Add code below into code behind of your ASP.Net page
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Label1.Text = Convert.ToString(Convert.ToInt32(this.TextBox1.Text) + Convert.ToInt32(this.TextBox2.Text));
    }
}

No comments:

Post a Comment