我需要在我们网站的表单中实现一些第三方字段验证。目前,我们的表单字段的验证是通过使用ASP.NET验证器(必需的,正则表达式)完成的,我想继续在我们不使用新验证的字段中使用这些验证器。为我们提供新验证服务的公司为我们提供了集成到我们网站的所有代码以获得新的验证功能,而基本实现只需要我们调用一个javascript方法来读取指定字段的值并通过验证它们来验证它们。一些服务电话。此外,实现要求表单最初设置onsubmit="return false;"
属性。
我遇到的问题是验证脚本试图提交表单时(在验证后函数删除表单的onsubmit属性之后),因为事件验证已打开(不是关闭的选项) ,该页面抛出异常“无效的回发或回调参数”。也许我提供了过多的细节来提供上下文,但也许问题归结为:如果启用事件验证而不会导致此错误,您是否可以使用javascript提交ASP.NET表单?如果是这样,怎么样?
答案 0 :(得分:1)
您可以在JavaScript函数中调用回发,如下所示:
<script type="text/javascript">
function DoValidation(parameter)
{
// Do calls to validation services here
// If all validation passes, then call this
if(valid) {
__doPostBack('btnSave', parameter)
}
}
</script>
<asp:Button id="btnSave" runat="server" OnClientClick="DoValidation()"
Text="Save" />
注意:btnSave
是一个组合名称,但它应该代表通常会回发的服务器端控件。
现在,在您的网页Page_Load
事件中,您可以确定发起回发的人以及随之发送的所有数据,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
// Get the control that caused the post back (btnSave)
Request["__EVENTTARGET"];
// Get the parameter value (parameter)
string parameter = Request["__EVENTARGUMENT"];
}
答案 1 :(得分:0)
是的,你可以。像这样的东西,请耐心等待,这只是伪代码。
<header>
<script type="text/javascript">
function JavaScriptSave()
{
// Validations here
if(passed)
{
__doPostBack('<%=MyButton.ClientID %>','OkToSave');
}
}
</script>
</header>
<asp:button id="MyButton" runat="server" Text="Submit Button" OnClientClick="JavaScriptSave()" />
然后在背面:
protected void Page_Load(object sender, EventArgs e)
{
string passedInValue = Request["__EVENTARGUMENT"];
if(passedInValue = "OkToSave")
{
// perform save function here
}
...
if(IsPostBack)
return;
}