ASP.NET 复选框事件未触发

3

我在一个表单上有一个复选框:

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

当我选中或取消勾选时,它并未触发下面的事件。
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    if (CheckBox1.Checked)
    {
        preContactTextBox.Visible = true;
        prePracticeCodeTextBox.Visible = true;            
    }
    else
    {
        preContactTextBox.Visible = false;
        prePracticeCodeTextBox.Visible = false;            
    }
}

它根本没有进入这个事件。我做错了什么?

这是完整的代码:

http://pastebin.com/amQURr91

我该如何让它触发事件?


问题可能出在视图状态或回发事件上。 - Vinay
@vinay 我该如何更改视图状态?我应该对它做什么? - Alex Gordon
你有没有用FireBug或其他JavaScript调试器检查页面中是否有JavaScript错误? - onof
@onof 是的,我已经检查过了,没有错误。 - Alex Gordon
这是浏览器解释后代码的样子:<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(&#39;CheckBox1&#39;,&#39;&#39;)', 0)" /></span></dd> 206 </dl> 207 - Alex Gordon
@onof请加入我们的会议 http://chat.stackoverflow.com/rooms/3451/discussion-between-i-and-james-johnson - Alex Gordon
4个回答

4

只有当复选框的 AutoPostBack 属性被指定为 "true" 时,Checkbox 控件的 CheckedChanged 事件才会触发。


2

我建议您检查一下是否存在验证机制阻止了事件的触发。我认为代码本身没有问题,因此验证可能是导致问题的最有可能的原因。

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

必填字段验证器及其类似功能。 - James Johnson
你使用的是哪个 .NET 版本? - James Johnson
页面上的任何事件都在触发吗? - James Johnson
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/3451/discussion-between-i-and-james-johnson - Alex Gordon

1

我认为由于您使用的niceforms插件,导致postback未触发。看起来该插件正在覆盖复选框的默认功能。

为了测试是否是这种情况,请尝试从表单标记中删除class="niceform"属性。

由于smartforms覆盖了onclick事件,据我所知,解决此问题的唯一方法是通过替换inputCheck函数来修改niceforms插件的源代码。我已经测试过了,对我有效。

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;
    }
}

希望这有所帮助。

@jdavis,对不起,你能告诉我如何把这个东西加入我的代码中吗? - Alex Gordon
没问题,很高兴能帮忙。关于更改的内容,我移除了onclick覆盖(似乎没有任何不良影响),并添加了注释代码行(请参见://设置复选框的选中状态//触发postback)。 - jdavies

1
一个建议是将复选框存储在tempcheck框中,并检查tempcheck是true还是false。 如果此复选框位于formedit中,则需要对字段进行directcast和查找控件。 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 '做某事 Else 做某事 End If
    Catch ex As Exception
        lblErrorMessage.Text = " Error occurred during ckbCheckBox.  Following are the errors: <br> " & ex.ToString
    End Try

End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接