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" />