0

我正在尝试更改 asp:TemplateField 的 ItemTemplate,以便如果 waitingFor 属性低于 120,则显示进度条,如果该值高于文本 Not Applicable 则显示。

你会如何做类似于我在下面尝试的事情

                      <asp:TemplateField HeaderText="Time" HeaderStyle-CssClass="table-header-repeat line-left"
                                                    SortExpression="WaitingFor">
                                                    if(Eval("WaitingFor")<120){
                                                    <ItemTemplate>
                                                        <span id="progressBar<%# Eval("OrderId") %>sec" style="color: #006600; font-weight: bold;">
                                                            <%# Eval("WaitingFor") %>
                                                            s</span>
                                                        <br />
                                                        <span id="progressBar<%# Eval("OrderId") %>" ordernr="<%# Eval("OrderId") %>" class="progressBar"
                                                            waitingfor="<%# Eval("WaitingFor") %>">[ Loading Progress Bar ]</span>
                                                    </ItemTemplate>
                                                    }else{
                                                    <ItemTemplate>
                                                        Not applicable
                                                    </ItemTemplate>
                                                    }
                                                    <HeaderStyle CssClass="table-header-repeat line-left" />
                                                    <ItemStyle Width="150px" />
                                                </asp:TemplateField>
4

1 回答 1

0

为什么不只制作一个<ItemTemplate>内部具有服务器控件的控件,然后您可以显示/隐藏满足或不满足条件的控件,如下所示:

Markup:
<asp:TemplateField HeaderText="Time" HeaderStyle-CssClass="table-header-repeat line-left" SortExpression="WaitingFor">
    <ItemTemplate>
        <asp:Label id="Seconds" runat="server" style="color: #006600; font-weight: bold;"></asp:Label>
        <br />
        <asp:Label id="ProgressBar" ruant="server" data-ordernr="<%# Eval("OrderId") %>" CssClass="progressBar" data-waitingfor="<%# Eval("WaitingFor") %>">[ Loading Progress Bar ]</asp:Label>
        <asp:Label id="NotApplicable" runat="server" Visible="False">Not applicable</asp:Label>
    </ItemTemplate>                                            
    <HeaderStyle CssClass="table-header-repeat line-left" />
    <ItemStyle Width="150px" />
</asp:TemplateField>

Code-behind:
protected void GridView_People_RowDataBound(object sender, GridViewRowEventArgs e)
{               
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Cast the e.Row object to type the GridView was bound to


        // Setup conditional logic here for what you want to test (WaitingFor value less than 120 seconds)

        // In else logic, hide the Seconds label, the ProgressBar label and show the NotApplicable label, like this:
        Label secondsLabel = e.Row.FindControl("Seconds") as Label;

        // Check for null before trying to use the control, like this:
        if(secondsLabel != null)
        {
          // Hide the label
          secondsLabel.Visible = false;
        }

        // Rinse and repeat for other controls that you want to show/hide
    }
}
于 2013-06-30T16:00:02.573 回答