ASP 复选框已“选中”属性总是返回 false
我最近将“DataList”控件移至 UserControl 中,并在我的 ASPX 页面上引用了它。 DataList 包含带有由数据源最初分配的选中属性的复选框。
<asp:DataList ID="dlspec" CssClass="specs" runat="server" GridLines="Vertical" OnItemDataBound="dlspec_ItemDataBound">
<FooterStyle BackColor="#CCCCCC" />
<AlternatingItemStyle CssClass="alt-grey" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<table>
<tr>
<td class="leftcol">
<asp:Label ID="lblDimension" runat="server" Text='<%# Eval("Dimension") %>'></asp:Label>:
</td>
<td class="ProductDetailData">
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Attribute") %>'></asp:Label>
</td>
<td class="find-similar">
<asp:CheckBox ID="FindSimilarCheckbox" runat="server" Checked='<%# Eval("CheckBox")=="true"? true:false %>' Text='<%# Eval("AttributeID") %>' Visible='<%# Eval("CheckBoxState")=="0"? true:false %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
现在,在用户控件绑定到的“aspx”中的按钮单击事件上,我尝试获取复选框的“checked”属性来执行一些逻辑。 我基本上使用下面的内容来查找用户控件并循环访问其中的控件。
Control SpecsPanel = FindSimilarPnl.FindControl("Specifications").FindControl("dlspec");
foreach (Control ct in SpecsPanel.Controls)
GetCheckedAttributes(ct, ref qry);
然而,在我将数据列表移入用户控件后,复选框的“已选中”属性总是显示为“假”。有什么想法吗?我错过了一些愚蠢的东西吗?非常感谢任何想法。如果需要,请告诉我添加更多代码以便您更好地理解。 谢谢
I had recently moved a "DataList" control in to a UserControl and referenced it on my ASPX page. The DataList contains checkboxes with checked properties assigned by the data source initially.
<asp:DataList ID="dlspec" CssClass="specs" runat="server" GridLines="Vertical" OnItemDataBound="dlspec_ItemDataBound">
<FooterStyle BackColor="#CCCCCC" />
<AlternatingItemStyle CssClass="alt-grey" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<table>
<tr>
<td class="leftcol">
<asp:Label ID="lblDimension" runat="server" Text='<%# Eval("Dimension") %>'></asp:Label>:
</td>
<td class="ProductDetailData">
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Attribute") %>'></asp:Label>
</td>
<td class="find-similar">
<asp:CheckBox ID="FindSimilarCheckbox" runat="server" Checked='<%# Eval("CheckBox")=="true"? true:false %>' Text='<%# Eval("AttributeID") %>' Visible='<%# Eval("CheckBoxState")=="0"? true:false %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Now, on a button click event in the "aspx" to which the user control is bound to, i try to get the "checked" properties of the check boxes to go through some logic.
I basically use the below to find the usercontrol and loop through the controls in it.
Control SpecsPanel = FindSimilarPnl.FindControl("Specifications").FindControl("dlspec");
foreach (Control ct in SpecsPanel.Controls)
GetCheckedAttributes(ct, ref qry);
However the "checked' property of the checkboxes always comes out to be "false" after i moved the datalist in to the user control. Any ideas why? Am I missing something silly? Greatly appreciate any thoughts ideas. Let me know if I need to add more code for you to understand better.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现为什么会发生这种情况......要回答我自己的问题。
因此,模板中的 CheckBox ID 为“FindSimilarCheckBox”,并在数据绑定时重命名。因此,当发生回发时,服务器将所有复选框的 ID 返回为“FindSimilarCheckBox”,并且所有内容的 Checked 属性均为 false。我必须重新数据绑定用户控件,这次放入一个条件来检查它是否是回发操作以及复选框的唯一 ID 是否存在于 Request.Form 集合中,以便在 chkbox 上设置 Checked 属性。像这样:
我的问题现在已经解决了。希望这个答案可以帮助您了解导致我的问题的原因。如果我需要提供更多细节,请告诉我。
I found out why this is happening...going to answer my own question.
So the CheckBox ID in the template is "FindSimilarCheckBox" and it is renamed at data-bind time. so when the postback occurs, the server returns the ID's for all the checkboxes as "FindSimilarCheckBox" and Checked property for everything is false. I had to re-data bind the usercontrol and this time put in a condition to check if it is a postback operation and if the Check Box's Unique ID exists in the Request.Form collection in order to set the Checked property on the chkbox. Something like this :
My issue is solved now. Hope this answer helps you understand what caused my issue. Let me know if i need to give more detail.
这是您可以创建的方法..
用法:FindAllCheckedBoxes(FindSimilarCheckbox);
如果您需要在网页上执行此操作,也可以使用以下代码
进行必要的更改以适合您的用例
Here is a Method you could create..
usage: FindAllCheckedBoxes(FindSimilarCheckbox);
if you need to do this on a Web page you could use the following code as well
make the changes necessary to fit your UseCase