创建可以取消提交的继承按钮时出现问题 (ASP.NET)

发布于 2024-09-19 02:26:54 字数 1239 浏览 5 评论 0原文

我的 Javascript 没有错误。 Javascript 返回 false,并且提交表单。此外,当将 UseSubmitBehavior 设置为 false 时,表单仍会提交。我缺少什么来阻止表单提交?

// DerivedButton.cs
public class DerivedButton : System.Web.UI.WebControls.Button
{
  public DerivedButton2()
    : base()
  {
    this.OnClientClick = "DerivedButton_OnClick()";
    //this.UseSubmitBehavior = false;
  }

  protected override void OnPreRender(EventArgs e)
  {
    base.OnPreRender(e);
    string resourceName = "MyControls.DerivedButton.js";

    ClientScriptManager scriptManager = this.Page.ClientScript;
    scriptManager.RegisterClientScriptResource(
      typeof(MyControls.DerivedButton), 
      resourceName);
  }
}


//DerivedButton.js
function DerivedButton_OnClick()
{
  var answer = confirm("Are you sure?");
  return answer;
}

//Output Html
<input type="submit" 
       name="DerivedButton1" 
       value="SomeButton" 
       onclick="DerivedButton_OnClick();
         WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions                     
           (&quot;DerivedButton1&quot;, &quot;&quot;, false, &quot;&quot;,
            &quot;Default2.aspx&quot;, false, false))"
       id="DerivedButton1" />

There are no errors with my Javascript. The Javascript returns false, and the form is submitted. Additionally when setting UseSubmitBehavior to false the form still submits. What am I missing to prevent the form from being submitted?

// DerivedButton.cs
public class DerivedButton : System.Web.UI.WebControls.Button
{
  public DerivedButton2()
    : base()
  {
    this.OnClientClick = "DerivedButton_OnClick()";
    //this.UseSubmitBehavior = false;
  }

  protected override void OnPreRender(EventArgs e)
  {
    base.OnPreRender(e);
    string resourceName = "MyControls.DerivedButton.js";

    ClientScriptManager scriptManager = this.Page.ClientScript;
    scriptManager.RegisterClientScriptResource(
      typeof(MyControls.DerivedButton), 
      resourceName);
  }
}


//DerivedButton.js
function DerivedButton_OnClick()
{
  var answer = confirm("Are you sure?");
  return answer;
}

//Output Html
<input type="submit" 
       name="DerivedButton1" 
       value="SomeButton" 
       onclick="DerivedButton_OnClick();
         WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions                     
           ("DerivedButton1", "", false, "",
            "Default2.aspx", false, false))"
       id="DerivedButton1" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻安 2024-09-26 02:26:55

尝试将您的 OnClientClick 更改为如下所示:

this.OnClientClick = "if (!DerivedButton_OnClick()) return false";

这应该可行,尽管我怀疑可能有一种更优雅的方式来完成您想要的操作。

Try changing your OnClientClick to something like this:

this.OnClientClick = "if (!DerivedButton_OnClick()) return false";

This should work, although I suspect there's probably a more elegant way to do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文