简体   繁体   English

更改 ItemDatabound 上数据列表内的链接按钮前景色

[英]Change link button fore color inside datalist on ItemDatabound

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:嗨,我正在尝试根据网络、copychimp、复制器和驱动器空间的条件更改链接按钮的前景色,我试过这个:

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;但是当我尝试用户更改e.Item.ForeColor = System.Drawing.Color.Green; to e.到 e。 Item. e.Item.BackColor = System.Drawing.Color.Green;
it work.这行得通。 Here is my html:这是我的 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?有人会帮我解决这个问题吗?

You need to use FindControl :您需要使用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.使用 FindControl 从代码隐藏页面中的 function 访问控件,访问另一个容器内的控件,或在调用者无法直接访问目标控件的其他情况下。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM