set a value of literal in web user control referenced by master page and master page is referenced by WebPage

Posted on 2007-07-19
Last Modified: 2007-12-19
I have this set up
Master Page references Web User control, in Web User control there is a Literal
WebPage.aspx references Master Page. I would like to set a value of Literal in Web User Control in WebPage.aspx.cs file. I can do this in two ways and I would like to find out if there is a better approach.

Approach One:
I use Master Type to create a reference to Master Page in WebPage.aspx and in WebPage.aspx.cs file I use Master.FindControl("Literal").Text = value etc ...  The problem with this approach is FindControl, if in master page Web User Control is sitting inside of server elements than it would be a hard to reference it, and I would have to do it recursively.

Approach Two:
I can create a public property in Master Page Code behind which will reference a Literal from Web user control and in WebPage.aspx.cs I access the property and set a value of a Literal through a property.

Which Approach is Better and is there another approach where I can avoid creating a property?

Question by:TransBind
    1 Comment
    LVL 6

    Accepted Solution

    Use FindControl and assign it to a local literal variable on the page, or just create a property in your page class like:
          protected Literal My Literal
                            System.Web.UI.Control found = this.Page.FindControl("MyLiteralName");
                            if (found == null)
                                  throw new ApplicationException("This page must define a literal control using the ID \"MyLiteralName\".");
                                  return (Literal)found;
                            catch (InvalidCastException ex)
                                  throw new ApplicationException("This page must define a Literal control using the ID \"MyLiteralControl\". Another control is using this ID.", ex);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now