【问题标题】:ASP checkbox's 'checked' property always returns falseASP 复选框的 'checked' 属性总是返回 false
【发布时间】:2012-02-13 21:58:49
【问题描述】:

我最近将一个“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”中的按钮单击事件中,我尝试获取复选框的“选中”属性以执行一些逻辑。 我基本上使用下面的方法来找到用户控件并循环访问其中的控件。

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

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

【问题讨论】:

  • 我可以建议一些关于如何以更简单的方式找到控件的建议吗?
  • 您是否在加载事件中以某种方式将值初始化为 false,而不测试 IsPostBack?
  • @DJKRAZE 任何比我拥有的更好的东西都是受欢迎的。谢谢
  • @JonathanWood 我将用户控件的“数据绑定”事件包装在 !IspostBack 条件中
  • protected void Page_PreRender(object sender, EventArgs e) { if(!IsPostBack) { Pricing.DataBind(); Specifications.DisplaySpecifications(_sri, IsMobilePage);可用性.DisplayAvailability(_sri, IsMobilePage); }}

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


【解决方案1】:

我发现了为什么会发生这种情况......我会回答我自己的问题。 因此,模板中的 CheckBox ID 为“FindSimilarCheckBox”,并在数据绑定时重命名。因此,当回发发生时,服务器将所有复选框的 ID 作为“FindSimilarCheckBox”返回,并且所有内容的 Checked 属性都为 false。我必须重新对用户控件进行数据绑定,这一次设置一个条件来检查它是否是回发操作,以及 Request.Form 集合中是否存在复选框的唯一 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;
                }
 }

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

【讨论】:

    【解决方案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);

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

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

    【讨论】:

    • 感谢您抽出宝贵时间...事实上,这正是我在“GetCheckedAttributes”方法中所做的工作。
    • 没问题..我已经这样做了很多次,一旦你做得足够多,它就变得几乎自然了..如果这个解决方案有帮助,请投票..度过一个愉快的夜晚..
    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2012-02-16
    • 2020-06-16
    相关资源
    最近更新 更多