【问题标题】:How Bind database in Checkbox?如何在 Checkbox 中绑定数据库?
【发布时间】:2014-01-15 18:18:31
【问题描述】:

我想将 SQL Server 上数据库中的每个 id 绑定到 CheckBox。但它不起作用。

这是我的代码:

表格取消信息

CancelId | CancelDetail
1 | Cancel01
2 | Cancel02
3 | Cancel03

HTML 来自

<asp:CheckBox ID="chkCancel" name="chkCancel" runat="server" OnLoad="chkCancel_Load" />

代码隐藏(类)

public static ClassCancelInfo[] ListCancel()
{
    using (var pcon = new ProjectConnect())
    {
        DataTable table = pcon.ExecuteDataTable("SELECT CancelDetail FROM CancelInfo Where CancelId =1 ");
        CancelInfo[] items = MapFrom(table);
        return items;
    }
}

代码 .aspx.cs

protected void chkCancel_Load(object sender, EventArgs e)
{
    chkCancel.Text = ClassCancelInfo.ListCancel().ToString();            
}

结果网页 ,,, □ >> 复选框

□□□

但我想要结果网页

□ 取消01

□ 取消02

□ 取消03

【问题讨论】:

    标签: c# asp.net sql-server data-binding checkbox


    【解决方案1】:

    你可以修改你的代码如下:

    <% 
     int i=0;
    foreach (ClassCancelInfo ci in ClassCancelInfo.ListCancel())
    { 
    %>
    
    <input type='checkbox' id="chk_<%=i.ToString()%>"/>
    <label for="chk_<%=i.ToString()%>"><%=ci.CancelDetail%></label>
    <br/>
    <% i=i+1;
     } 
    %>
    

    【讨论】:

      【解决方案2】:

      如何使用inline syntax with foreach 循环

        <% foreach(ClassCancelInfo ci in ClassCancelInfo.ListCancel()){  %>
      
               <asp:CheckBox ID="chk__<%= counter++ %>" value= "<%= ci.Value%>" /> 
               <%= ci.CancelDetail%>
      
               </br>
        <%  } %>
      

      【讨论】:

      • 我试试这个代码。不起作用,因为文本复选框不显示。
      • @nettoon493,我认为您正在使用 CancelInfo 类的 CancelDetail 属性进行显示。我更新了我的答案。
      猜你喜欢
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多