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

R

Radu

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.
 
M

Misbah Arefin

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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top