Problems accessing site master textbox value in another aspx form
I trying to access a textbox value from a aspx page from my site master but it doesn't seem to work and I get a "System.NullReferenceException: Object reference not set to an instance of an object." error. Appreciate any help given. Thanks!
In my site master code behind I am using the get accessor: Thing is if I hardcode my value for my get accessor return value, I will have no problem
public partial class SiteMaster : System.Web.UI.MasterPage
{
public string Text
{
get
{
return TextBox1.Text
}
}
}
The aspx page which is trying to get the value from s开发者_如何学Goite master:
public partial class ProductSearch : System.Web.UI.Page
{
SiteMaster sm = new SiteMaster();
CommerceEntities db = new CommerceEntities();
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (sm.getSearch() != null)
{
search(sm.getSearch());
}
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
You should not create a new instance of SiteMaster()
So remove the line SiteMaster sm = new SiteMaster();
from your ProductSearch
class
Try this in your Page_Load
SiteMaster sm = Page.Master as SiteMaster;
if(sm!=null)
{
if (sm.getSearch() != null)
{
search(sm.getSearch());
}
}
SiteMaster MasterPage = (SiteMaster)Page.Master;
That's how you will have to access the masterpage given that you have properly set up the masterpage.
精彩评论