Hi,
I am having problems with transferring data using cross-page. The public methods which supposed to return the value from the previous page textboxes, always returns null.
Code from the present page (CrossPage2) as follows:
public partial class CrossPage2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
lblinfo.Text = "You came from a page titled " +
PreviousPage.Title + "
";
CrossPage1 prevPage = Page.PreviousPage as CrossPage1;
if (prevPage != null)
{
lblinfo.Text += "You typed in this: " + prevPage.FullName;
}
else
{
//lblinfo.Text = "null";
}
}
}
}
Code from the previous page (CrossPage1) as follows:
public partial class CrossPage1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public TextBox FirstNameTextBox
{
get { return txtFirstName; }
}
public TextBox LastNameTextBox
{
get { return txtLastName; }
}
public String FullName
{
get { return txtFirstName.Text + " " + txtLastName.Text; }
}
}
Can someone please help me find out what's wrong here.
(I am using ASP.NET 4)
Thank you
Viki
Loading
Om prakash SinghPosted Jul 9, 2011, 3:18 AM
1.On source page,you have given the url of distinaton page on the postbackurl property of the button.
2.You have added the <%Previouspagetype%> directive in the header of distination page.
3.set directive value as <%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
4. On the distination page load event add the code
if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack) )
{
SourcePage prevPage = PreviousPage;
//code to fetch the source page value
}
here SourcePage is the partial class name defined in the source page
VikiPosted Jul 9, 2011, 1:06 AM
I am learning ASP.Net by a book and according to it, I should be able to read the public methods from the PreviousPage (CrossPage1) using the following code.
CrossPage1 prevPage = Page.PreviousPage as CrossPage1;
if (prevPage != null)
{
lblinfo.Text += "You typed in this: " + prevPage.FullName;
}
the problem is object "prevPage" always becomes null. I am trying to find out why it always become null.
have you got any suggestions.
thank you
regards
Viki
Suthish NairPosted Jul 8, 2011, 5:01 PM
Different Ways to Pass Data between Web Forms