【发布时间】:2019-06-13 09:54:24
【问题描述】:
我有一对复选框列表,我想在它们之间切换项目。这些项目的切换是使用javascript完成的。基本上就是生成了页面,生成了复选框列表的表格,然后通过点击按钮,可以在两者之间交换项目。
<tr>
<th>Report Columns</th>
<td>
<div id="allFieldsContainer" style="overflow-y: scroll; height: 200px; width: auto; border: 1px solid #DDDBDB;">
<a href="#" class="list-group-item active">All Columns
<input title="toggle all" type="checkbox" class="all pull-right" /></a>
<asp:CheckBoxList ID="allFields" CssClass="list-group" runat="server">
</asp:CheckBoxList>
</div>
</td>
<td>
<div id="transferButtons">
<button class="add" onclick="return false;" style="width: 100%;">--></button>
<button class="remove" onclick="return false;" style="width: 100%;"><--</button>
</div>
</td>
<td>
<div id="selectedFieldsContainer" style="overflow-y: scroll; height: 200px; width: auto; border: 1px solid #DDDBDB;">
<a href="#" class="list-group-item active">Selected Columns
<input title="toggle all" type="checkbox" class="all pull-right" /></a>
<asp:CheckBoxList ID="selectedFields" CssClass="list-group" runat="server" >
</asp:CheckBoxList>
</div>
</td>
</tr>
当然,其中一个复选框列表应该是空的,而另一个是满的。所以“allFields”将包含他们想要选择的所有字段,然后“selectedFields”将开始为空。问题是,空的复选框列表不会为他们自己生成任何 html。请参阅下面生成的页面。
<th>Report Columns</th>
<td>
<div id="allFieldsContainer" style="overflow-y: scroll; height: 200px; width: auto; border: 1px solid #DDDBDB;">
<a href="#" class="list-group-item active">All Columns
<input title="toggle all" type="checkbox" class="all pull-right"></a>
<table id="ContentPlaceHolder1_allFields" class="list-group">
<tbody>
<!–– Data in here is generated fine, along with the table -->
</tbody>
</table>
</div>
</td>
<td>
<div id="transferButtons">
<button class="add" onclick="return false;" style="width: 100%;">--></button>
<button class="remove" onclick="return false;" style="width: 100%;"><--</button>
</div>
</td>
<td>
<div id="selectedFieldsContainer" style="overflow-y: scroll; height: 200px; width: auto; border: 1px solid #DDDBDB;">
<a href="#" class="list-group-item active">Selected Columns
<input title="toggle all" type="checkbox" class="all pull-right"></a>
<!-- There should be a table here, there isn't one -->
<!-- This causes javascript to fail as it references a missing table -->
</div>
</td>
我尝试了一些方法,例如添加一个带有display:none; 的虚拟项目,但这会在表格中留下尴尬的间距。我一直在考虑编写一些 javascript 以在页面加载后立即将其删除,但我想知道是否有一些更简单的东西我错过了。
【问题讨论】:
-
既然你使用javascript来操作列表,为什么不直接定义一个带有id的html表呢?
-
我需要使用 C# 处理代码隐藏中的数据,引用控件比引用表格要容易得多(在我看来),这是我业务中的标准。我宁愿不自己手写表格。
-
相反,通过javascript添加到列表中的项目不会以标准方式在代码隐藏中可用。