ASP 复选框已“选中”属性总是返回 false

发布于 2025-01-05 04:22:34 字数 1806 浏览 0 评论 0原文

我最近将“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2025-01-12 04:22:34

我发现为什么会发生这种情况......要回答我自己的问题。
因此,模板中的 CheckBox ID 为“FindSimilarCheckBox”,并在数据绑定时重命名。因此,当发生回发时,服务器将所有复选框的 ID 返回为“FindSimilarCheckBox”,并且所有内容的 Checked 属性均为 false。我必须重新数据绑定用户控件,这次放入一个条件来检查它是否是回发操作以及复选框的唯一 ID 是否存在于 Request.Form 集合中,以便在 chkbox 上设置 Checked 属性。像这样:

protected void dlspec_ItemDataBound(object sender, DataListItemEventArgs e)
 {
    var ck = e.Item.FindControl("FindSimilarCheckbox") as CheckBox;
            if (ck != null)
            {
                ck.ID = ck.Text;
                ck.Text = "";
                //EDIT: Karthik - Since we moved the Specifications in to user control, check if this a postback , then check to see the CheckBox state on the form while posting back
                if(IsPostBack && Request.Form[ck.UniqueID] != null)
                {
                    ck.Checked = true;
                }
 }

我的问题现在已经解决了。希望这个答案可以帮助您了解导致我的问题的原因。如果我需要提供更多细节,请告诉我。

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 :

protected void dlspec_ItemDataBound(object sender, DataListItemEventArgs e)
 {
    var ck = e.Item.FindControl("FindSimilarCheckbox") as CheckBox;
            if (ck != null)
            {
                ck.ID = ck.Text;
                ck.Text = "";
                //EDIT: Karthik - Since we moved the Specifications in to user control, check if this a postback , then check to see the CheckBox state on the form while posting back
                if(IsPostBack && Request.Form[ck.UniqueID] != null)
                {
                    ck.Checked = true;
                }
 }

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.

樱娆 2025-01-12 04:22:34

这是您可以创建的方法..

public void FindAllCheckedBoxes(Control ctrl) 
{ 
    if (ctrl != null) 
    { 
        foreach (Control c in ctrl.Controls) 
        { 
            if (c is CheckBox)
            {   
               ((CheckBox)c).Checked = false;
               //or mess around with the code to do what ever it is you want.. 
            } 
           //uncomment if you need to add recurisve call FindAllCheckedBoxes(c); 
        } 
    } 
} 

用法:FindAllCheckedBoxes(FindSimilarCheckbox);

如果您需要在网页上执行此操作,也可以使用以下代码
进行必要的更改以适合您的用例

Protected void SetCheckBoxState( ControlCollection  controls)
{
    Foreach (Control c in controls)
    {
        If (c is System.Web.UI.WebControls.CheckBox)//change to make it CheckBox
        {
            CheckBox cb = c as CheckBox;
            cb.Checked = false; // or true what ever you need to do 
        }
        Else if (c.controls.Count > 0)
        {
            SetCheckBoxState(c.Controls)
        }
    }
}

Here is a Method you could create..

public void FindAllCheckedBoxes(Control ctrl) 
{ 
    if (ctrl != null) 
    { 
        foreach (Control c in ctrl.Controls) 
        { 
            if (c is CheckBox)
            {   
               ((CheckBox)c).Checked = false;
               //or mess around with the code to do what ever it is you want.. 
            } 
           //uncomment if you need to add recurisve call FindAllCheckedBoxes(c); 
        } 
    } 
} 

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

Protected void SetCheckBoxState( ControlCollection  controls)
{
    Foreach (Control c in controls)
    {
        If (c is System.Web.UI.WebControls.CheckBox)//change to make it CheckBox
        {
            CheckBox cb = c as CheckBox;
            cb.Checked = false; // or true what ever you need to do 
        }
        Else if (c.controls.Count > 0)
        {
            SetCheckBoxState(c.Controls)
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文