Reputation: 15
I want to access label value in code behind page.
int a = int.Parse(Label5.Text);
if (a <= 10)
{
Label5.BackColor = System.Drawing.Color.Red;
}
<asp:Label CssClass="txtStock" ID="Label5" runat="server" Text='<%# Eval("Pquant") %>'></asp:Label>
I'm not able to access Label5
in code behind page as it doesn't exist.
I want to fetch Label5
value and store it in a variable.
Label5
is taken inside datalist control
Upvotes: 1
Views: 958
Reputation: 45947
You can't access Label5
in codebehind because it's part of a datacontrol like FormView
, GridView
, Repeater
or someting. So this Label
doesn't exist only once - it exists in every item of your data control.
If you want to set your BackColor
dynamicly you can do it in the Databinding method (e.g. 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;
}
}
}
or do directly in the Label
:
<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>
Upvotes: 1