Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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# 我想访问代码隐藏页中的标签值_C#_Asp.net_Razor - Fatal编程技术网

C# 我想访问代码隐藏页中的标签值

C# 我想访问代码隐藏页中的标签值,c#,asp.net,razor,C#,Asp.net,Razor,我想访问代码隐藏页中的标签值 inta=int.Parse(Label5.Text); 如果(a) 我无法访问代码隐藏页面中的Label5,因为它不存在。 我想获取Label5值并将其存储在变量中。 Label5是在datalist控件中获取的Label5您不能在codebehind中访问Label5,因为它是数据控件的一部分,如FormView、GridView、Repeater或其他内容。因此,标签不是只存在一次,它存在于数据控件的每个项中 如果要动态设置背景色,可以使用数据绑定方法(例如

我想访问代码隐藏页中的标签值

inta=int.Parse(Label5.Text);
如果(a)
我无法访问代码隐藏页面中的
Label5
,因为它不存在。 我想获取
Label5
值并将其存储在变量中。
Label5
是在datalist控件中获取的
Label5
您不能在codebehind中访问
Label5
,因为它是数据控件的一部分,如
FormView
GridView
Repeater
或其他内容。因此,
标签
不是只存在一次,它存在于数据控件的每个项中

如果要动态设置
背景色
,可以使用数据绑定方法(例如
GridView

protected void YourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label Label5 = (Label)e.Row.FindControl("Label5");
        if (int.Parse(Label5.Text) <= 10)
        {
            Label5.BackColor = System.Drawing.Color.Red;
        }
    }
}
<asp:Label CssClass="txtStock" ID="Label5" runat="server" Text='<%# Eval("Pquant") %>'
BackColor='<%# int.Parse(Eval("Pquant")) <= 10 ? System.Drawing.Color.Red : System.Drawing.Color.Black %>' 
></asp:Label>