Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Mystery: Validators don't always validate ? Can unrelated Javascriptstop them ?

Reply
Thread Tools

Mystery: Validators don't always validate ? Can unrelated Javascriptstop them ?

 
 
Radu
Guest
Posts: n/a
 
      02-08-2008
Hi. I'm struggling to understand what's happening here:

I have the following HTML:

<script language="javascript" type="text/javascript">
function ShowConfirm1()
{
var varMessage='Would you also like to add an \"Action Taken\" on
this PIN ?'
var varAnswer = confirm(varMessage)
if (varAnswer == true)
{
document.getElementById('HiddenField1').value="1";
return true;
}
else
{
document.getElementById('HiddenField1').value="0";
return true;
}
}
</script>

<asp:GridView
ID="GridView1"
DataSourceID="SqlDataSource1"
DataKeyNames="ID"
AutoGenerateColumns="False"
ShowFooter="True"
OnPreRender="GridView1_Prerender"
OnRowUpdating="GridView1_OnRowUpdating"
OnRowUpdated="GridView1_OnRowUpdated"
OnRowDeleting="GridView1_OnRowDeleting"
OnRowDeleted="GridView1_OnRowDeleted"
Runat="server"
>

<Columns>
<asp:TemplateField HeaderText="Employee PIN">
<ItemTemplate>
<asp:Label ID="EmployeePINLabel" Runat="Server"><
%#Eval("EmployeePIN")%></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="EmployeePINTextBoxAdd" Columns="6"
OnTextChanged="EmployeePINChanged_Add" Runat="Server"></asp:TextBox>
<asp:RequiredFieldValidator
id="reqEmployeePINAdd"
ValidationGroup="Footer-Add"
ControlToValidate="EmployeePINTextBoxAdd"
Display="Dynamic"
Text="*"
ErrorMessage="The employee's PIN# is required !"
runat="server">
</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator
id="regexEmployeePINAdd"
ValidationGroup="Footer-Add"
ControlToValidate="EmployeePINTextBoxAdd"
Display="Dynamic"
ValidationExpression="\d{6}"
Text="*"
ErrorMessage="The PIN entered is not correct !"
runat="server">
</asp:RegularExpressionValidator>

<asp:CompareValidator
id="compareEmployeePINAdd"
ValidationGroup="Footer-Add"
ControlToValidate
="EmployeePINTextBoxAdd"
ValueToCompare="100000"
Operator="GreaterThan"
Display="Dynamic"
Text="*"
ErrorMessage="The PIN entered is not
correct !"
runat="server">
</asp:CompareValidator>
</FooterTemplate>

<EditItemTemplate>
<asp:TextBox ID="EmployeePINTextBoxEdit" Columns="6"
OnTextChanged="EmployeePINChanged_Edit" Text='<%# Eval("EmployeePIN")
%>' Runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
id="reqEmployeePINEdit"
ValidationGroup="Row-Edit"
ControlToValidate="EmployeePINTextBoxEdit"
Display="Dynamic"
Text="*"
ErrorMessage="The employee's PIN# is required !"
runat="server">
</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator
id="regexEmployeePINEdit"
ValidationGroup="Row-Edit"
ControlToValidate="EmployeePINTextBoxEdit"
Display="Dynamic"
ValidationExpression="\d{6}"
Text="*"
ErrorMessage="The PIN entered is not correct !"
runat="server">
</asp:RegularExpressionValidator>

<asp:CompareValidator
id="compareEmployeePINEdit"
ValidationGroup="Row-Edit"
ControlToValidate
="EmployeePINTextBoxEdit"
ValueToCompare="100000"
Operator="GreaterThan"
Display="Dynamic"
Text="*"
ErrorMessage="The PIN entered is not
correct !"
runat="server">
</asp:CompareValidator>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center"/>
</asp:TemplateField>
.....
.....
<asp:TemplateField HeaderText="Edit Record">
<ItemTemplate>
<asp:LinkButton
ID = "linkEdit"
ForeColor="darkBlue"
runat = "server"
CausesValidation ="false"
CommandName = "Edit"
text = "Edit"
>

</asp:LinkButton>
</ItemTemplate>

<EditItemTemplate>
<asp:LinkButton
ID = "linkUpdate"
ForeColor="darkBlue"
runat = "server"
CausesValidation ="true"
CommandName = "Update"
text = "Update"
>

</asp:LinkButton>

<asp:LinkButton
ID = "linkCancel"
ForeColor="darkBlue"
runat = "server"
CausesValidation ="false"
CommandName = "Cancel"
text = "Cancel"
>

</asp:LinkButton>
</EditItemTemplate>

<FooterTemplate>
<asp:Button
ID="cmdAdd"
Font-Size="Smaller"
Text="Save"
ValidationGroup="Footer-Add"
CausesValidation="true"
Runat="Server"
OnClientClick="javascript:return
ShowConfirm1()"
OnClick="cmdAdd_OnClick"
/>
</FooterTemplate>
.....
.....
</asp:GridView>

<asp:ValidationSummary
id="validSummaryAdd"
ValidationGroup="Footer-Add"
ShowSummary="false"
ShowMessageBox="true"
HeaderText="The following things have to be fixed (Record -
Add)"
runat="server"
/>
<asp:ValidationSummary
id="validSummaryEdit"
ValidationGroup="Row-Edit"
HeaderText="The following things have to be fixed (Record -
Edit)"
ShowSummary="false"
ShowMessageBox="true"
runat="server"
/>

....
....

Then I have the code:
protected void cmdAdd_OnClick(object sender, EventArgs e) //Handles
cmdAdd.Click
{
//Add the record to the Watchlist table, and maybe also add a
record to the Comments table...
Util_AddRecord();
.....
.....
}


and

protected void Util_AddRecord()
//Called by cmdAdd_OnClick
{
....
....
bool boolAddCommentsAsWell;

//Ask the user if he wants to also add a record in the "Comments"
table:
if (HiddenField1.Value.Equals("1"))
//User clicked okay
boolAddCommentsAsWell = true;
else
//User clicked cancel
boolAddCommentsAsWell = false;

//Create a one-to-one reference between this record and the record
in Comments if
//strCommentsID not null:
string strCommentsID;
if (boolAddCommentsAsWell == true)
strCommentsID = "W " + lblID.Text;
else
strCommentsID = "";

SqlDataSource1.InsertParameters["EmployeePIN"].DefaultValue =
txtEmployeePIN.Text;
....
SqlDataSource1.InsertParameters["CommentsID"].DefaultValue =
strCommentsID;

SqlDataSource1.Insert();
....
....
}

The problem is the following:

- if I delete the HTML line

OnClientClick="javascript:return ShowConfirm1()"

and therefore skip asking the question, and if I change

if (HiddenField1.Value.Equals("1"))
//User clicked okay
boolAddCommentsAsWell = true;
else
//User clicked cancel
boolAddCommentsAsWell = false;

to
boolAddCommentsAsWell = true;

then the validators validate, and the code SqlDataSource1.Insert()
executes properly.

- if I run the code as it is, with the confirmation script and the
if (HiddenField1.... lines, the validators DON'T VALIDATE, so
therefore SqlDataSource1.Insert() inserts whatever I have entered in
the gridview. Of course, if I skip entering some cells, it will
attempt to insert NULLS, which could fail, depending on the fields.

Therefore, my question is - why don't the validators validate when I
have an extra javascript in the code ? I would have expected to have
the validators validate BEFORE the cmdAdd_OnClick starts executing...
Besides, the Javascript (ShowConfirm1) has nothing to do with them....

Thanks a lot
Alex.
 
Reply With Quote
 
 
 
 
Misbah Arefin
Guest
Posts: n/a
 
      02-08-2008
http://msdn2.microsoft.com/en-us/library/yb52a4x0.aspx
If client-side validation is enabled, the page includes references to script
libraries that are used to perform the client-side validation. The page
includes a client-side method to intercept and handle the Click event before
the page is submitted.

If you specify a onclientlcick method then you override the default click
event (which fires the validators). You need to include the validator
event/func in your custom onclientclick event handler

--
Misbah Arefin


"Radu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi. I'm struggling to understand what's happening here:
>
> I have the following HTML:
>
> <script language="javascript" type="text/javascript">
> function ShowConfirm1()
> {
> var varMessage='Would you also like to add an \"Action Taken\" on
> this PIN ?'
> var varAnswer = confirm(varMessage)
> if (varAnswer == true)
> {
> document.getElementById('HiddenField1').value="1";
> return true;
> }
> else
> {
> document.getElementById('HiddenField1').value="0";
> return true;
> }
> }
> </script>
>
> <asp:GridView
> ID="GridView1"
> DataSourceID="SqlDataSource1"
> DataKeyNames="ID"
> AutoGenerateColumns="False"
> ShowFooter="True"
> OnPreRender="GridView1_Prerender"
> OnRowUpdating="GridView1_OnRowUpdating"
> OnRowUpdated="GridView1_OnRowUpdated"
> OnRowDeleting="GridView1_OnRowDeleting"
> OnRowDeleted="GridView1_OnRowDeleted"
> Runat="server"
> >

> <Columns>
> <asp:TemplateField HeaderText="Employee PIN">
> <ItemTemplate>
> <asp:Label ID="EmployeePINLabel" Runat="Server"><
> %#Eval("EmployeePIN")%></asp:Label>
> </ItemTemplate>
> <FooterTemplate>
> <asp:TextBox ID="EmployeePINTextBoxAdd" Columns="6"
> OnTextChanged="EmployeePINChanged_Add" Runat="Server"></asp:TextBox>
> <asp:RequiredFieldValidator
> id="reqEmployeePINAdd"
> ValidationGroup="Footer-Add"
> ControlToValidate="EmployeePINTextBoxAdd"
> Display="Dynamic"
> Text="*"
> ErrorMessage="The employee's PIN# is required !"
> runat="server">
> </asp:RequiredFieldValidator>
>
> <asp:RegularExpressionValidator
> id="regexEmployeePINAdd"
> ValidationGroup="Footer-Add"
> ControlToValidate="EmployeePINTextBoxAdd"
> Display="Dynamic"
> ValidationExpression="\d{6}"
> Text="*"
> ErrorMessage="The PIN entered is not correct !"
> runat="server">
> </asp:RegularExpressionValidator>
>
> <asp:CompareValidator
> id="compareEmployeePINAdd"
> ValidationGroup="Footer-Add"
> ControlToValidate
> ="EmployeePINTextBoxAdd"
> ValueToCompare="100000"
> Operator="GreaterThan"
> Display="Dynamic"
> Text="*"
> ErrorMessage="The PIN entered is not
> correct !"
> runat="server">
> </asp:CompareValidator>
> </FooterTemplate>
>
> <EditItemTemplate>
> <asp:TextBox ID="EmployeePINTextBoxEdit" Columns="6"
> OnTextChanged="EmployeePINChanged_Edit" Text='<%# Eval("EmployeePIN")
> %>' Runat="server"></asp:TextBox>
> <asp:RequiredFieldValidator
> id="reqEmployeePINEdit"
> ValidationGroup="Row-Edit"
> ControlToValidate="EmployeePINTextBoxEdit"
> Display="Dynamic"
> Text="*"
> ErrorMessage="The employee's PIN# is required !"
> runat="server">
> </asp:RequiredFieldValidator>
>
> <asp:RegularExpressionValidator
> id="regexEmployeePINEdit"
> ValidationGroup="Row-Edit"
> ControlToValidate="EmployeePINTextBoxEdit"
> Display="Dynamic"
> ValidationExpression="\d{6}"
> Text="*"
> ErrorMessage="The PIN entered is not correct !"
> runat="server">
> </asp:RegularExpressionValidator>
>
> <asp:CompareValidator
> id="compareEmployeePINEdit"
> ValidationGroup="Row-Edit"
> ControlToValidate
> ="EmployeePINTextBoxEdit"
> ValueToCompare="100000"
> Operator="GreaterThan"
> Display="Dynamic"
> Text="*"
> ErrorMessage="The PIN entered is not
> correct !"
> runat="server">
> </asp:CompareValidator>
> </EditItemTemplate>
> <ItemStyle HorizontalAlign="Center"/>
> </asp:TemplateField>
> .....
> .....
> <asp:TemplateField HeaderText="Edit Record">
> <ItemTemplate>
> <asp:LinkButton
> ID = "linkEdit"
> ForeColor="darkBlue"
> runat = "server"
> CausesValidation ="false"
> CommandName = "Edit"
> text = "Edit"
> >

> </asp:LinkButton>
> </ItemTemplate>
>
> <EditItemTemplate>
> <asp:LinkButton
> ID = "linkUpdate"
> ForeColor="darkBlue"
> runat = "server"
> CausesValidation ="true"
> CommandName = "Update"
> text = "Update"
> >

> </asp:LinkButton>
>
> <asp:LinkButton
> ID = "linkCancel"
> ForeColor="darkBlue"
> runat = "server"
> CausesValidation ="false"
> CommandName = "Cancel"
> text = "Cancel"
> >

> </asp:LinkButton>
> </EditItemTemplate>
>
> <FooterTemplate>
> <asp:Button
> ID="cmdAdd"
> Font-Size="Smaller"
> Text="Save"
> ValidationGroup="Footer-Add"
> CausesValidation="true"
> Runat="Server"
> OnClientClick="javascript:return
> ShowConfirm1()"
> OnClick="cmdAdd_OnClick"
> />
> </FooterTemplate>
> .....
> .....
> </asp:GridView>
>
> <asp:ValidationSummary
> id="validSummaryAdd"
> ValidationGroup="Footer-Add"
> ShowSummary="false"
> ShowMessageBox="true"
> HeaderText="The following things have to be fixed (Record -
> Add)"
> runat="server"
> />
> <asp:ValidationSummary
> id="validSummaryEdit"
> ValidationGroup="Row-Edit"
> HeaderText="The following things have to be fixed (Record -
> Edit)"
> ShowSummary="false"
> ShowMessageBox="true"
> runat="server"
> />
>
> ...
> ...
>
> Then I have the code:
> protected void cmdAdd_OnClick(object sender, EventArgs e) //Handles
> cmdAdd.Click
> {
> //Add the record to the Watchlist table, and maybe also add a
> record to the Comments table...
> Util_AddRecord();
> .....
> .....
> }
>
>
> and
>
> protected void Util_AddRecord()
> //Called by cmdAdd_OnClick
> {
> ....
> ....
> bool boolAddCommentsAsWell;
>
> //Ask the user if he wants to also add a record in the "Comments"
> table:
> if (HiddenField1.Value.Equals("1"))
> //User clicked okay
> boolAddCommentsAsWell = true;
> else
> //User clicked cancel
> boolAddCommentsAsWell = false;
>
> //Create a one-to-one reference between this record and the record
> in Comments if
> //strCommentsID not null:
> string strCommentsID;
> if (boolAddCommentsAsWell == true)
> strCommentsID = "W " + lblID.Text;
> else
> strCommentsID = "";
>
> SqlDataSource1.InsertParameters["EmployeePIN"].DefaultValue =
> txtEmployeePIN.Text;
> ....
> SqlDataSource1.InsertParameters["CommentsID"].DefaultValue =
> strCommentsID;
>
> SqlDataSource1.Insert();
> ....
> ....
> }
>
> The problem is the following:
>
> - if I delete the HTML line
>
> OnClientClick="javascript:return ShowConfirm1()"
>
> and therefore skip asking the question, and if I change
>
> if (HiddenField1.Value.Equals("1"))
> //User clicked okay
> boolAddCommentsAsWell = true;
> else
> //User clicked cancel
> boolAddCommentsAsWell = false;
>
> to
> boolAddCommentsAsWell = true;
>
> then the validators validate, and the code SqlDataSource1.Insert()
> executes properly.
>
> - if I run the code as it is, with the confirmation script and the
> if (HiddenField1.... lines, the validators DON'T VALIDATE, so
> therefore SqlDataSource1.Insert() inserts whatever I have entered in
> the gridview. Of course, if I skip entering some cells, it will
> attempt to insert NULLS, which could fail, depending on the fields.
>
> Therefore, my question is - why don't the validators validate when I
> have an extra javascript in the code ? I would have expected to have
> the validators validate BEFORE the cmdAdd_OnClick starts executing...
> Besides, the Javascript (ShowConfirm1) has nothing to do with them....
>
> Thanks a lot
> Alex.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Validators to validate UserControls Nathan Sokalski ASP .Net Web Controls 0 09-05-2008 01:29 PM
Using Validators to validate UserControls Nathan Sokalski ASP .Net 0 09-05-2008 01:29 PM
Can we get rid of unrelated non-python posts? Bruce Python 0 11-13-2007 06:00 AM
Validate a ListBox using validators inside a usercontrol Fernando Lopes ASP .Net 4 11-18-2004 05:50 PM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM



Advertisments