Wednesday, May 8, 2013

Sample code for ajax update panel in asp.net

Here we call ajax in asp.net using script manager update panel. by using update panel we can update particular portion of page.
Sample code as below :
Default.aspx:
<%@ 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" />
        <br />
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label" Width="249px"></asp:Label><br />
            <br />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    &nbsp;
            <hr />       
            <asp:Label ID="Label2" runat="server" Text="Label" Width="249px"></asp:Label>
            <hr />
            </ContentTemplate>
            <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            <asp:PostBackTrigger ControlID="Button2"/>
            </Triggers>
            </asp:UpdatePanel>
            <br />
            <asp:Label ID="Label3" runat="server" Text="Label" Width="249px"></asp:Label><br />
            <br />
            &nbsp;<asp:Button ID="Button1" runat="server" Text="Refresh single label only" Width="233px" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Refresh all" Width="233px" OnClick="Button2_Click" />
            </div>
    </form>
</body>
</html>

Default.aspx.cs

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)
    {
        Label1.Text = DateTime.Now.ToString();
        Label2.Text = DateTime.Now.ToString();
        Label3.Text = DateTime.Now.ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    }
}

Ouptut :

Source code download : Download Source code

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website