Is it possible to access an element on a Master page from the page loaded within the ContentPlaceHolder for the master?
I have a ListView that lists people's names in a navigation area on the Master page. I would like to update the ListView after a person has been added to the table that the ListView is data bound to. The ListView currently does not update it's values until the cache is reloaded. We have found that just re-running the ListView.DataBind() will update a listview's contents. We have not been able to run the ListView.DataBind() on a page that uses the Master page.
Below is a sample of what I wanted to do but a compiler error says
"PeopleListView does not exist in the current context"
GIS.master - Where ListView resides
...<asp:ListView ID="PeopleListView"...
GISInput_People.aspx - Uses GIS.master as it's master page
GISInput_People.aspx.cs
AddNewPerson()
{
// Add person to table
....
// Update Person List
PeopleListView.DataBind();
...
}
What would be the best way to resolve an issue like this in C# .Net?
I believe you could do this by using this.Master.FindControl or something similar, but you probably shouldn't - it requires the content page to know too much about the structure of the master page.
I would suggest another method, such as firing an event in the content area that the master could listen for and re-bind when fired.
Assuming the control is called "PeopleListView" on the master page
ListView peopleListView = (ListView)this.Master.FindControl("PeopleListView");
peopleListView.DataSource = [whatever];
peopleListView.DataBind();
But #palmsey is more correct, especially if your page could have the possibility of more than one master page. Decouple them and use an event.
Option 1 :you can create public property of your master page control
public TextBox PropMasterTextBox1
{
get { return txtMasterBox1; }
set { txtMasterBox1 = value; }
}
access it on content page like
Master.PropMasterTextBox1.Text="SomeString";
Option 2:
on Master page:
public string SetMasterTextBox1Text
{
get { return txtMasterBox1.Text; }
set { txtMasterBox1.Text = value; }
}
on Content Page:
Master.SetMasterTextBox1Text="someText";
option 3 :
you can create some public method that works for you
these approach is not so useful but it helps if you just want to use some limited and predefined control
One think to remember is the following ASP.NET directive.
<%# MasterType attribute="value" [attribute="value"...] %>
MSDN Reference
It will help you when referencing this.Master by creating a strongly typed reference to the master page. You can then reference your ListView without needing to CAST.
you can access with the code this.Master.FindControl(ControlID) which control you wish. It returns the reference of the control, so that the changes are effective. about firing an event could not be possible each situation.
Assuming your master page was named MyMaster:
(Master as MyMaster).PeopleListView.DataBind();
Edit: since PeopleListView will be declared protected by default, you will either need to change this to public, or create a public property wrapper so that you can access it from your page.
Related
A little new to asp.net.
In my main.aspx page i have:
<users:UsersControl runat="server" ID="usersControl" />
In my UserControl page_load i have:
ViewState["test"] = "test";
In my Page_PreRender in main.aspx.cs:
log...(ViewState["test"]); <-- empty
Why dont i see the value on test?
Im guessing here that the ViewState collection is different in the two contexts you have mentioned.
The first is in the context of the control, and the second is in the context of the page, therefore "test" key is not shared between them.
Also, it is not a good idea to expose a controls ViewState beyond the control boundary. For example use properties on the UserControl as the interface to the viewstate, e.g
public string Test
{
get { return this.ViewState["Test"]; }
set { this.ViewState["Test"] = value; }
}
ViewState should be considered an internal implementation detail of the user control.
Then whenever you need to use this property from the page:
this.userControl1.Test = "This Goes Into ViewState";
I've found a similar answer to your question:
.net ViewState in page lifecycle
It's necessary to understand the life cycle, so why don't use Attributes on the UserControl?
I have a master page.
I'm working on a testpage which inherits the master page.
the master page has a public property which is accessible to turn visibility on and off.
I have a set of controls which I can include into testpage. One of these controls needs to be able to set the visibility of a masterpage control.
Normally in a page code behind I would just say;
this.Master.ShowItem = false;
I have no idea how to be able to access this property from the custom control?
You just have to cast the master to the correct type. Assuming the type of your master is SiteMaster:
var master = this.Page.Master as SiteMaster;
if(master != null) // cast failed, your master is a different type
{
master.ShowItem = false;
}
So the navigation is:
UserControl.Page
Page.Master
type cast
(sry due to my reputation I cant make comments, so as an answer)
Hint: Make sure that the Property you want to access in the master page is set to public.
Heeding this the solution of Tim Schmelter works fine.
I don't believe you can set a property of master page directly.
But you can find the control of the master page and made them visible/invisible from conternt page like this:
((Label)this.Page.Master.FindControl("IdOfTYurControl")).Visible = false;
I have the following case:
Page1.aspx this page has the master page master.aspx.
I have some code in the master page :
if (!Page.IsPostBack)
{
adjustServiceBar();
}
when i click any button in the Page1.aspxit enters the !Page.IsPostBack and execute the method !!
i want this method in the !Page.IsPostBack) only
One way to do this is to set a session variable and then check that variable to ensure your code will fire only once.
Another way is to set a hidden control on your form and play with its text or value property.
According to each scenario the solution may be very complex such as custom derived masterpages and pages that extend the current events functionality to suit your needs.
I believe it is more consistent to check for IsPostBack in content page. You can move this condition to Page1.aspx and expose adjustServiceBar() method in your master page, so that content pages can call it, like Master.adjustServiceBar().
I'd like someone to give what we're doing a glance-over and tell me whether we're going about it an odd way.
What I have:
We have a .master (Master Page), .aspx (Content Page) and an .ascx (User Control).
The User Control should appear on all pages, so we have placed it on the Master Page
The User Control should be initialised differently based on the Content Page it appears on. So, the Content Page has the resposibility of raising an event on the User Control, passing in some arguments, which initialises the control.
Questions:
In my mind, this removes any dependency between the User Control and either the Content Page or Master Page. Am I correct?
Is calling an event on the User Control the easiest way to pass arguments in and initialise the control accordingly?
Happy to clarify any further points, Thanks.
I would say that's OK, but I would like to seek a clarification: how does the user control subscribe to the event of the content page?
Alternatively, you could also consider creating an interface for the master page:
public interface IMyMaster
{
UserControlType Control { get; }
}
After applying this interface to the master, the interface can return a direct reference to the control. The content page can then do:
if (this.Page.Master is IMyMaster) {
((IMyMaster)this.Page.Master).Control.Initialize();
}
HTH.
When I asked recently a question about how To retrieve a control in a Master Page from Content Page.
Many peoples suggest me to use this code from my Content Page:
Label lbl = this.Master.Page.FindControl("uxLabel") as Label;
//Note any server controls defined in the master page could be not be accessible even after a cast is performed, because they could be marked as protected
This approach certainly works, I also realize that is available a strongly-typed solution that doesn't involve casting the Master property.
In the Master Page place:
public Label HeaderLabel
{
get { return uxLabel; }
}
Using a MasterType in the Content Page:
<%# MasterType VirtualPath="~/Templates/WebsiteMasterPage.master" %>
Now it is pretty easy find the control from the Content Page:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.HeaderLabel.Text = "Any Text here!";
}
I would like to know:
what do you think about this
approach?
any other solution?
Thanks for your time
My answer is "why not?".
Both are for me good approaches but first needs less coding in order to get started with it, since you don't need to initialize any class field and design properties. But control must be found during run-time.
Second approach, call it "typed approach", is just cast to specific master page class and you get access to any class-specific member.
What would be the main problem of "typed approach"? You need a reference to the class library (assembly) in order to access to such master page's members, which wouldn't be desirable in some scenarios. For example, you've a control library and you want to access a mandatory master page's control which provides some behaviors needed to work with some library's control. You would need to reference your web client assembly, but you can't do that because you reference your control library in the web client itself, and this is a circular reference.
Tools, approaches are there for use in specific scenarios. Why not? answer can be expanded to why not to use "typed approach" if it's needed and your scenario is compatible with that concept?.