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
("DerivedButton1", "", false, "",
"Default2.aspx", 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.
精彩评论