You want to check on the client and on the server, since you cannot guarantee that the client has JavaScript enabled. Below are the steps for doing this on the client side, as server-side implementation of this should be trivial.
For a simple div, for example:
<div id="divErrors" runat="server" style="display: none;">
This should only appear when validation fails.
</div>
Add the following JavaScript to your page:
<script language="javascript" type="text/javascript">
function showErrors() {
if (!Page_IsValid) {
document.getElementById('divErrors').style.display = 'block';
} else {
document.getElementById('divErrors').style.display = 'none';
}
}
</script>
Finally, register a submit script that calls this new showErrors function (in the Page_Load event):
If Not Page.IsPostBack() Then
Dim scriptName As String = "OnSubmitScript"
Dim scriptType As Type = Me.GetType()
Dim script As ClientScriptManager = Page.ClientScript
If Not script.IsOnSubmitStatementRegistered(scriptType, scriptName) Then
script.RegisterOnSubmitStatement(scriptType, _
scriptName, "showErrors();")
End If
End If
source
share