• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

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

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?


 
0
TransBind
Asked:
TransBind
1 Solution
 
PraesidiumCommented:
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
            {
                  get
                  {
                        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\".");
                        }
                        try
                        {
                              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);
                        }
                  }
            }
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now