3

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 complete code:

http://pastebin.com/amQURr91

How can I get it to fire the event?

6
  • the problem might be with the viewstate or the postback event. Commented Sep 14, 2011 at 18:38
  • @vinay how do i change viewstate? what should i do wtih it 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? Commented Sep 14, 2011 at 19:24
  • @onof yes i did check and there are no errors 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 Commented Sep 14, 2011 at 19:29

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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" ... />

4 Comments

required field validators and the like.
What version of .NET are you using?
Are any of the events on the page firing?
1

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.

2 Comments

@jdavis im sorry but can you show me how i would incorporate this into my code
No worries, glad to help. With regards to what's changed. I removed the onclick override (which didn't seem to have any adverse effect) and added the commented lines of code (see: //Set the checked state of the checkbox and //Fire the postback).
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.