开发者

Issues creating an inherited button that can cancel a submit (ASP.NET)

开发者 https://www.devze.com 2023-01-15 07:30 出处:网络
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

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
func开发者_开发百科tion 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" />


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消