admin管理员组

文章数量:1287487

I have a checkbox on a form:

<asp:CheckBox ID="CheckBox1" runat="server"
Visible="true" AutoPostBack="True" oncheckedchanged="CheckBox1_CheckedChanged" />

It is not firing the below event when I check or uncheck it.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    if (CheckBox1.Checked)
    {
        preContactTextBox.Visible = true;
        prePracticeCodeTextBox.Visible = true;            
    }
    else
    {
        preContactTextBox.Visible = false;
        prePracticeCodeTextBox.Visible = false;            
    }
}

It is not entering this event at all. What I am doing wrong?

here's the plete code:

How can I get it to fire the event?

I have a checkbox on a form:

<asp:CheckBox ID="CheckBox1" runat="server"
Visible="true" AutoPostBack="True" oncheckedchanged="CheckBox1_CheckedChanged" />

It is not firing the below event when I check or uncheck it.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    if (CheckBox1.Checked)
    {
        preContactTextBox.Visible = true;
        prePracticeCodeTextBox.Visible = true;            
    }
    else
    {
        preContactTextBox.Visible = false;
        prePracticeCodeTextBox.Visible = false;            
    }
}

It is not entering this event at all. What I am doing wrong?

here's the plete code:

http://pastebin./amQURr91

How can I get it to fire the event?

Share Improve this question edited Jul 24, 2013 at 13:18 Ryan Gates 4,5397 gold badges52 silver badges92 bronze badges asked Sep 14, 2011 at 18:21 Alex GordonAlex Gordon 60.9k304 gold badges703 silver badges1.1k bronze badges 6
  • the problem might be with the viewstate or the postback event. – Vinay Commented Sep 14, 2011 at 18:38
  • @vinay how do i change viewstate? what should i do wtih it – Alex Gordon Commented Sep 14, 2011 at 19:15
  • Did you check with FireBug or some other javascript debugger if you have a javascript error in the page? – onof Commented Sep 14, 2011 at 19:24
  • @onof yes i did check and there are no errors – Alex Gordon Commented Sep 14, 2011 at 19:26
  • here is what the code looks like after browser interprets it <dl> 204 <dt><label for="CheckBox1">PreAnalytical?</label></dt> 205 <dd> <span OnCheckChanged="CheckBox1_CheckedChanged"><input id="CheckBox1" type="checkbox" name="CheckBox1" onclick="javascript:setTimeout('__doPostBack(\'CheckBox1\',\'\')', 0)" /></span></dd> 206 </dl> 207 – Alex Gordon Commented Sep 14, 2011 at 19:29
 |  Show 1 more ment

4 Answers 4

Reset to default 4

The CheckedChanged event of Checkbox control raises only if the AutoPostBack property of checkbox is specified with value "true".

I would check to make sure that you don't have validation somewhere that's preventing the event from firing. I don't see anything wrong with the code, so validation is the next most likely culprit.

<asp:CheckBox ID="CheckBox1" runat="server" CausesValidation="false" ... />

I think the postback is not firing due to the niceforms plugin you are using. It seems the plugin is overriding the default functionality of the checkbox.

To test if this is the case trying removing the class="niceform" attribute from your form tag.

Due to the fact that smartforms override the onclick event, as far as I can tell the only way to resolve this is to modify the source of the niceforms plugin by replacing the inputCheck function with the following. I have tested this and it worked for me.

function inputCheck(el) { //extend Checkboxes
    el.oldClassName = el.className;
    el.dummy = document.createElement('img');
    el.dummy.src = imagesPath + "0.png";
    if (el.checked) { el.dummy.className = "NFCheck NFh"; }
    else { el.dummy.className = "NFCheck"; }
    el.dummy.ref = el;
    if (isIE == false) { el.dummy.style.left = findPosX(el) + 'px'; el.dummy.style.top = findPosY(el) + 'px'; }
    else { el.dummy.style.left = findPosX(el) + 4 + 'px'; el.dummy.style.top = findPosY(el) + 4 + 'px'; }
    el.dummy.onclick = function () {
        //Set the checked state of the checkbox
        this.ref.checked = this.ref.checked ? false : true;
        //Fire the postback
        this.ref.click();
        if (!this.ref.checked) {
            this.ref.checked = true;
            this.className = "NFCheck NFh";
        }
        else {
            this.ref.checked = false;
            this.className = "NFCheck";
        }
    }
    el.onfocus = function () { this.dummy.className += " NFfocused"; }
    el.onblur = function () { this.dummy.className = this.dummy.className.replace(/ NFfocused/g, ""); }
    el.init = function () {
        this.parentNode.insertBefore(this.dummy, this);
        el.className = "NFhidden";
    }
    el.unload = function () {
        this.parentNode.removeChild(this.dummy);
        this.className = this.oldClassName;
    }
}

Hope this helps.

One suggestion is to store the checkbox in a tempcheck box and check if the tempcheck is true or false. If this checkbox is in a formedit, then you need a directcast and find control on the field. Protected Sub ckbCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Dim tempChk As CheckBox Try tempChk = DirectCast(YourFormView.FindControl("ckbCheckBox"), CheckBox) If tempChk.Checked = True Then 'do something Else do something End If

    Catch ex As Exception
        lblErrorMessage.Text = " Error occurred during ckbCheckBox.  Following are the errors: <br> " & ex.ToString
    End Try

End Sub

本文标签: caspnet checkbox event not firingStack Overflow