2

I am trying to pass text to a Literal, the parent.master has the ContentTemplateID's and the Child.master has the contentID's, one of them is the literal of form

<asp:Literal runat="server" ID="myLiteral"></Literal>

I am trying to pass it from a UserControl.cs file like this

 gooleTrackin = track.GetTrack(track.OrderType.YAHOO); 
 Literal mpLiteral = (Literal)this.Parent.Page.Master.FindControl("myLiteral");
mpLiteral.Text = gooleTrackin.ToString(); //nullReference here 

But it is giving me NulLReference in the last line.

By the way I do not have access to the .cs files of the master pages, it has to be done through the UserControl.

Thank you

ADDITIONAL CODE (THIS IS LOCATED IN THE CHILD.MASTER)

<%@ Master Language="C#" AutoEventWireup="true" MasterPageFile="../common/child.master" %>
<%@ Register Src="UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
   <div class="inner"><uc1:UserControl ID="theRceipt" runat="server" Visible="true"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BottomContentTemplate" Runat="Server">
<div style="margin-bottom:30px;">
   <asp:Literal ID="myLiteral" runat="server"></asp:Literal>
<a href="~/" id="23" runat="server" class="BackLink">Back to Home Page</a>
</div>
</asp:Content>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user710502
  • 11,181
  • 29
  • 106
  • 161
  • Could we have some more details. For instance, is `mpLiteral` have a null value, or does `googleTrackin`? If you could show us some code surrounding the `asp:Literal` tag, that would probably help as well. – Ken Wayne VanderLinde Jun 29 '11 at 02:42
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Jun 29 '11 at 03:30
  • Ok I am new to C#, And I understood why they happen, but still i am not sure where in my Code it could happen? – user710502 Jun 29 '11 at 03:39

1 Answers1

1

When master page is used, controls from master page are merged into the control hierarchy of page so this can be an issue while finding controls. I will suggest that you try following and see if it works:

Literal mpLiteral = (Literal)this.Page.FindControl("myLiteral");

OR

Literal mpLiteral = (Literal)this.Parent.FindControl("myLiteral");

Otherwise, you may have to try recursive find - see http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

However, I will rather recommend a alternate way - assuming that you have defined your literal control in Child.Master and Child is the name of code behind class for master, you can add an helper method in it such as

public void UpdateMyLiteral(string text)
{
   myLiteral.Text = text;
}

And in user control code, invoke the helper method such as

((Child)this.Page.Master).UpdateMyLiteral("xyz");

Note that we are casting master to its code-behind class.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • Thank you so much VinayC. The problem is that I dont have access to the Master.cs file (company wants me to use the user control) :( – user710502 Jun 29 '11 at 04:57
  • I see the problem. It is not finding the control... How can I test this or fix it? – user710502 Jun 29 '11 at 05:01
  • @user710502, to find the control, you have to go to its immediate naming container - I believe it to be the page in this case - so try `this.Page.FindControl`, if that doesn't work then use recursive `FindControl` on page (see the implementation provided in the link). – VinayC Jun 29 '11 at 05:29
  • Ok good, but you see how messy the design is for these masters?, the child inherits the parent, in the parent we have the ContentTemplates but in the child we add the content and the user control passes the values :( and no .cs for any of both master – user710502 Jun 29 '11 at 05:38
  • My IDE can not recognize wwWebUtils this.lblError = wwWebUtils.FindControlRecursive(this.Master,"lblError") as Label; – user710502 Jun 29 '11 at 06:15