带有模板字段的GridView,它根据属性进行更改

时间:2013-06-30 15:19:33

标签: asp.net gridview

我正在尝试更改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>

1 个答案:

答案 0 :(得分: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
    }
}