0

For some reason, I cannot get text into any textbox or label!

I'm using Master pages and the code is going in the code behind view. I have created the textbox:

<asp:Textbox ID="whatever" runat="Server">

When I want to add some text I simply add the code in the code behind view like:

whatever.Text = "myText";

I get an error that says:

"System.NullReferenceException:Object reference not set to an instance of an object"

hightlighting this line in red: whatever.Text = "myText";

I guess its because it saying it not there but how can it let me reference the textbox?

Apologies if the answer is on the site, I have searched but found nothing. :)

This is my code in Basket.asp - I've changed the textbox to a label, it's called bskItems

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server"> <asp:Label ID="bskItems" runat="server"></asp:Label> <div id="cart"> <asp:Button ID="btnCheckout" CssClass="BasketBtnAdd" runat="server" CommandName="checkout" Text="Checkout" /> </div> </asp:Content>

This is my masterpage, where I'm using a loginView. ContentPlaceHolder3 is where the textbox should be. I only want it to display a count of items.

<asp:LoginView ID="loginView" runat="server">
    <LoggedInTemplate>
    <asp:LoginName ID="loginName" runat="server" FormatString="Hi, {0}!"/>
    (<asp:LoginStatus ID="loginStatus" runat="server" />)
    <% 
    if (HttpContext.Current.User.IsInRole("Admin"))
    { 
    %>
    <asp:SiteMapDataSource ID="admin" SiteMapProvider="admin" runat="server" ShowStartingNode="false" />
    <asp:Menu ID="Menu" runat="server" DataSourceID="admin">
        <StaticItemTemplate>
            <%# Eval("Text") %>
        </StaticItemTemplate>        
    </asp:Menu>
    <%
    }
    if (HttpContext.Current.User.IsInRole("Users"))
    { 
    %>
    <asp:SiteMapDataSource ID="user" runat="server" SiteMapProvider="user" ShowStartingNode="false" />
    <asp:Menu ID="Menu1" runat="server" DataSourceID="user">
        <StaticItemTemplate>
            <%# Eval("Text") %>
        </StaticItemTemplate>        
    </asp:Menu>

    <%
    }
    %>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server"></asp:ContentPlaceHolder>
    </LoggedInTemplate>
    <AnonymousTemplate>
        <asp:LoginStatus ID="loginStatus" runat="server" />
        <asp:SiteMapDataSource ID="anon" runat="server" SiteMapProvider="anon" ShowStartingNode="false" />
        <asp:Menu ID="Menu2" runat="server" DataSourceID="anon">
        <StaticItemTemplate>
            <%# Eval("Text") %>
        </StaticItemTemplate>        
    </asp:Menu>
    </AnonymousTemplate>

</asp:LoginView>
8
  • Are you trying to set the text in the child content page or the master page? Commented Mar 3, 2011 at 17:53
  • When you debug, is the "whatever" null? Are you sure it's being instantiated on page load? Commented Mar 3, 2011 at 17:54
  • On the child content, I'm leaving the master alone and just using it to set up my template Commented Mar 3, 2011 at 17:56
  • As a side note, runat="server" should be all lowercase Commented Mar 3, 2011 at 17:56
  • Surely it being instantiated, it did cross my mind,is there a way to find out? Commented Mar 3, 2011 at 17:58

5 Answers 5

1

In addition to the other answers, if you're setting the value in Page.OnLoad, remember that the Master page controls haven't been created yet.

Here's a complete layout of the order in which things happen: Complete Lifecycle of an ASP Page

Sign up to request clarification or add additional context in comments.

3 Comments

Good point. Sometime can search a lot for a problem like that
Thanks for the heads up with this info, it's something I was unaware of. If the Page.Onload is'nt effectively ready for writing to a textbox etc, how would you get items into textboxes etc as the page loads?
You init the master page items in myMasterPage.OnLoad()
1

What I usualy do is to make the control visible as a property of my MasterPage.

On the master page (AMasterPage.master):

public TextBox MyTextBox { get { return this.theTextBoxControl; } }

So then, on a child using this masterPage (APage.aspx) :

((AMasterPage)this.Master).MyTextBox.Text = "myText";

6 Comments

should MyTextBox and theTextBoxControl be the same?
The "this.theTextBoxControl" is the TextBox control's ID from your MasterPage. So, no you can't name it the same ID because of an Ambigous ID exeption.
I'm not sure if this is what I need (or maybe I'm missing something:)) The textbox has been created in the child page and the master page just holds a container for contentplaceholders, so the textbox is not in the master page.
Oh well, so you try to access to master child page's control? ... show us more of your code please so I could help you more. Because "Object Reference not set t an instance of an object" exeption is probably the most global error of .NET ;o)
Just edited now, Thanks for the help;) I'm fairly new to ASP although I've been coding for a while (not sure if you could tell though eh at the minute!!!!)
|
0

When accessing Master Page members from Code-Behind in a Content Place Holder file, I believe you need to do:

this.Master.whatever.Text = "new Text";

Check this link on ASP.NET Master Pages, from MSDN.

Comments

0

You need to do get a reference to the textbox on the master page, then set the text

TextBox tb = Master.Page.FindControl("whatever") as TextBox;

if(tb != null)
{
    tb.Text = "myText";

}

Comments

0

Set the ClientIDMode on the textbox to "Static". When the page is rendered it assigns the TextBox's ID to something random. By changing the ClientIDMode to "Static", you should be able to reference the ID because the ID will stay the same and not change.

Or try adding an OnDataBinding event handler and casting the "sender" as a (TextBox). For example:

protected void TextBox_OnDataBinding(object sender, EventArgs e)
{
     var txt = (TextBox)sender;
     txt.Text = "Something";
}

This should talk to the control directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.