在做东西的时候偶尔同时用到javascript的事件和.net的验证控件,具体细节如下:
上面的意图是,在点击按钮的时候弹出一个确认框,如果选择是则继续回传,否则阻止回传。在回传过程中如果某个字段的验证不通过,ValidationSummary 应该弹出消息框来阻止该回传.
但是事与愿违,当我单击按钮的时候,如果选择‘否’,阻止回传,正常。
但当我选择‘是’的时候,有两种情况:
1. 所有字段的验证都通过,然后点击‘是’,页面继续回传,一切正常。
2. 当有字段的验证不通过的时候,点击‘是’,验证消息不会弹出来,而且页面会继续回传,导致操作数据不正确。
为什么验证消息框会弹不出来呢?
后来,了解到.net验证控件的机制就是通过javascript来返回true或false来控制页面是否回传的。在上面的代码中,如果选择‘是’,直接在按钮的点击事件中返回true,页面当然要回传了。后来发现验证控件是通过给event.returnValue=true/false来实现阻止与否的。于是我改写了我的代码如下:
如果我选择'是',我什么都不做,那就是默认的继续回传,但是如果有验证不通过的字段,验证控件通过event.returnValue=false来阻止页面继续回传,这样页面就会跳出验证不通过的对话框。如果我选择‘否’,我也用event.returnValue=false的方法来阻止页面回传。这样的话,就达到了最初的目的。
总结:
在使用.net的验证控件的时候,不要再事件中直接return true or false,而是通过event.returnValue=true/false来实现页面的回传阻止。
<
head runat
=
"
server
"
>
< title > Untitled Page </ title >
< script language = " javascript " type = " text/javascript " >
var IsDetailsModifed = true
function SubmitDetailModified()
{
var retvalue;
if (IsDetailsModifed == true)
{
retvalue = confirm("Do you want to save?);
}
else
{
alert("No need to save because of no field modified.");
retvalue = false
}
return retvalue;
}
</ script >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:TextBox ID = " txtTest " runat = " server " Width = " 80px " ></ asp:TextBox >
< asp:RegularExpressionValidator ID = " RegularExpressionValidator4 " runat = " server " ControlToValidate = " txtTest "
ErrorMessage = " Test must be Numeric " ValidationExpression = " ^[0-9]{1,28}.?[0-9]{0,4}$ " >*</ asp:RegularExpressionValidator >
< asp:Button ID = " Button1 " runat = " server " Text = " Button " OnClientClick = " return SubmitDetailModified() " />
< asp:ValidationSummary ID = " ValidationSummary1 " runat = " server " ShowMessageBox = " true " ShowSummary = " false " />
</ div >
</ form >
</ body >
< title > Untitled Page </ title >
< script language = " javascript " type = " text/javascript " >
var IsDetailsModifed = true
function SubmitDetailModified()
{
var retvalue;
if (IsDetailsModifed == true)
{
retvalue = confirm("Do you want to save?);
}
else
{
alert("No need to save because of no field modified.");
retvalue = false
}
return retvalue;
}
</ script >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:TextBox ID = " txtTest " runat = " server " Width = " 80px " ></ asp:TextBox >
< asp:RegularExpressionValidator ID = " RegularExpressionValidator4 " runat = " server " ControlToValidate = " txtTest "
ErrorMessage = " Test must be Numeric " ValidationExpression = " ^[0-9]{1,28}.?[0-9]{0,4}$ " >*</ asp:RegularExpressionValidator >
< asp:Button ID = " Button1 " runat = " server " Text = " Button " OnClientClick = " return SubmitDetailModified() " />
< asp:ValidationSummary ID = " ValidationSummary1 " runat = " server " ShowMessageBox = " true " ShowSummary = " false " />
</ div >
</ form >
</ body >
上面的意图是,在点击按钮的时候弹出一个确认框,如果选择是则继续回传,否则阻止回传。在回传过程中如果某个字段的验证不通过,ValidationSummary 应该弹出消息框来阻止该回传.
但是事与愿违,当我单击按钮的时候,如果选择‘否’,阻止回传,正常。
但当我选择‘是’的时候,有两种情况:
1. 所有字段的验证都通过,然后点击‘是’,页面继续回传,一切正常。
2. 当有字段的验证不通过的时候,点击‘是’,验证消息不会弹出来,而且页面会继续回传,导致操作数据不正确。
为什么验证消息框会弹不出来呢?
后来,了解到.net验证控件的机制就是通过javascript来返回true或false来控制页面是否回传的。在上面的代码中,如果选择‘是’,直接在按钮的点击事件中返回true,页面当然要回传了。后来发现验证控件是通过给event.returnValue=true/false来实现阻止与否的。于是我改写了我的代码如下:
<
head runat
=
"
server
"
>
< title > Untitled Page </ title >
< script language = " javascript " type = " text/javascript " >
var IsDetailsModifed = true
function SubmitDetailModified()
{
var retvalue;
if (IsDetailsModifed == true)
{
retvalue = confirm("Do you want to save ?");
}
else
{
alert("No need to save because of no field modified.");
retvalue = false
}
if (retvalue == false)
{
event.returnValue = retvalue;
}
}
</ script >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:TextBox ID = " txtTest " runat = " server " Width = " 80px " ></ asp:TextBox >
< asp:RegularExpressionValidator ID = " RegularExpressionValidator4 " runat = " server " ControlToValidate = " txtTest "
ErrorMessage = " Test must be Numeric " ValidationExpression = " ^[0-9]{1,28}.?[0-9]{0,4}$ " >*</ asp:RegularExpressionValidator >
< asp:Button ID = " Button1 " runat = " server " Text = " Button " OnClientClick = " SubmitDetailModified() " />
< asp:ValidationSummary ID = " ValidationSummary1 " runat = " server " ShowMessageBox = " true " ShowSummary = " false " />
</ div >
</ form >
</ body >
< title > Untitled Page </ title >
< script language = " javascript " type = " text/javascript " >
var IsDetailsModifed = true
function SubmitDetailModified()
{
var retvalue;
if (IsDetailsModifed == true)
{
retvalue = confirm("Do you want to save ?");
}
else
{
alert("No need to save because of no field modified.");
retvalue = false
}
if (retvalue == false)
{
event.returnValue = retvalue;
}
}
</ script >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:TextBox ID = " txtTest " runat = " server " Width = " 80px " ></ asp:TextBox >
< asp:RegularExpressionValidator ID = " RegularExpressionValidator4 " runat = " server " ControlToValidate = " txtTest "
ErrorMessage = " Test must be Numeric " ValidationExpression = " ^[0-9]{1,28}.?[0-9]{0,4}$ " >*</ asp:RegularExpressionValidator >
< asp:Button ID = " Button1 " runat = " server " Text = " Button " OnClientClick = " SubmitDetailModified() " />
< asp:ValidationSummary ID = " ValidationSummary1 " runat = " server " ShowMessageBox = " true " ShowSummary = " false " />
</ div >
</ form >
</ body >
如果我选择'是',我什么都不做,那就是默认的继续回传,但是如果有验证不通过的字段,验证控件通过event.returnValue=false来阻止页面继续回传,这样页面就会跳出验证不通过的对话框。如果我选择‘否’,我也用event.returnValue=false的方法来阻止页面回传。这样的话,就达到了最初的目的。
总结:
在使用.net的验证控件的时候,不要再事件中直接return true or false,而是通过event.returnValue=true/false来实现页面的回传阻止。