创建可以取消提交的继承按钮时出现问题 (ASP.NET)
我的 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
("DerivedButton1", "", false, "",
"Default2.aspx", 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将您的
OnClientClick
更改为如下所示:这应该可行,尽管我怀疑可能有一种更优雅的方式来完成您想要的操作。
Try changing your
OnClientClick
to something like this:This should work, although I suspect there's probably a more elegant way to do what you want.