ASP.Net 验证和 javascript 用于更新和取消按钮
ASP.Net validation and javascript for update- and cancel-button
我想要做的是在 ASP.Net WebForm 的服务器往返期间显示一条消息。在我的 WebForm 上,我有两个按钮,例如更新和取消按钮。
如果单击更新或取消按钮,则应始终显示该消息。
如果单击更新按钮,则应验证文本框中是否给出了值。
所以这是我的代码:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
Protected Sub ButtonValidated_Click(sender As Object, e As EventArgs)
Label1.Text = "ButtonValidated " & Now.ToLongTimeString & " " & TextBox1.Text
End Sub
Protected Sub ButtonUnvalidated_Click(sender As Object, e As EventArgs)
Label1.Text = "ButtonUnvalidated " & Now.ToLongTimeString & " " & TextBox1.Text
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Validators and JS</title>
<script type="text/javascript">
function ShowValMsg(e, val) {
dv = $get("ProgressDiv");
var r = true;
if (val != null) {
r = Page_ClientValidate(val);
if (r) {
dv = $get("ProgressDiv");
dv.style.visibility = "visible";
}
} else {
dv = $get("ProgressDiv");
dv.style.visibility = "visible";
}
return r;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="ValGrp" />
<br />
<!-- this could be the update button -->
<asp:Button ID="ButtonJsValidated" runat="server" Text="JsValidated" OnClick="ButtonValidated_Click" CausesValidation="false" OnClientClick="return ShowValMsg(this, "ValGrp");" />
<!-- this could be the cancel button -->
<asp:Button ID="ButtonJsUnvalidated" runat="server" Text="JsUnvalidated" OnClick="ButtonUnvalidated_Click" CausesValidation="false" OnClientClick="return ShowValMsg(this, null);" />
<br />
<asp:Button ID="ButtonValidated" runat="server" Text="Validated" OnClick="ButtonValidated_Click" CausesValidation="true" ValidationGroup="ValGrp" />
<asp:Button ID="ButtonUnvalidated" runat="server" Text="Unvalidated" OnClick="ButtonUnvalidated_Click" CausesValidation="false" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label" />
<div id="ProgressDiv" style="position:absolute;top:0px;left:300px;height:100px;width:100px;visibility:hidden;opacity: 1; z-index: auto" >Please wait ...</div>
</div>
</form>
</body>
</html>
如果 TextBox1
为空,我点击 ButtonValidated
,然后点击 ButtonJsUnvalidated
,它就如我所愿。
如果 TextBox1
为空,我单击 ButtonJsValidated
,然后单击 ButtonJsUnvalidated
,则没有服务器往返。如果我第二次点击 ButtonJsUnvalidated
它会起作用。
所以我认为,如果我点击 ButtonJsValidated
,就会出现问题。
我发现的所有内容都是关于再次单击一个按钮并再次验证或如何重置验证器的讨论(我也尝试过但没有成功)。
有人知道解决方案吗?
感谢 ppedv.de 的 Hannes 我找到了解决方案。我的 JS 代码现在看起来像这样:
function ShowValMsg(e, val, msg) {
if (msg == null) {
if (val != null) {
if (Page_ClientValidate(val)) {
Show(e);
}
return;
} else {
Show(e);
return;
}
} else {
if (val != null) {
if (Page_ClientValidate(val)) {
if (confirm(msg)) {
Show(e);
return;
} else {
return false;
}
}
} else {
if (confirm(msg)) {
Show(e);
return;
} else {
return false;
}
}
}
}
function Show(e) {
$get("ProgressDiv").style.visibility = "visible";
}
我想要做的是在 ASP.Net WebForm 的服务器往返期间显示一条消息。在我的 WebForm 上,我有两个按钮,例如更新和取消按钮。 如果单击更新或取消按钮,则应始终显示该消息。 如果单击更新按钮,则应验证文本框中是否给出了值。 所以这是我的代码:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
Protected Sub ButtonValidated_Click(sender As Object, e As EventArgs)
Label1.Text = "ButtonValidated " & Now.ToLongTimeString & " " & TextBox1.Text
End Sub
Protected Sub ButtonUnvalidated_Click(sender As Object, e As EventArgs)
Label1.Text = "ButtonUnvalidated " & Now.ToLongTimeString & " " & TextBox1.Text
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Validators and JS</title>
<script type="text/javascript">
function ShowValMsg(e, val) {
dv = $get("ProgressDiv");
var r = true;
if (val != null) {
r = Page_ClientValidate(val);
if (r) {
dv = $get("ProgressDiv");
dv.style.visibility = "visible";
}
} else {
dv = $get("ProgressDiv");
dv.style.visibility = "visible";
}
return r;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="ValGrp" />
<br />
<!-- this could be the update button -->
<asp:Button ID="ButtonJsValidated" runat="server" Text="JsValidated" OnClick="ButtonValidated_Click" CausesValidation="false" OnClientClick="return ShowValMsg(this, "ValGrp");" />
<!-- this could be the cancel button -->
<asp:Button ID="ButtonJsUnvalidated" runat="server" Text="JsUnvalidated" OnClick="ButtonUnvalidated_Click" CausesValidation="false" OnClientClick="return ShowValMsg(this, null);" />
<br />
<asp:Button ID="ButtonValidated" runat="server" Text="Validated" OnClick="ButtonValidated_Click" CausesValidation="true" ValidationGroup="ValGrp" />
<asp:Button ID="ButtonUnvalidated" runat="server" Text="Unvalidated" OnClick="ButtonUnvalidated_Click" CausesValidation="false" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label" />
<div id="ProgressDiv" style="position:absolute;top:0px;left:300px;height:100px;width:100px;visibility:hidden;opacity: 1; z-index: auto" >Please wait ...</div>
</div>
</form>
</body>
</html>
如果 TextBox1
为空,我点击 ButtonValidated
,然后点击 ButtonJsUnvalidated
,它就如我所愿。
如果 TextBox1
为空,我单击 ButtonJsValidated
,然后单击 ButtonJsUnvalidated
,则没有服务器往返。如果我第二次点击 ButtonJsUnvalidated
它会起作用。
所以我认为,如果我点击 ButtonJsValidated
,就会出现问题。
我发现的所有内容都是关于再次单击一个按钮并再次验证或如何重置验证器的讨论(我也尝试过但没有成功)。
有人知道解决方案吗?
感谢 ppedv.de 的 Hannes 我找到了解决方案。我的 JS 代码现在看起来像这样:
function ShowValMsg(e, val, msg) {
if (msg == null) {
if (val != null) {
if (Page_ClientValidate(val)) {
Show(e);
}
return;
} else {
Show(e);
return;
}
} else {
if (val != null) {
if (Page_ClientValidate(val)) {
if (confirm(msg)) {
Show(e);
return;
} else {
return false;
}
}
} else {
if (confirm(msg)) {
Show(e);
return;
} else {
return false;
}
}
}
}
function Show(e) {
$get("ProgressDiv").style.visibility = "visible";
}