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>