RequiredField Validator + OnClientClick


This artical i'm trying to explain RequiredField Validator and confirmation message issues. For ex if we want to check requiredfieldvalidator and confirmation message at same time then first it executes client side script then only it's executed requiredfieldvalidation. Here i'm trying to explain how to overcome this type of issues in our applications.

RequiredFieldValidator + OnClientClick



In this artical i'm trying to explain about requiredfield validator and onclientclick method functionalities.

If we want to check required field validator and display confirmation message both are we perform in same event, this case Client side scripting is executed first rather than server side.

EX:

Javascript:



<script type="text/javascript" language="javascript">
function ConfirmToSave() {
var agree = confirm("Are you sure Quantity Not Applicable...?");
if (agree)
return true;
else
return false;
}
</script>

SourceCode:




<asp:Label ID="lblReason" runat="server" Text="Reason : " Enabled="false"/>
<asp:TextBox ID="txtReason" runat="server" Enabled="false"
Height="40px" TextMode="MultiLine" ValidationGroup="Check"/>
<asp:RequiredFieldValidator ID="req" runat="server" ControlToValidate="txtReason" ErrorMessage="*" ForeColor="Red" ValidationGroup="Check"></asp:RequiredFieldValidator>
<asp:Button Id="btnAdd" Text="Add" runat="server" OnClientClick="return ConfirmToSave();" OnClick="btnAdd_Click" Enabled="false" ValidationGroup="Check"/>



If we wrote code like this first it's asking the confirmation message if we choose confirm then only validation part raises.

ClientScript raises first

This is not exact output, we need to check requiredfieldvalidation first and then we need to confirmation. So, for that i implement my javascript code something like below. If we wrote javascript like this here first it's executed requiredfieldvalidator after that it's executed Confirmation message.

EX:

JavascriptCode:




<script type="text/javascript" language="javascript">
function ConfirmToSave() {
if (!Page_ClientValidate("Check")) {
return false;
}
else {
var agree = confirm("Are you sure Quantity Not Applicable...?");
if (agree)
return true;
else
return false;
}
}
</script>

Source Code:




<asp:Label ID="lblReason" runat="server" Text="Reason : " Enabled="false"/>
<asp:TextBox ID="txtReason" runat="server" Enabled="false"
Height="40px" TextMode="MultiLine" ValidationGroup="Check"/>
<asp:RequiredFieldValidator ID="req" runat="server" ControlToValidate="txtReason" ErrorMessage="*" ForeColor="Red" ValidationGroup="Check"></asp:RequiredFieldValidator>
<asp:Button Id="btnAdd" Text="Add" runat="server" OnClientClick="return ConfirmToSave();" OnClick="btnAdd_Click" Enabled="false" ValidationGroup="Check"/>


Here, in javascript first it executes requiredfieldvalidator based on validationgroup value.



Using this we achieve our desired output.

Validation executed first

Confirmation


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: