c# - 中继器控件内的 IF 条件?

标签 c# javascript asp.net repeater

<asp:CheckBox ID="chkBox1" runat="server" Text="1" />
<asp:CheckBox ID="chkBox2" runat="server" Text="2" />

我有两个复选框,基于我需要运行查询并绑定(bind)到转发器控件的选择。

在中继器控制中:

<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 建议我更改了编码,如果条件正常,但我需要将数据库中的数据绑定(bind)到转发器控件中。

最佳答案

你可以试试,

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

编辑:

@Prince Antony G:如果我选择 checkbox1 然后运行查询 - select 1 from table1;或者如果我选择 checkbox2 然后运行查询 - 从 table2 中选择 2;并将数据绑定(bind)到我的转发器控件中(这两个表都有不同的字段)

您不能将不同的数据源绑定(bind)到 DataControl,因为某些绑定(bind)表达式(列)在或者的情况下不可用(找到)。

Page1.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>

Page1.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;
    }
}

关于c# - 中继器控件内的 IF 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12067373/

相关文章:

c# - C# 新手 - 无法使 File.Copy 工作

c# - 请求的操作需要 sql clr 上下文

javascript - JavaScript 中的 Ruby 样式 block ?

javascript - 向最终用户隐藏 html DOM 数据

javascript - 如何防止 keydown 事件监听器在输入文本字段时监听?

javascript - 在 ASP.NET 中使用 Underscore.js

c# - Access 数据库到 Web 应用程序

javascript - Asp.net 使用 jQuery 将文本框输入转换为大写

c# - 如何在公式中使用 SQL 值

c# - 使用正则表达式删除文本部分 C#