Quantcast
Channel: User Alexis Abril - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by Alexis Abril for Nested Masterpages and .FindControl

$
0
0

When you're nesting master pages, you'll get an extra container "Content" you need to look through.

As a result, if you're trying to use FindControl from a given child page the usual approach is something to the effect of:

Label myLabel = (Label)this.Master.FindControl("myLabel");myLabel.Text = "Success!";

Since we have a nested master page, with "myLabel" in the child master, this control will be contained within a content control.

So, this changes the code to:

ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("yourContentPane");Label myLabel = (Label)ph.FindControl("myLabel");myLabel.Text = "Success!";

and in VB.NET

Dim ph As ContentPlaceHolder = DirectCast(Me.Master.Master.FindControl("yourContentPane"), ContentPlaceHolder)Dim myLabel As Label = DirectCast(ph.FindControl("myLabel"), Label)myLabel.Text = "Success!"

The content from the child page is loaded into the first master page control, which is subsequently loaded into the grandparent master page.


Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>