簡體   English   中英

如何在Checkbox中綁定數據庫?

[英]How Bind database in Checkbox?

我想將每個ID從SQL Server上的數據庫綁定到CheckBox。 但這是行不通的。

這是我的代碼:

表CancelInfo

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

HTML FROM

<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

inline syntax with foreach循環中使用inline syntax with foreach怎么辦

  <% foreach(ClassCancelInfo ci in ClassCancelInfo.ListCancel()){  %>

         <asp:CheckBox ID="chk__<%= counter++ %>" value= "<%= ci.Value%>" /> 
         <%= ci.CancelDetail%>

         </br>
  <%  } %>

您可以按以下方式修改代碼:

<% 
 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;
 } 
%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM