0

I am trying this

if (!IsPostBack)
    {
        Label theLabel = Master.FindControl("HeadingLabel") as Label;
        if (theLabel != null)
        {
            theLabel.Text = "Our Valuable Customers ";
        }
    }

i am getting a NullRefernceException error ,i am following a video series ,so i am doing the same ,but not able to run the code.He is is using .Net version =2.0 and i am using 4.0.

here is code for my master page

<asp:Label ID="HeadingLabel" runat="server" Font-Bold="True"Text="Online Shopping Website"></asp:Label>
Jatin Asija
  • 128
  • 4
  • 13
  • First thing to double-check: is `FindControl` actually returning `null`, or is it the use of the `as` operator? (This is why I prefer casting, in most places... it makes diagnostics simpler.) – Jon Skeet Mar 17 '15 at 09:35
  • [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Mar 17 '15 at 09:35
  • Find control is not recursive. chances are your HeadingLabel is contained in another control – Ewan Mar 17 '15 at 09:35
  • Looks like `Master` is null. If so, the page you're on isn't actually based on the master page. Make sure the `aspx` actually has a master page specified in the `@Page` directive. – Luaan Mar 17 '15 at 09:36
  • thanks @Luaan I forgot to do it on this page – Jatin Asija Mar 17 '15 at 09:45

1 Answers1

1

Looks like Master is null. If so, the page you're on isn't actually based on the master page.

Make sure the aspx actually has a master page specified in the @Page directive.

Luaan
  • 62,244
  • 7
  • 97
  • 116