为什么没有禁用asp.net复选框?

时间:2011-12-23 01:53:55

标签: c# jquery asp.net

我正在尝试将CheckBox的禁用属性设置为true。但是,当我进行回发时,CheckBox仍处于启用状态?

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

            }
        }

2 个答案:

答案 0 :(得分:2)

disabled这样添加了jQuery的CSS属性,或者以任何方式添加的CSS属性,在执行回发时都不会发布到服务器。

如果你真的需要跟踪哪些复选框被禁用,一种方法是将这些元素的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
});

答案 1 :(得分:1)

当您使用javascript更改复选框时,更改仅在客户端上,我相信复选框的视图状态信息将不会更新,因此当回页发生时,只要页面知道复选框仍未选中< / p>

相关问题