Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access .Net element on Master page from a Content page?

Tags:

c#

.net

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?

like image 465
Eddie Avatar asked Aug 08 '08 21:08

Eddie


People also ask

How do you access master page controls from the content page?

we create the instance of master page button click event before content page raises its page_load event. To access the master page controls we need to use the Master key word then call the Property method of control. As we use Master. EnterNameTextBox() to access the master page EnterName textbox and Master.

How do you access a public property defined in a master page from the content page?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.

How do I find the Div of a master page in the content page?

add to the div in the master page the attribute: runat="server" and then in the content page write the following code to access the div: Control c= this. Master. FindControl("masterDiv");// "masterDiv"= the Id of the div.


2 Answers

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.

like image 91
palmsey Avatar answered Sep 22 '22 03:09

palmsey


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.

like image 20
Kev Avatar answered Sep 22 '22 03:09

Kev