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

Discussion in 'ASP .Net' started by Radu, Feb 8, 2008.

  1. Radu

    Radu Guest

    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.
    Radu, Feb 8, 2008
    #1
    1. Advertising

  2. Re: Mystery: Validators don't always validate ? Can unrelated Javascript stop them ?

    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" <> wrote in message
    news:...
    > 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.
    Misbah Arefin, Feb 8, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ratman
    Replies:
    0
    Views:
    650
    Ratman
    Sep 14, 2004
  2. Fernando Lopes
    Replies:
    4
    Views:
    3,977
    Peter Blum
    Nov 18, 2004
  3. fiziwig
    Replies:
    0
    Views:
    381
    fiziwig
    Aug 17, 2006
  4. Bruce
    Replies:
    0
    Views:
    251
    Bruce
    Nov 13, 2007
  5. Nathan Sokalski

    Using Validators to validate UserControls

    Nathan Sokalski, Sep 5, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    293
    Nathan Sokalski
    Sep 5, 2008
Loading...

Share This Page