Subscribe Now!

Enter your email address:

Wednesday, June 1, 2011

Asp.net Cross-Page PostBack example & Post Back

 Post Back means sending the data to server and return the data in same page.

 Cross-Page Post Back means sending the data from one page to another page.

Example:
 CrossPagePostBack.aspx 

 <%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net cross page postback example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net Cross-Page PostBack example</h2>
        <asp:Label
            ID="Label1"
            runat="server"
            Text="ProductID"
            ForeColor="DodgerBlue"
            >
        </asp:Label>
        <asp:TextBox
            ID="TextBox1"
            runat="server"
            ForeColor="AliceBlue"
            BackColor="DodgerBlue"
            >
        </asp:TextBox>
        <br />       
        <asp:Label
            ID="Label2"
            runat="server"
            Text="Product Name"
            ForeColor="DodgerBlue"
            >
        </asp:Label>
        <asp:TextBox
            ID="TextBox2"
            runat="server"
            ForeColor="AliceBlue"
            BackColor="DodgerBlue"
            >
        </asp:TextBox>
        <br />
        <asp:Button
            ID="Button1"
            runat="server"
            Text="Submit data"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            PostBackUrl="~/NextPage.aspx"
            />
    </div>
    </form>
</body>
</html>

NextPage.aspx 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        TextBox pvProductID = (TextBox)PreviousPage.FindControl("TextBox1");
        TextBox pvProductName = (TextBox)PreviousPage.FindControl("TextBox2");
        Label1.Text ="You came from: "+ PreviousPage.Title.ToString();
        Label2.Text = "Product ID: " + pvProductID.Text.ToString();
        Label2.Text += "<br />Product Name: " + pvProductName.Text.ToString();

        string imageSource = "~/Images/" + pvProductID.Text + ".jpg";
        Image1.ImageUrl = imageSource;
        Image1.BorderWidth = 2;
        Image1.BorderColor = System.Drawing.Color.DodgerBlue;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net Cross-Page PostBack example: how to submit a page to another page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Teal">asp.net cross page postback example</h2>
        <asp:Label ID="Label1" runat="server" ForeColor="Crimson">
        </asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" ForeColor="SeaGreen">
        </asp:Label>
        <br />
        <asp:Image ID="Image1" runat="server" />
    </div>
    </form>
</body>
</html>
 

 OUTPUT:
[CrossPagePostBack1.gif] 

[CrossPagePostBack2.gif]
 

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...