My main objective is to show a message during the server roundtrip of an ASP.Net WebForm. Within my WebForm, there are two buttons: an update button and a cancel button. The message should always appear if either the update button or the cancel button is clicked. When the update button is clicked, the TextBox must be validated to ensure it contains a value. Below is the code snippet I am working with:
<%@ 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>
If TextBox1
is empty and I click on ButtonValidated
, followed by ButtonJsUnvalidated
, it functions as intended.
If TextBox1
is empty and I click on ButtonJsValidated
, then press ButtonJsUnvalidated
, there is no server roundtrip. However, on clicking ButtonJsUnvalidated
again, it works properly.
Therefore, it seems like something goes wrong when clicking on ButtonJsValidated
.
I have searched for solutions related to revalidation upon clicking the same button again or resetting validators but have not been successful.
Does anyone have a solution?