解決Button的OnClientClick與RequiredFieldValidator的問題
今天幫小舖網友解了有關這方面的問題...問題內容如下:
Button 如果有設定OnClientClick EX return confirm('請再次確認金額')
那麼驗證RequiredFieldValidator的部份
就會失效
有沒有 方法
按下Button
先驗證 再去執行OnClientClick
解決方法有兩種:
1.ButtonConfirm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ButtonConfirm1.aspx.cs" Inherits="ButtonConfirm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ButtonConfirm</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="不可為空白"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="送出" OnClientClick="return ConfirmMe()" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
function ConfirmMe()
{
if(Page_ClientValidate())
{
return confirm('確定送出嗎?');
}
else
{
return false;
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ButtonConfirm</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="不可為空白"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="送出" OnClientClick="return ConfirmMe()" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
function ConfirmMe()
{
if(Page_ClientValidate())
{
return confirm('確定送出嗎?');
}
else
{
return false;
}
}
</script>
2.ButtonConfirm2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ButtonConfirm2.aspx.cs" Inherits="ButtonConfirm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server" onsubmit="if (Page_ValidationActive && !confirm('確定送出嗎?')) return false;">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="不可為空白"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="送出" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server" onsubmit="if (Page_ValidationActive && !confirm('確定送出嗎?')) return false;">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="不可為空白"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="送出" />
</div>
</form>
</body>
</html>
執行結果:
參考網址:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD200803261016349KT&fumcde=FUM20041006161839LRJ
http://www.cnblogs.com/firstyi/archive/2006/12/26/604216.html
http://forums.asp.net/t/1058284.aspx