2011-12-23 74 views
1

我试图将我的CheckBox的禁用属性设置为true。但是,当我做回发CheckBox仍然启用?为什么asp.net复选框没有被禁用?

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 

<head runat="server"> 
    <title></title> 
    <script> 
     $(document).ready(
      function() { 
       $("#CheckBoxList1_0").attr('disabled',true); 
      } 
    ); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:CheckBoxList ID="CheckBoxList1" runat="server"> 
      <asp:ListItem>een</asp:ListItem> 
      <asp:ListItem>twee</asp:ListItem> 
      <asp:ListItem>drie</asp:ListItem> 
     </asp:CheckBoxList> 
    </div> 
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
    </form> 
</body> 
</html> 

C#:

protected void Button1_Click(object sender, EventArgs e) 
     { 
      if (!CheckBoxList1.Items[0].Enabled) 
      { 
       Response.Write("it is disabled"); 

      } 
     } 
+0

是否有原因使用CheckBoxList而不是三个单独的CheckBox?如果你使用了一个真正的CheckBox,你可以使用Enabled属性并将其设置为false。 – NoLifeKing 2011-12-23 08:57:51

回答

2

CSS属性,如disabled与jQuery添加,或以任何方式为此事补充说,将不会发布到服务器时,你做回发。

如果您确实需要跟踪哪些复选框被禁用,则完成此操作的一种方法是将这些元素的ID存储到隐藏输入中,其中发送到服务器的值。

像这样的东西应该工作

<input type="hidden" runat="server" id="yourHiddenInput" /> 

$("form").submit(function(){ 
    var allIds = []; 
    $("input[type='checkbox']:disabled").each(function() { 
     allIds.push($(this).attr("id")); 
    }); 
    $("#yourHiddenInput").val(allIds.join()); 

    //form is about to post. When it does, 
    //you'll be able to read the ids via yourHiddenInput.Value 
}); 
+0

如果你有一个复选框,并且用jQuery禁用它(set disabled = disabled),那么它确实有一个效果在服务器上,尽管不是你可能期望的。如果禁用了设置,服务器端将忽略该控件的“已检查”值。如果您不禁用它,服务器会看到该复选框的正确值。它不会影响服务器的item.Enabled,甚至item.Attributes [“disabled”]的概念。 – philw 2012-06-05 12:34:43

1

当您更改使用javascript变化是只在客户端上的复选框,我相信,对于复选框的视图状态信息将不会被更新,所以当回传发生只要该网页知道复选框仍未选中

+0

该示例不是关于“检查”复选框。 即使如此,你错了:用js改变复选框的“checked”状态,就像你期望的那样传播到服务器。 – philw 2012-06-05 12:39:51