简体   繁体   中英

Error on String.Format

I get an "Object reference not set to an instance of an object." error on the line "welcome.Text = ...."

Home:

protected void OKButton_Click(object sender, EventArgs e)
{
    if (UserNameTextBox.Text != String.Empty)
    {
        Session["UserName"] = UserNameTextBox.Text;
        Label welcome = (Label)Master.FindControl("GreetingLabel");
        welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);
    }
}

<%@ Page Title="" Language="C#" MasterPageFile="~/Professional.master" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home"%>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <br /><br />
    <asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
    <br /><br />
    <asp:DropDownList ID="SitePrefDropDownList" runat="server" AutoPostBack="True">
        <asp:ListItem Text="Professional" Value="Professional"></asp:ListItem>
        <asp:ListItem Text="Colourful" Value="Colourful"></asp:ListItem>
    </asp:DropDownList>
    <br /><br />
    <asp:Button ID="OKButton" runat="server" Text="OK" onclick="OKButton_Click" />

</asp:Content>

I got the code from MCTS Exam 70-515 Web Dev book.

I looked at the Errata page, no luck. http://oreilly.com/catalog/errataunconfirmed.csp?isbn=9780735627406

Master Page:

public partial class Professional : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] != null)
        {
            GreetingLabel.Text = String.Format("Welcome, {0}!", Session["UserName"]);
        }
    }
}

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Professional.master.cs" Inherits="Professional" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <asp:ContentPlaceHolder id="HeadContent" runat="server">
    </asp:ContentPlaceHolder>

<link href="~/Styles/Site.css" rel="Stylesheet" type="text/css" />

</head>
<body>
    <form id="form1" runat="server">
    <div>

        <img src="Contoso.gif"  /><asp:Label ID="Label1" runat="server" Text="Welcome to Contoso!" 
                Font-Size="X-Large"></asp:Label>



        &nbsp;<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
            <Items>
                <asp:MenuItem Text="Products" Value="Products"></asp:MenuItem>
                <asp:MenuItem Text="Services" Value="Services"></asp:MenuItem>
                <asp:MenuItem Text="Downloads" Value="Downloads"></asp:MenuItem>
                <asp:MenuItem Text="About Us" Value="About Us"></asp:MenuItem>
            </Items>
        </asp:Menu>



        <asp:ContentPlaceHolder id="MainContent" runat="server">

            <asp:Label ID="GreetingLabel" runat="server" Text="Label"></asp:Label>

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Regards LF

Where is the GreetingLabel control located in the master? If it's in a ContentPlaceHolder the NamingContainer is that not the master.

You: "it is in a Master, under":

<asp:ContentPlaceHolder id="MainContent" runat="server">

That's it, you first have to find the ContentPlaceHolder , then use FindControl on it:

var cPlaceHolder = (ContentPlaceHolder)Master.FindControl("MainContent");
Label welcome = (Label)cPlaceHolder.FindControl("GreetingLabel");

Now you don't get a NullReferenceException on welcome.Text anymore:

welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);

Edit Since you have commented that it still doesn't work for whatever reason. I will suggest a different - better - approach. Provide a public property in your Master , for example Greeetings . Then you can get/set the Label.Text via this property. That is much more readable and maintainable. It will also work even if you change the label to a different control like TextBox or Div .

For example (in the MasterPage of type Professional ):

public string Greetings
{
    get { return GreetingLabel.Text;  }
    set { GreetingLabel.Text = value; }
}

Now you can cast the Master property in your content-page to Professional to access it:

Professional professional = (Professional) this.Master;
professional.Greetings = String.Format("Welcome, {0}!", Session["UserName"]);

Try this:

see this

welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);// replace Session["UserName"] with Session["UserName"].ToString()

now your new line is

welcome.Text = String.Format("Welcome, {0}!", Session["UserName"].ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM