如果那么在带有Checkbox的

时间:2016-06-23 05:15:34

标签: c# asp.net if-statement

这是我的ASP转发器代码

<asp:repeater id="filterTable" runat="server">
<HeaderTemplate>
    <br/><br/><br/>
    <div class="table-responsive">
            <table width="100%" cellspacing="0" id="table_odd" class="quickordertable table table-bordered table-striped table-condensed grid-table">
        <tr>
            <th style="width: 80px">
                Details
            </th>
            <th style="width: 80px">
                Filter
            </th>
            <th style="width: 30px">
                Qty
            </th>
            <th style="width: 60px">
                Include In Order
            </th>
        </tr>

</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td><%#Eval("Details")%></td>
        <td><%#Eval("FilterType")%></td>
        <td><%#Eval("Qty")%></td>
        <td><asp:CheckBox ID="orderPartNumber" Checked="true" runat="server"/><asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>'></td>
    </tr>
</ItemTemplate>
<FooterTemplate>
        </table>
    </div>
    </FooterTemplate>
</asp:Repeater>

我有一个ASP ItemTemplate,其字段为<%#Eval("GenuineOnly")%>

如果字段GenuineOnly包含值,(或者不是Null)那么我想显示此代码

<td>GENUINE ONLY</td>

否则我想显示此代码

<td><asp:CheckBox ID="orderPartNumber" runat="server"/><asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>'></td> 

我尝试过多种不同的方式,但似乎无法正确理解语法!任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用以下方式实现此目的:

<td>
    <asp:Panel runat="server" Visible='<%#Eval("GenuineOnly").ToString().Length > 0 %>'>
        <asp:CheckBox ID="orderPartNumber" Checked="true" runat="server" />
        <asp:HiddenField runat="server" ID="hiddenPartNumber" Value='<%#Eval("PartNumber")%>' />
    </asp:Panel>
    <asp:Panel runat="server" Visible='<%#Eval("GenuineOnly").ToString().Length == 0 %>'>
        GENUINE ONLY
    </asp:Panel>
</td>

答案 1 :(得分:1)

我假设您可以根据需要添加Table / Td。

<asp:Repeater ID="rptr" runat="server" OnItemDataBound="rptr_ItemDataBound">
<ItemTemplate>
        <asp:Label ID="lblGenuineOnly" runat="server" Text='<%#Eval("GenuineOnly") %>' />
        <asp:CheckBox ID="orderPartNumber" runat="server"/>
        <asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>' runat="server" />
    </ItemTemplate>

背后的代码

protected void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lblPartNumber = (Label)e.Item.FindControl("lblGenuineOnly");
        CheckBox chkPart = (CheckBox)e.Item.FindControl("orderPartNumber");

        if (string.IsNullOrEmpty(lblPartNumber.Text.Trim())) {
            //Display GENUINE ONLY in any label like lblPartNumber.Text = "GENUINE ONLY"
            chkPart.Visible = false;
        }
        else
        {
            chkPart.Visible = true;                
        }
    }