String.Format出错

时间:2013-09-13 12:57:07

标签: asp.net

我得到一个“对象引用未设置为对象的实例”。 “welcome.Text = ....”

行上的错误

主页:

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>

我从MCTS考试70-515 Web开发书中获得了代码。

我查看了勘误表页面,没有运气。 http://oreilly.com/catalog/errataunconfirmed.csp?isbn=9780735627406

母版页:

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>

此致 LF

2 个答案:

答案 0 :(得分:1)

主人的GreetingLabel控件位于何处?如果它位于ContentPlaceHolder NamingContainer,则不是主人。

你:“它在一个大师的下面,”:

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

就是这样,你首先必须找到ContentPlaceHolder,然后在其上使用FindControl

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

现在你不再NullReferenceException welcome.Text了:

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

修改由于您已评论说它仍然无法正常工作。我会建议一种不同的 - 更好的方法。在Master中提供公共财产,例如Greeetings。然后,您可以通过此属性获取/设置Label.Text。这更具可读性和可维护性。即使您将标签更改为TextBoxDiv之类的其他控件,它也会起作用。

例如(在MasterPage类型的Professional中):

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

现在,您可以将内容页面中的Master属性转换为Professional来访问它:

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

答案 1 :(得分:0)

试试这个:

看到这个

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

现在你的新行是

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