0

VWD 2010 C# を使用。Default.aspx (.cs)。
どうなるか: ボタンを押すと、ラベルの色が に変わりますID = "Day31"。この例では、月の 31 日であるとします。ボタンとラベルは、「MainContent」内のテーブル内に配置されます。

protected void Red_Click(object sender, EventArgs e)
{
    ContentPlaceHolder MainContent = Page.Master.FindControl("MainContent") as   ContentPlaceHolder;
    int theday;
    theday = System.DateTime.Now.Day; // example the day is 31st
    string str="Day"+theday;
    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + str+ "');", true);
    Label dayLabel = FindControl(str) as Label;

    dayLabel.BackColor = System.Drawing.Color.Red; // this line error, "Null"
}

質問:Nullこのコード行にエラー メッセージがあるのはなぜですか?

4

2 に答える 2

0

そうすればうまくLabel dayLabel = MainContent.FindControl(str) as Label;いくはずです。

于 2012-11-07T20:03:27.830 に答える
0

メソッドが入っているコンテナにその特定の ID 値を持つコントロールがないか、その ID 値を持つコントロールが ではないため、null です。それはLabel他の型です。

于 2012-11-07T19:53:03.460 に答える