ASP复选框的'checked'属性始终返回false

时间:2012-02-13 21:58:49

标签: c# asp.net user-controls datalist

我最近将一个“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”中的按钮单击事件上,我尝试获取复选框的“已检查”属性以通过某些逻辑。 我基本上使用下面的代码来查找usercontrol并遍历其中的控件。

Control SpecsPanel = FindSimilarPnl.FindControl("Specifications").FindControl("dlspec");
foreach (Control ct in SpecsPanel.Controls)
        GetCheckedAttributes(ct, ref qry);

然而,在我将数据列表移动到用户控件后,复选框的“已检查”属性总是“假”。任何想法为什么?我错过了一些愚蠢的东西吗?非常感谢任何想法的想法。让我知道如果我需要添加更多代码供您更好地理解。 感谢

2 个答案:

答案 0 :(得分:2)

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

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);

如果您需要在网页上执行此操作,您也可以使用以下代码 进行必要的更改以适合您的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)
        }
    }
}

答案 1 :(得分:2)

我发现了为什么会发生这种情况......要回答我自己的问题。 因此模板中的CheckBox ID是“FindSimilarCheckBox”,并在数据绑定时重命名。因此,当发生回发时,服务器将所有复选框的ID返回为“FindSimilarCheckBox”,并且Checked属性为false。我不得不重新绑定usercontrol数据,这次放入一个条件来检查它是否是一个回发操作,如果Check.Form集合中存在Check Box的唯一ID,以便在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;
                }
 }

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