Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串强制转换为Web控件并在容器中定位变量_C#_Asp.net_Web Controls - Fatal编程技术网

C# 将字符串强制转换为Web控件并在容器中定位变量

C# 将字符串强制转换为Web控件并在容器中定位变量,c#,asp.net,web-controls,C#,Asp.net,Web Controls,使用VWD 2010 C#。Default.aspx(.cs)。 发生的情况:按钮使用ID=“Day31”更改标签的颜色。 考虑到本例中是每月的第31天。 按钮和标签位于“MainContent”内的表格内 问题:为什么在这行代码处出现Null错误消息?它为Null,原因可能是该方法所在的容器中没有具有该特定ID值的控件,或者具有该ID值的控件不是标签,它是另一种类型。如果使用标签daylab=MainContent.FindControl(str)作为标签它应该可以工作。试试MainConte

使用VWD 2010 C#。Default.aspx(.cs)。
发生的情况:按钮使用
ID=“Day31”
更改标签的颜色。 考虑到本例中是每月的第31天。 按钮和标签位于“MainContent”内的表格内


问题:为什么在这行代码处出现
Null
错误消息?

它为Null,原因可能是该方法所在的容器中没有具有该特定ID值的控件,或者具有该ID值的控件不是
标签,它是另一种类型。

如果使用标签daylab=MainContent.FindControl(str)作为标签它应该可以工作。

试试
MainContent.FindControl
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"
}