How to get a control on the previous page when it is a content page?

G

George Wei

There are 2 pages Default.aspx and Result.aspx:

<!-- Default.aspx -->
<%@ Page Language="C#" AutoEventWireup="true"
MasterPageFile="~/Default.master" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<asp:Content ID="Content1" runat="server"
ContentPlaceHolderID="ContentPlaceHolder1">
&nbsp;<asp:Label ID="Label1" runat="server" Text="Please input a
string here"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
PostBackUrl="~/Result.aspx" /></asp:Content>

//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)
{
}
}

<!-- Result.aspx -->
<%@ Page Language="C#" MasterPageFile="~/Default.master"
AutoEventWireup="true" CodeFile="Result.aspx.cs" Inherits="Result"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Label ID="Label1" runat="server" Text="The string you input in
the previous page is"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Content>

//Result.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox tb = (TextBox)PreviousPage.FindControl("TextBox1");
if (tb != null)
TextBox1.Text = tb.Text;
}
}
}

Both of these 2 pages have the property MasterPageFile set, but the
contents of Default.master is not important. There are 2 controls on
Default.aspx, a TextBox named TextBox1 to accept the input, a button
named Button1 and its PostBackUrl is set to Result.aspx. While
Result.aspx is loading, it tries to get the string the user inputted in
the previous page and shows it in TextBox1.

The problem is after executing the following statement, tb is set to
null:

TextBox tb = (TextBox)PreviousPage.FindControl("TextBox1");

I tried to replace this statement with the following statements:

Content con = (Content)PreviousPage.FindControl("Content1");
if (con == null)
return;

TextBox tb = (TextBox)con.FindControl("TextBox1");

But con is null after executing, so the next statements are ignored.

How can I get TextBox1 on Default.aspx while Result.aspx is loading?
Thanks for any clues.

Regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top