中继器控件内的 IF 条件

IF condition inside the repeater control?

本文关键字:IF 条件 控件 中继器      更新时间:2023-09-26
<asp:CheckBox ID="chkBox1" runat="server" Text="1" />
<asp:CheckBox ID="chkBox2" runat="server" Text="2" />

我有两个复选框,根据选择,我需要运行查询并绑定到中继器控件中。

在中继器控制中:

<asp:Repeater runat="server" ID="Repeater1">
<HeaderTemplate >
<table class="list_table" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<th>1</th>
<th>2</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%=CheckBox1.Checked ? Eval("1") : "" %></td> 
 <td><%=CheckBox2.Checked ? Eval("2") : "" %></td> 
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>

AVD 建议我已经更改了我的编码,如果条件工作正常,但我需要将数据库中的数据绑定到中继器控件中。

你可以

试试,

 <ItemTemplate>
    <tr>
        <td><%=CheckBox1.Checked ? "1" : "" %></td>
        <td><%=CheckBox2.Checked ? "2" : "" %></td>
   </tr>
  </ItemTemplate>

编辑:

@Prince Antony G :如果我选择复选框 1,则运行查询 - 从表 1 中选择

1;或者如果我选择复选框 2 然后运行查询 - 从表 2 中选择 2;并将数据绑定到我的中继器控件中(这两个表都有不同的字段(

不能将不同的数据源绑定到 DataControl,因为某些绑定表达式(列(在这两种情况下都不可用(找到(。

页1.aspx标记


<div>
<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <!--Bind the result from first table -->
        </ItemTemplate>
        </asp:Repeater>
    </asp:View>
    <asp:View ID="View2" runat="server">
        <asp:Repeater ID="Repeater2" runat="server">
        <ItemTemplate>
            <!--Bind the result from second table -->
        </ItemTemplate>
        </asp:Repeater>
    </asp:View>
</asp:MultiView>
</div>

第 1 页.aspx.cs - 代码隐藏


protected void BindResult(object sender, EventArgs e)
{
    CheckBox check = sender as CheckBox;
    switch (check.Text)
    {
        case "1": 
            /* 
                * 1. Execute - Select * from Table1
                * 2. Bind the DataSource to Repeater1
                */
            /* Shows the first View*/
            MultiView1.ActiveViewIndex = 0;
            break;
        case "2":
            /* 
                * 1. Execute - Select * from Table2
                * 2. Bind the DataSource to Repeater2
                */
            /* Shows the second View*/
            MultiView1.ActiveViewIndex = 1;
            break;
    }
}