Reputation: 85
Hi i'm trying to change the link button fore color base on the condition of network, copychimp, replicator and drive space I've tried this:
protected void dgrMachines_ItemDataBound(object sender, DataListItemEventArgs e)
{
string copychimp = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "copychimp"));
string network = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "network_isconnected"));
string drive = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "drive_alert"));
string replicator = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "replicator_isactive"));
if (network == "0" || copychimp == "Disconnected" || drive == "1" || replicator == "0")
{
e.Item.ForeColor = System.Drawing.Color.Red;
}
else
{
e.Item.ForeColor = System.Drawing.Color.Green;
}
}
no luck
but when i try to user change the e.Item.ForeColor = System.Drawing.Color.Green;
to e.Item. e.Item.BackColor = System.Drawing.Color.Green;
it work.
Here is my html:
<asp:DataList ID="dgrMachines" runat="server" RepeatColumns="5" OnSelectedIndexChanged="dgrMachines_SelectedIndexChanged" OnItemCommand="dgrMachines_ItemCommand" CellPadding="3" CssClass="col-12" OnItemDataBound="dgrMachines_ItemDataBound">
<HeaderTemplate>
<div class="container col-12" style="background-color: #333333">
<b>
<h2 class="text-center" style="color: white">Machines List</h2>
</b>
</div>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lblMachine" Text='<%# Eval("machine") %>' runat="server" Font-Size="Medium" ForeColor="Black"></asp:LinkButton>
<%-- <asp:label ID="lblcopychimp" runat="server" Text='<%# Eval("copychimp") %>' />
<asp:label ID="lblNetwork" runat="server" Text='<%# Eval("network_isconnected") %>' />
<asp:label ID="lblreplicator" runat="server" Text='<%# Eval("replicator_isactive") %>' />
<asp:label ID="lbldrive" runat="server" Text='<%# Eval("drive_alert") %>' />--%>
<%-- <%#Eval("machine")%> --%>
</ItemTemplate>
</asp:DataList>
Would someone help me out with this?
Upvotes: 0
Views: 605
Reputation: 5380
You need to use FindControl
:
Use FindControl to access a control from a function in a code-behind page, to access a control that is inside another container, or in other circumstances where the target control is not directly accessible to the caller. This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.
So it should be like this:
LinkButton machineButton = (e.Item.FindControl("lblMachine") as LinkButton);
if (machineButton != null)
{
machineButton.ForeColor = System.Drawing.Color.Red;
}
instead of:
e.Item.ForeColor = System.Drawing.Color.Red;
Upvotes: 1