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 site 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.
Related
I have a question and I can not find the right terms to do a reasoned search and solve the question.
Let's see, when I'm creating a page, at some point I need to create a WebUserControl and defer something like state = "true" (like the text of the lables) inside the html tag so that as soon as the page loads , Whether or not that control is subsequently edited in code.
<MyControls:Teste Id="aaa" runat="server" state="false"/>
The test control code is as follows: (The HTML page of this control is blank, it only has the header)
public partial class WebUserControls_WUC_Tect : System.Web.UI.UserControl
{
private static bool state ;
public bool State
{
get { return state ; }
set { state = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Problem:
Whenever the page returns to the server and is reloaded, the state variable is always set to false or true depending on the initial state I passed, what I intended was for this variable to be loaded only once at the beginning of the page and then Could only be changed by codebeind.
I am grateful for your suggestions.
greetings
Patrick Veiga
You need to use the ViewState to store the property value to keep the persistent value saved.
public partial class WebUserControls_WUC_Tect : System.Web.UI.UserControl
{
private static bool state ;
public bool State
{
get
{
if (ViewState["MyState"] == null)
{
ViewState["MyState"] = false;
}
return (bool)ViewState["MyState"];
}
set
{
ViewState["MyState"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
I want to ask you, what do you think, what is the best approach to get from second page to first page? I use something like this.(MVVM)
Second page:
public partial class AddProfilePageView : PhoneApplicationPage
{
public AddProfilePageView()
{
InitializeComponent();
DataContext = new AddProfileViewModel();
}
public AddProfileViewModel ViewModel { get { return DataContext as AddProfileViewModel; } }}
First page:
public partial class ProfilesPageView : PhoneApplicationPage
{
public ProfilesPageView()
{
InitializeComponent();
DataContext = new ProfilesViewModel();
}
public ProfilesViewModel ViewModel
{
get { return DataContext as ProfilesViewModel; }
}}
AddProfileViewModel() class has properties, that are binded to controls in xaml. From this page I need to get data to first page ProfilesPageView.
My solution is:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
var content = e.Content as ProfilesPageView;
if (content != null && ViewModel.IsOk)
{
content.ViewModel.ProfilesList.Add(ViewModel.ProfileRecord);
}
}
So what do you think? Is it good solution how obtain data?
Thanks
In reality here you're not trying to get data from one page to another.
On the second/detail page you're adding a new item and then when you return to the first/main page you want it to update the displayed data to show the new item.
I'd assume that AddProfileViewModel and ProfilesViewModel are both persisting to disk/IsolatedStorage so you could just refresh the main/list/first page when returning to it.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// ...
if (e.NavigationMode == NavigationMode.Back)
{
(this.DataContext as ProfilesViewModel).Refresh();
}
}
I am having problem in accessing user control properties from page.
I have usercontrol on master page with some properties,but i am unable to access them from the codebehind of the page which uses that master page.i want to set some properties of usercontrol on page load.Can anyone suggest how can i access them from page.
E.G.
User Control
ucTabSystem.ascx has following properties:
public string TabName
{
get { return _tabName; }
set { _tabName = value; }
}
public string TabUrl
{
get { return _tabUrl; }
set { _tabUrl = value; }
}
Master Page
InHouseTPAMaster.master has this user control in it.
ClaimHomePage.aspx
Uses Master page InHouseTPAMaster.master and i want to set usercontrol properties in page load of this page.
You can use two methods.
The first is by using Page.Master.FindControl('controlID'). Then you can cast it to the type of your user control. The second method is by adding a <%# MasterType VirtualPath="" TypeName=""%> tag to your aspx page. In the VirtualPath add the virtual path to the master page, and the class in the TypeName. You can then access everything with intellisense
You can try this way to set your properties...
<%# Register TagPrefix="Tab" TagName="sys" Src="ucTabSystem.ascx" %>
<tab:sys id="mysys" runat="server" TabName="xxxxx" TabUrl = "yyyy" />
You need to define a public interface with two properties - TabName and TabUrl in separate code file.
public interface IUserControl
{
string TabName{get;set;}
string TabUrl {get;set;}
}
Implements the IUserControl interface to UserControl class. For instance, I've MyUserControl and its code-behind is:
public partial class MyUserControl : System.Web.UI.UserControl , IUserControl
{
public string TabName
{
get { return ViewState["TabName"] == null ? string.Empty : ViewState["TabName"].ToString(); }
set { ViewState["TabName"]= value; }
}
public string TabUrl
{
get { return ViewState["TabUrl"] == null ? string.Empty : ViewState["TabUrl"].ToString(); }
set { ViewState["TabUrl"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Register MyUserControl in MasterPage and it has following markup in it.(master page)
<%# Register src="MyUserControl.ascx" tagname="MyUserControl" tagprefix="uc1" %>
......
<uc1:MyUserControl ID="MyUserControl1" runat="server" />
In Page_Load event (or any other) handler of aspx page (Which is a content page of said master page).
protected void Page_Load(object sender, EventArgs e)
{
IUserControl control = Master.FindControl("MyUserControl1") as IUserControl;
control.TabName = "Something";
control.TabUrl = "http://www.example.com";
}
This question is for an ASP.NET guru. Its driving me nuts.
I have inherited an ASP.NET Web Forms application. This application uses a complex
structure of nested user controls. While complex, it does seem necessary in this case.
Regardless, I have a page that uses a single UserControl. We will call this UserControl
root control. This UserControl is defined as follows:
widget.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="resources_userControls_widget" %>
<div>
<asp:Panel ID="bodyPanel" runat="server" />
</div>
widget.ascx.cs
public partial class resources_userControls_widget : System.Web.UI.UserControl
{
private string source = string.Empty;
public string Source
{
get { return source; }
set { source = value; }
}
private string parameter1 = string.Empty;
public string Parameter1
{
get { return parameter1; }
set { parameter1 = value; }
}
private DataTable records = new DataTable();
public DataTable Records
{
get { return records; }
set { records = value; }
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
UserControl userControl = LoadControl(source) as UserControl;
if (parameter1.Length > 0)
userControl.Attributes.Add("parameter1", parameter1);
bodyPanel.Controls.Add(userControl);
}
private void InsertUserControl(string filename)
{
}
}
In my application, I am using widget.ascx in the following way:
page.aspx
<uc:Widget ID="myWidget" runat="server" Source="/userControls/widgets/info.ascx" />
page.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = GetData();
myWidget.Records = table;
}
Please notice how info.ascx is set as the UserControl we want to load in this case. This approach is necessary in this case. I've removed the extraneous code that justifies it to focus on the problem. Regardless, in info.ascx.cs I have the following:
info.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
// Here's the problem
// this.Parent.Parent is a widget.ascx instance.
// However, I cannot access the Widget class. I want to be able to do this
// Widget widget = (Widget)(this.Parent.Parent);
// DataTable table = widget.Records;
}
I really need to get the value of the "Records" property from the Parent user control. Unfortunately, I can't seem to access the Widget class from my code-behind. Are there some rules about UserControl visibility at compile time that I'm not aware of? How do I access the Widget class from the code-behind of info.ascx.cs?
Thank you!
Firstly you need to create an interface and implement it to the Widget user control class.
For instance,
public interface IRecord
{
DataTable Records {get;set;}
}
public partial class resources_userControls_widget : System.Web.UI.UserControl, IRecord
{
...
}
And in code behind of Info.ascx.cs,
protected void Page_Load(object sender, EventArgs e)
{
// Here's the problem
// this.Parent.Parent is a widget.ascx instance.
// However, I cannot access the Widget class. I want to be able to do this
// Widget widget = (Widget)(this.Parent.Parent);
// DataTable table = widget.Records;
IRecord record=this.Parent.Parent;
DataTable table = widget.Records;
}
In your case, maybe better to use some server object's like ViewState or Session. Fill it within DataTable on your page and get it in Page_load event handler on info.ascx user control.
I've built a small User Control which is essentially a DropDownList with some preset Values based on what the Target-Property is set on.
Here's the Code:
public partial class Selector : System.Web.UI.UserControl
{
public string SelectedValue { get {return this.ddl.SelectedValue; } }
public int SelectedIndex { get { return this.ddl.SelectedIndex; } }
public ListItem SelectedItem { get { return this.ddl.SelectedItem; } }
private string target;
public string Target { get { return this.target; } set { this.target = value; } }
protected void Page_Load(object sender, EventArgs e)
{
ddl.DataSource = target=="Group"?Util.GetAllGroups(Session["sessionId"].ToString()):Util.GetAllUsers(Session["sessionId"].ToString());
ddl.DataBind();
}
}
ASP-Markup:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Selector.ascx.cs" Inherits="InspireClient.CustomControls.Selector" %>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>
If I insert my Selector into an aspx-Page it works just fine.
Example:
<SCL:Selector Target="Group" runat="server" />
However, If I programmatically add it like this
ctrl = new Selector();
ctrl.Target = "User";
the DropDownList "ddl" is null and the application (logically) throws an error. Is Page_Load the wrong Method to do such a thing? What am I doing wrong?
I should add, "ctrl" is of type dynamic, not sure if this has anything to do with it.
Thanks in advance!
Dennis
Since you're dynamically adding a user control and not a "simple" web control, you should use the LoadControl() method to instantiate it:
protected void Page_Load(object sender, EventArgs e)
{
Selector yourControl = (Selector) LoadControl("Selector.ascx");
yourControl.Target = "User";
Controls.Add(yourControl);
}