Selasa, 13 Agustus 2013

How to use Cross-Page Posting in ASP.NET

Postback 
Postback is the process of sending the data back to the server for processing. This is done to authenticate the login and password of a client or other such tasks that a client can not perform on its own. ASP.NET provides a rich framework for handling postbacks from ASP.NET Web pages .Postback includes cross-page posting, which is the name given to the process of receiving the response of a request by the server on another page.

Cross-Page Posting
When we post back data to the server, the server sends the response back to the same page. If we want to receive the response on another page, we use the concept of cross-page posting

Example of Cross-page posting


Step-1 : Take a web form in your project name as "Default3.aspx"

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>  
        Enter Name :
        <asp:TextBox ID="TextBox1" runat="server" Height="19px" Width="165px"></asp:TextBox>
        <br />
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Same page poat back" />
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" PostBackUrl="~/Default4.aspx" Text="Cross Page Post back" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
 
    </div>
    </form>
</body>
</html>

CodeBehind page of same page postback "Default3.aspx.cs"


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "hello" + " here is the output of the same page post back button" + Calendar1.SelectedDate.ToString();

    }
}
Step-2 : Take another Webform in your project name as "Default4.aspx"


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

CodeBehind page of Default4.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Calendar cal = new Calendar();
        TextBox txt = new TextBox();
        cal = (Calendar)PreviousPage.FindControl("Calendar1");
        txt = (TextBox)PreviousPage.FindControl("TextBox1");
        Label1.Text = txt.Text + "here is the output of the cross page post back" + cal.SelectedDate.ToString();

    }
}



How to use Cross-Page Posting in ASP.NET
Different page post backing output




Tidak ada komentar:

Posting Komentar