<p>Is it possible to access an element on a Master page from the page loaded within the <code>ContentPlaceHolder</code> for the master?</p>

<p>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 <code>ListView</code> currently does not update it's values until the cache is reloaded. We have found that just re-running the <code>ListView.DataBind()</code> will update a listview's contents. We have not been able to run the <code>ListView.DataBind()</code> on a page that uses the Master page. </p>

<p>Below is a sample of what I wanted to do but a compiler error says </p>

<blockquote>
  <p>"PeopleListView does not exist in the current context"</p>
</blockquote>

<p>GIS.master - Where ListView resides</p>

<pre><code>...&lt;asp:ListView ID="PeopleListView"...
</code></pre>

<p>GISInput_People.aspx - Uses GIS.master as it's master page</p>

<p>GISInput_People.aspx.cs</p>

<pre><code>AddNewPerson()
{
    // Add person to table
    ....

    // Update Person List
    PeopleListView.DataBind();
    ...
}
</code></pre>

<p>What would be the best way to resolve an issue like this in C# .Net?</p>

[{'answer_id': 6411, 'author': 'Brandon Wood', 'author_id': 423, 'author_profile': 'https://Stackoverflow.com/users/423', 'pm_score': -1, 'selected': False, 'text': '<p>Assuming your master page was named MyMaster:</p>\n\n<pre><code>(Master as MyMaster).PeopleListView.DataBind();\n</code></pre>\n\n<p><strong>Edit:</strong> 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.</p>\n'}, {'answer_id': 6412, 'author': 'palmsey', 'author_id': 521, 'author_profile': 'https://Stackoverflow.com/users/521', 'pm_score': 5, 'selected': True, 'text': "<p>I believe you <em>could</em> 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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 6413, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 2, 'selected': False, 'text': '<p>Assuming the control is called "PeopleListView" on the master page</p>\n\n<pre><code>ListView peopleListView = (ListView)this.Master.FindControl("PeopleListView");\npeopleListView.DataSource = [whatever];\npeopleListView.DataBind();\n</code></pre>\n\n<p>But @<a href="https://stackoverflow.com/users/521/palmsey">palmsey</a> is more correct, especially if your page could have the possibility of more than one master page. Decouple them and use an event.</p>\n'}, {'answer_id': 23871, 'author': 'Adam Carr', 'author_id': 1405, 'author_profile': 'https://Stackoverflow.com/users/1405', 'pm_score': 1, 'selected': False, 'text': '<p>One think to remember is the following ASP.NET directive.</p>\n\n<pre><code>&lt;%@ MasterType attribute="value" [attribute="value"...] %&gt;\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms228274.aspx" rel="nofollow noreferrer">MSDN Reference</a></p>\n\n<p>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.</p>\n'}, {'answer_id': 5591665, 'author': 'icaptan', 'author_id': 696296, 'author_profile': 'https://Stackoverflow.com/users/696296', 'pm_score': 0, 'selected': False, 'text': '<p>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. </p>\n'}, {'answer_id': 10945605, 'author': 'BrainCoder', 'author_id': 1245631, 'author_profile': 'https://Stackoverflow.com/users/1245631', 'pm_score': 2, 'selected': False, 'text': '<p>Option 1 :you can create public property of your master page control</p>\n\n<pre><code>public TextBox PropMasterTextBox1\n{\n    get { return txtMasterBox1; }\n    set { txtMasterBox1 = value; }\n}\n</code></pre>\n\n<p>access it on content page like</p>\n\n<pre><code>Master.PropMasterTextBox1.Text="SomeString";\n</code></pre>\n\n<p>Option 2:\non Master page:</p>\n\n<pre><code>public string SetMasterTextBox1Text\n{  \n    get { return txtMasterBox1.Text; }\n    set { txtMasterBox1.Text = value; }\n}\n</code></pre>\n\n<p>on Content Page:</p>\n\n<pre><code>Master.SetMasterTextBox1Text="someText";\n</code></pre>\n\n<p>option 3 : \nyou can create some public method that works for you</p>\n\n<hr>\n\n<p>these approach is not so useful  but it helps if you just want to use some limited and predefined control</p>\n'}]
['https://Stackoverflow.com/questions/6406', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/576/']