Friday, 21 July 2023

Cross Page Postback in ASP.NET

Cross Page Postback in ASP.NET
One page data send to the another page without using session called CrossPagePostback. There is a need to transfer some data from one web page to another web page then first thing as  a developer is sessions. But the use of a session can be bad sometimes since the page becomes heavy due to it. There is one more way to do it and thereby avoid sessions or other state management techniques, we can use a cross page postback.

CrossPagePostback_1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CrossPagePostback_1.aspx.cs" Inherits="ASPNETALL.CrossPagePostback_1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1" style="background-color: #99CCFF">
            <tr>
                <td colspan="2">Enter Your Name :-
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px">
                        <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                        <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                        <OtherMonthDayStyle ForeColor="#CC9966" />
                        <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                        <SelectorStyle BackColor="#FFCC66" />
                        <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
                        <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                    </asp:Calendar>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblMsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSamePage" runat="server" BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" OnClick="btnSamePage_Click" Text="SamePagePostBack" />
                </td>
                <td>
                    <asp:Button ID="btnCrossPage" runat="server" BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" OnClick="btnCrossPage_Click" PostBackUrl="~/CrossPagePostback_2.aspx" Text="CrossPagePostBack" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

CrossPagePostback_1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace ASPNETALL
{
    public partial class CrossPagePostback_1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnSamePage_Click(object sender, EventArgs e)
        {
            lblMsg.Text = "Hi ! " + txtName.Text + " here is the selected Data of the Same Page Post Back Method Day&Date :" + Calendar1.SelectedDate.ToLongDateString();
        }
    }
}

CrossPagePostback_2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CrossPagePostback_2.aspx.cs" Inherits="ASPNETALL.CrossPagePostback_2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="background-color: #99CCFF">
        <asp:Label ID="lblMsg1" runat="server" Font-Bold="True" ForeColor="Maroon"></asp:Label>
    </div>
    </form>
</body>
</html>

CrossPagePostback_2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace ASPNETALL
{
    public partial class CrossPagePostback_2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Calendar Calendar1 = new Calendar();
            TextBox Text1 = new TextBox();
            Calendar1 = (Calendar)(PreviousPage.FindControl("Calendar1"));
            Text1 = (TextBox)(PreviousPage.FindControl("txtName"));
            lblMsg1.Text = "Hi ! " + Text1.Text + " here is the Cross Page Post Back Method Day&Date :" + Calendar1.SelectedDate.ToLongDateString();
        }
    }
}

0 comments

Post a Comment