asp.net checkbox event not firing
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;
prePracticeCode开发者_运维问答TextBox.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?
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
精彩评论