仅在检查未取消选中时调用checkchanged事件

时间:2014-07-05 08:16:09

标签: c# asp.net

我在Checkbox后面有CheckChanged事件,无论是勾选还是取消选中复选框都会调用它,但我只会在未选中复选框的情况下调用此事件。

代码:

protected void chkIsHead_CheckedChanged(object sender, EventArgs e)
    {

        if (txtSelectedID.Text != "")
        {
            int DepID = Convert.ToInt32(ViewState["depID"]);
            ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
            bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);

            if (isHead == true)
            {
                HiddenFieldSetMessage.Value = "HeadExists";
                HiddenFieldShowMessage.Value = "True";
                chkIsHead.Checked = false;
                HiddenFieldShowHideButtons.Value = "True";
            }
        }
        else 
        {
            int DepID = Convert.ToInt32(ViewState["depID"]);
            ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
            bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);

            if (isHead == true)
            {
                HiddenFieldSetMessage.Value = "HeadExists";
                HiddenFieldShowMessage.Value = "True";
                chkIsHead.Checked = false;
            }
        }


    }

2 个答案:

答案 0 :(得分:0)

您可以使用if条件检查Checked控件的CheckBox属性的状态。如下:

protected void chkIsHead_CheckedChanged(object sender, EventArgs e)
{
    if (chkIsHead.Checked)
    {
        // put your code here
    }
}

答案 1 :(得分:0)

此替代方案可能有效

<asp:CheckBox ID="CheckBox1" runat="server" Text="Check" 
    AutoPostBack="True" OnClick="return chkSelected();" 
    OnCheckedChanged="CheckBox1_CheckedChanged" />
<script type="text/javascript">
    function chkSelected() {
        var chk = document.getElementById('<%= CheckBox1.ClientID %>');
        if (chk.checked) {
            __doPostBack('<%= CheckBox1.ClientID %>', '');
        } else {
            return false;
        }
    }
</script>