在我的 .NET 应用程序中,我需要为动态创建的 asp:Table 中的每一行添加一个复选框。是否可以通过为每一行动态创建一个 asp:CheckBox 并以某种方式将其放在 TableCell 对象中来做到这一点?(在那种情况下怎么办?)
或者我是否需要将 asp:table 控件替换为其他东西,例如 Repeater 控件或 GridView 以使其工作?
我正在寻找最快的解决方案,因为我没有太多时间。
提前致谢!
/伊尔瓦
在 aspx 中:
<asp:Table id=T1 runat=server />
在cs中:
TableCell tc;
foreach(TableRow tr in T1.Rows)
{
tr.Cells.Add(tc = new TableCell());
((IParserAccessor)tc).AddParsedSubObject(new CheckBox());
}
您不想在服务器端执行此操作(如 Yossarian 所说,在 cs 中)。因为每次重新加载或刷新您的页面时,您都必须重新创建这些复选框,这意味着每次加载新的复选框,这也意味着您的复选框控件信息将丢失,因为它们不在客户端,所以所有更新的信息用户完成的(复选框选中)将丢失,因此您希望能够找出检查的内容,除非您添加 jquery 并且它开始变得更加复杂,然后需要
如果您使用的是网页,那么最好使用 asp:Gridview Web 控件并将数据绑定到后面代码中的表格,如下所示:
Gridview.Datasource=//ex:data;
Gridview.Databind();
如本页示例所示here
但是如果您使用的是 MVC,那么您可以将它们以如下形式添加到客户端代码中:
<% using (Html.BeginForm("Presentation", "Home")) %>
<% { %>
<table id="Table" class="color" width="100%" border="1">
<colgroup width="3%" ></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="20%"></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="47%"></colgroup>
<thead>
<tr class="dxgvHeader_Glass">
<th id="CheckBox" class="style1" ><input type="checkbox" class="selectall" id="selectall" name="CheckBox" /></th>
<th id="DateTime" runat="server"></th>
<th id="Description" runat="server"></th>
</tr>
</thead>
<tbody >
<%try
{ %>
<% foreach (var SamAuditLog in ViewData.Model)
{ %>
<tr>
<td class="style1" align="center"><%=Html.CheckBox(""+data.ID) %></td>
<td><%= data.DateTime%></td>
<td><%= data.Description%></td>
</tr>
<% } %>
<%} %>
</tbody>