2011-12-23 31 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

相關問題