【问题标题】:asp.net checkbox event not firingasp.net 复选框事件未触发
【发布时间】:2011-11-17 06:39:42
【问题描述】:

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

<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 我如何更改视图状态?我该怎么办呢
  • 如果页面中有 javascript 错误,您是否使用 FireBug 或其他一些 javascript 调试器进行检查?
  • @onof 是的,我确实检查过,没有错误
  • 这是浏览器解释后代码的样子
    204
    205
    206
    207

标签: c# javascript asp.net html


【解决方案1】:

仅当复选框的 AutoPostBack 属性指定为“true”时,才会引发 Checkbox 控件的 CheckedChanged 事件。

【讨论】:

    【解决方案2】:

    一个建议是将复选框存储在 tempcheck 框中,并检查 tempcheck 是 true 还是 false。 如果此复选框在 formit 中,则您需要直接投射并在该字段上找到控件。 Protected Sub ckbCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) 将 tempChk 调暗为复选框 尝试 tempChk = DirectCast(YourFormView.FindControl("ckbCheckBox"), CheckBox) 如果 tempChk.Checked = True 那么 '做一点事 别的 做一点事 结束如果

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

    【讨论】:

      【解决方案3】:

      我认为由于您使用的 niceforms 插件,回发没有触发。该插件似乎覆盖了复选框的默认功能。

      要测试是否是这种情况,请尝试从表单标签中删除 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 很抱歉,但你能告诉我如何将它合并到我的代码中
      • 不用担心,很高兴为您提供帮助。至于发生了什么变化。我删除了 onclick 覆盖(似乎没有任何不利影响)并添加了注释的代码行(参见://Set the checked state of the checkbox//Fire the postback)。
      【解决方案4】:

      我会检查以确保您在某处没有阻止事件触发的验证。我看不出代码有什么问题,所以验证是下一个最有可能的罪魁祸首。

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-09
      • 2016-04-13
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      相关资源
      最近更新 更多