.net 验证控件和javaScript的冲突问题。
2008-02-27 11:20
351 查看
在做东西的时候偶尔同时用到javascript的事件和.net的验证控件,具体细节如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>Untitled Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script language="javascript" type="text/javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var IsDetailsModifed = true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function SubmitDetailModified()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (IsDetailsModifed == true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = confirm("Do you want to save?);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("No need to save because of no field modified.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:TextBox ID="txtTest" runat="server" Width="80px"></asp:TextBox>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtTest"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ErrorMessage="Test must be Numeric" ValidationExpression="^[0-9]{1,28}.?[0-9]{0,4}$">*</asp:RegularExpressionValidator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return SubmitDetailModified()" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
上面的意图是,在点击按钮的时候弹出一个确认框,如果选择是则继续回传,否则阻止回传。在回传过程中如果某个字段的验证不通过,ValidationSummary 应该弹出消息框来阻止该回传.
但是事与愿违,当我单击按钮的时候,如果选择‘否’,阻止回传,正常。
但当我选择‘是’的时候,有两种情况:
1. 所有字段的验证都通过,然后点击‘是’,页面继续回传,一切正常。
2. 当有字段的验证不通过的时候,点击‘是’,验证消息不会弹出来,而且页面会继续回传,导致操作数据不正确。
为什么验证消息框会弹不出来呢?
后来,了解到.net验证控件的机制就是通过javascript来返回true或false来控制页面是否回传的。在上面的代码中,如果选择‘是’,直接在按钮的点击事件中返回true,页面当然要回传了。后来发现验证控件是通过给event.returnValue=true/false来实现阻止与否的。于是我改写了我的代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>Untitled Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script language="javascript" type="text/javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var IsDetailsModifed = true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function SubmitDetailModified()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (IsDetailsModifed == true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = confirm("Do you want to save ?");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("No need to save because of no field modified.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (retvalue == false)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event.returnValue = retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:TextBox ID="txtTest" runat="server" Width="80px"></asp:TextBox>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtTest"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ErrorMessage="Test must be Numeric" ValidationExpression="^[0-9]{1,28}.?[0-9]{0,4}$">*</asp:RegularExpressionValidator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="SubmitDetailModified()" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
如果我选择'是',我什么都不做,那就是默认的继续回传,但是如果有验证不通过的字段,验证控件通过event.returnValue=false来阻止页面继续回传,这样页面就会跳出验证不通过的对话框。如果我选择‘否’,我也用event.returnValue=false的方法来阻止页面回传。这样的话,就达到了最初的目的。
总结:
在使用.net的验证控件的时候,不要再事件中直接return true or false,而是通过event.returnValue=true/false来实现页面的回传阻止。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>Untitled Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script language="javascript" type="text/javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var IsDetailsModifed = true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function SubmitDetailModified()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (IsDetailsModifed == true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = confirm("Do you want to save?);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("No need to save because of no field modified.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:TextBox ID="txtTest" runat="server" Width="80px"></asp:TextBox>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtTest"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ErrorMessage="Test must be Numeric" ValidationExpression="^[0-9]{1,28}.?[0-9]{0,4}$">*</asp:RegularExpressionValidator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return SubmitDetailModified()" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
上面的意图是,在点击按钮的时候弹出一个确认框,如果选择是则继续回传,否则阻止回传。在回传过程中如果某个字段的验证不通过,ValidationSummary 应该弹出消息框来阻止该回传.
但是事与愿违,当我单击按钮的时候,如果选择‘否’,阻止回传,正常。
但当我选择‘是’的时候,有两种情况:
1. 所有字段的验证都通过,然后点击‘是’,页面继续回传,一切正常。
2. 当有字段的验证不通过的时候,点击‘是’,验证消息不会弹出来,而且页面会继续回传,导致操作数据不正确。
为什么验证消息框会弹不出来呢?
后来,了解到.net验证控件的机制就是通过javascript来返回true或false来控制页面是否回传的。在上面的代码中,如果选择‘是’,直接在按钮的点击事件中返回true,页面当然要回传了。后来发现验证控件是通过给event.returnValue=true/false来实现阻止与否的。于是我改写了我的代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>Untitled Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script language="javascript" type="text/javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var IsDetailsModifed = true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function SubmitDetailModified()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (IsDetailsModifed == true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = confirm("Do you want to save ?");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("No need to save because of no field modified.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retvalue = false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (retvalue == false)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event.returnValue = retvalue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:TextBox ID="txtTest" runat="server" Width="80px"></asp:TextBox>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtTest"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ErrorMessage="Test must be Numeric" ValidationExpression="^[0-9]{1,28}.?[0-9]{0,4}$">*</asp:RegularExpressionValidator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="SubmitDetailModified()" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
如果我选择'是',我什么都不做,那就是默认的继续回传,但是如果有验证不通过的字段,验证控件通过event.returnValue=false来阻止页面继续回传,这样页面就会跳出验证不通过的对话框。如果我选择‘否’,我也用event.returnValue=false的方法来阻止页面回传。这样的话,就达到了最初的目的。
总结:
在使用.net的验证控件的时候,不要再事件中直接return true or false,而是通过event.returnValue=true/false来实现页面的回传阻止。
相关文章推荐
- .net 验证控件和javaScript的冲突问题
- 服务端控件验证与自己写的javascript验证函数同时存在时出现的冲突问题(RequiredFieldValidator&JS)
- 解决 asp.net onClientClick 与 验证控件冲突问题
- Button控件的OnClientClick与验证控件冲突的问题
- 验证控件和javascript冲突的解决办法
- 解决ASP.NET1.1, ASP.NET2.0,ASP.NET3.5中验证控件和自定义弹出确认窗口的冲突问题
- ASP.NET中关于验证控件和自定义弹出确认窗口的冲突问题
- ASP.NET中关于验证控件和自定义弹出确认窗口的冲突问题
- 验证控件和js脚本验证冲突问题
- 遇到.net加了验证控件的表单无法提交的问题
- .NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突
- 同一页面中onclientclick与验证控件冲突问题
- 验证控件和javascript冲突的解决办法
- asp.net 确认对话框与数据验证控件的冲突问题
- 使用javascript 实现.net 验证控件功能
- 验证控件和javascript冲突的解决办法
- 通过Page_ClientValidate解决验证控件和confirm冲突的问题
- onClientClick=''return confirm与验证控件冲突问题
- Fckeditor在.net下用验证控件问题
- onClientClick=''return confirm与验证控件冲突问题