I'm trying to find a text box that is within a form on the previous page. I've used the following code and it keeps returnning null, when trying to find the formview. I've looked through debugger and i can see Formview1 under the previous page under base.
Likely it is because the text box control is set to protected. Either set the textbox control to public or the better approach is to add a property to the previous page. Something like below. Note I would not name like I did a page PreviousPage. It is for
example only.
The in the code behind for this page you have something like this.
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;
namespace ASPNETForumResponses
{
public partial class PreviousPage : System.Web.UI.Page
{
public string BusinessUnit
{
get
{
return txt_BusinessUnit.Text;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
The on the receiving page in the code behind you have something like this.
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;
namespace ASPNETForumResponses
{
public partial class ReceivingPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
string myValue = ((ASPNETForumResponses.PreviousPage)PreviousPage).BusinessUnit;
Response.Write(myValue);
}
}
}
}
I think your code is right. But I wonder whether you are using MasterPage?
If you are using MasterPage, the code won't work.
Jessica
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
I have a problem using MasterPage. Why did you say that? I'd like to know which is the problem. I use correctly the "PreviousPage " code, but I always return a "PreviousPage" null.
This is the code:
In the PreviousPage, I have the following:
public ControlCollection ControlsPanel
{
get
{
return QuestionsPanel.Controls;
}
}
"QuestionsPanel" is a "Panel" control in the PreviousPage
In the target page I have this code:
private ControlCollection controls;
if (PreviousPage != null )
{
controls = PreviousPage.ControlsPanel;
}
The PreviousPage use a MasterPage, and when I use the "PreviousPage.ControlsPanel" sentence, returns a PreviousPage null.
I have one Master Page and two ASPX pages. Both aspx pages use Master Page. First aspx page has a text box and a button. Second aspx page has a label and PostBackURL returns null.
asp_newbie_m...
0 Points
16 Posts
PreviousPage.FindControl not working - Returning Null
Apr 19, 2007 09:16 PM|LINK
I'm trying to find a text box that is within a form on the previous page. I've used the following code and it keeps returnning null, when trying to find the formview. I've looked through debugger and i can see Formview1 under the previous page under base.
Please help!!!
if (PreviousPage != null){
FormView fv = new FormView();fv = (
FormView) Page.PreviousPage.FindControl("FormView1"); TextBox searchstring = (TextBox) fv.FindControl("txt_BusinessUnit");txtSearch.Text = searchstring.Text;
}
jdaresta
Member
222 Points
72 Posts
Re: PreviousPage.FindControl not working - Returning Null
Apr 19, 2007 11:18 PM|LINK
Likely it is because the text box control is set to protected. Either set the textbox control to public or the better approach is to add a property to the previous page. Something like below. Note I would not name like I did a page PreviousPage. It is for example only.
Jessica Cao ...
All-Star
25328 Points
2567 Posts
Re: PreviousPage.FindControl not working - Returning Null
Apr 23, 2007 01:46 AM|LINK
Hi,
I think your code is right. But I wonder whether you are using MasterPage?
If you are using MasterPage, the code won't work.
Jessica
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
colapsomine
Member
2 Points
4 Posts
Re: PreviousPage.FindControl not working - Returning Null
Apr 26, 2007 05:04 PM|LINK
Hi!
I have a problem using MasterPage. Why did you say that? I'd like to know which is the problem. I use correctly the "PreviousPage " code, but I always return a "PreviousPage" null.
This is the code:
In the PreviousPage, I have the following:
public ControlCollection ControlsPanel
{
get
{
return QuestionsPanel.Controls;
}
}
"QuestionsPanel" is a "Panel" control in the PreviousPage
In the target page I have this code:
private ControlCollection controls;
if (PreviousPage != null )
{
controls = PreviousPage.ControlsPanel;
}
The PreviousPage use a MasterPage, and when I use the "PreviousPage.ControlsPanel" sentence, returns a PreviousPage null.
Can anyone help me?
Thanks!
surajkapadia
Member
2 Points
1 Post
Re: PreviousPage.FindControl not working - Returning Null
Mar 31, 2009 12:56 PM|LINK
Jessica,
I have one Master Page and two ASPX pages. Both aspx pages use Master Page. First aspx page has a text box and a button. Second aspx page has a label and PostBackURL returns null.
I found a solution at http://aspnet101.com/aspnet101/tutorials.aspx?id=66 and thought to post it for others.
Thanks,
Suraj.
duketexan
Member
2 Points
1 Post
Re: PreviousPage.FindControl not working - Returning Null
Jul 24, 2009 09:28 PM|LINK
Thanks for posting this link-- I was having the same problem with master pages and the write-up provided a great workaround!