Using Javascript and Validation controls

Discussion in 'ASP .Net Web Controls' started by Peter Afonin, Mar 14, 2007.

  1. Peter Afonin

    Peter Afonin Guest

    Hello,

    I'm creating an application in ASP.NET 1.1. I need to check whether at least
    one checkbox in my datagrid has been checked. To do this, I'm using
    Javascript - I'm adding this code to Page_Load event:

    Dim iCount As Int32
    Dim sClientSideValidate As New StringBuilder
    iCount = dgReport.Items.Count
    For i As Int32 = 2 To iCount + 1
    If Not i = iCount + 1 Then
    sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked ||")
    Else
    sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked")
    End If
    Next
    btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    & ")){ return confirm('You have not selected any DRs. Are you sure you want
    to submit change?');}")
    'RELEASE RESOURCES
    sClientSideValidate = Nothing

    This works great. The problem is that if I use this script, my other ASP.NET
    validation controls on this page no longer work. I can see why - since the
    Javascript runs on button onclick (client) event, I guess all other
    Javascript on this page is ignored.

    I wonder - is there a way to use both - javascript validation and ASP.NET
    1.1 validation contols on the same page, or I have to put all validation in
    JS?

    I would appreciate your help.

    Thank you,

    --
    Peter Afonin
     
    Peter Afonin, Mar 14, 2007
    #1
    1. Advertising

  2. Peter Afonin

    marss Guest

    Peter Afonin wrote:

    > btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    > & ")){ return confirm('You have not selected any DRs. Are you sure you want
    > to submit change?');}")
    > 'RELEASE RESOURCES
    > sClientSideValidate = Nothing
    >
    > This works great. The problem is that if I use this script, my other ASP.NET
    > validation controls on this page no longer work. I can see why - since the
    > Javascript runs on button onclick (client) event, I guess all other
    > Javascript on this page is ignored.
    >
    > I wonder - is there a way to use both - javascript validation and ASP.NET
    > 1.1 validation contols on the same page, or I have to put all validation in
    > JS?


    Hi,
    I think your "return confirm(..." breaks off the client-side
    validation of ASP validation controls.

    Try to replace
    return confirm('... to submit change?');

    with
    if (confirm('... to submit change?')) return false;
    Maybe it helps.
     
    marss, Mar 16, 2007
    #2
    1. Advertising

  3. Peter Afonin

    marss Guest

    marss wrote:

    > if (confirm('... to submit change?')) return false;


    I mean
    if (!confirm('... to submit change?')) return false;
     
    marss, Mar 16, 2007
    #3
  4. Peter Afonin

    Peter Afonin Guest

    Thank you very much, you were correct! Without return everything
    works.

    Peter

    On Mar 16, 7:25 am, "marss" <> wrote:
    > PeterAfoninwrote:
    > > btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    > > & ")){ return confirm('You have not selected any DRs. Are you sure you want
    > > to submit change?');}")
    > > 'RELEASE RESOURCES
    > > sClientSideValidate = Nothing

    >
    > > This works great. The problem is that if I use this script, my other ASP.NET
    > > validation controls on this page no longer work. I can see why - since the
    > > Javascript runs on button onclick (client) event, I guess all other
    > > Javascript on this page is ignored.

    >
    > > I wonder - is there a way to use both - javascript validation and ASP.NET
    > > 1.1 validation contols on the same page, or I have to put all validation in
    > > JS?

    >
    > Hi,
    > I think your "return confirm(..." breaks off the client-side
    > validation of ASP validation controls.
    >
    > Try to replace
    > return confirm('... to submit change?');
    >
    > with
    > if (confirm('... to submit change?')) return false;
    > Maybe it helps.
     
    Peter Afonin, Mar 16, 2007
    #4
  5. Peter Afonin

    Peter Afonin Guest

    I'm still having a problem here, because I do need to use 'return
    false' at the end:

    btnSubmit.Attributes.Add("onClick", "if (!(" &
    sClientSideValidate.ToString
    & ")){ return confirm('You have not selected any DRs. Are you sure you
    want
    to submit change?');return false;}")

    If I use it - my other validation controls are dead.

    I wonder - are there any substitutes for 'return false'?

    Thank you,

    Peter

    On Mar 16, 7:25 am, "marss" <> wrote:
    > PeterAfoninwrote:
    > > btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    > > & ")){ return confirm('You have not selected any DRs. Are you sure you want
    > > to submit change?');}")
    > > 'RELEASE RESOURCES
    > > sClientSideValidate = Nothing

    >
    > > This works great. The problem is that if I use this script, my other ASP.NET
    > > validation controls on this page no longer work. I can see why - since the
    > > Javascript runs on button onclick (client) event, I guess all other
    > > Javascript on this page is ignored.

    >
    > > I wonder - is there a way to use both - javascript validation and ASP.NET
    > > 1.1 validation contols on the same page, or I have to put all validation in
    > > JS?

    >
    > Hi,
    > I think your "return confirm(..." breaks off the client-side
    > validation of ASP validation controls.
    >
    > Try to replace
    > return confirm('... to submit change?');
    >
    > with
    > if (confirm('... to submit change?')) return false;
    > Maybe it helps.
     
    Peter Afonin, Mar 19, 2007
    #5
  6. Peter Afonin

    marss Guest

    Peter Afonin wrote:
    > I'm still having a problem here, because I do need to use 'return
    > false' at the end:


    Why? You should to use 'return' only if "your condition"=false

    >
    > btnSubmit.Attributes.Add("onClick", "if (!(" &
    > sClientSideValidate.ToString
    > & ")){ return confirm('You have not selected any DRs. Are you sure you
    > want
    > to submit change?');return false;}")
    >
    > If I use it - my other validation controls are dead.
    >
    > I wonder - are there any substitutes for 'return false'?
    >
    > Thank you,
    >
    > Peter
    >


    Run your project and look through the page source.
    You can find that the Button control is rendered as a html like this:

    <input type="submit" name="btnSubmit" value="Submit" onclick="if
    (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
    language="javascript" id="btnSubmit" />

    Here "if (typeof(Page_ClientValidate) == 'fu..." is the script to call
    the client-side validation. If you add one more event handler for
    client-side "onclick" event it will be added right before above
    script.
    Example:

    btnSubmit.Attributes.Add("onclick", "return false;");

    causes next html to be rendered:
    .... onclick="return false;if (typeof(Page_ClientValidate) ==
    'functio...

    The "return" breaks off the next code execution. So you should write
    your script in the way that does not interrupt the validation if
    everything is ok.
    Try something like this:

    "if (!(" & sClientSideValidate.ToString & ") || confirm('...?'))
    return false;"
     
    marss, Mar 20, 2007
    #6
  7. Peter Afonin

    Peter Afonin Guest

    Thank you, I'll try this.

    Peter

    On Mar 20, 4:24 am, "marss" <> wrote:
    > PeterAfoninwrote:
    > > I'm still having a problem here, because I do need to use 'return
    > > false' at the end:

    >
    > Why? You should to use 'return' only if "your condition"=false
    >
    >
    >
    > > btnSubmit.Attributes.Add("onClick", "if (!(" &
    > > sClientSideValidate.ToString
    > > & ")){ return confirm('You have not selected any DRs. Are you sure you
    > > want
    > > to submit change?');return false;}")

    >
    > > If I use it - my other validation controls are dead.

    >
    > > I wonder - are there any substitutes for 'return false'?

    >
    > > Thank you,

    >
    > >Peter

    >
    > Run your project and look through the page source.
    > You can find that the Button control is rendered as a html like this:
    >
    > <input type="submit" name="btnSubmit" value="Submit" onclick="if
    > (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
    > language="javascript" id="btnSubmit" />
    >
    > Here "if (typeof(Page_ClientValidate) == 'fu..." is the script to call
    > the client-side validation. If you add one more event handler for
    > client-side "onclick" event it will be added right before above
    > script.
    > Example:
    >
    > btnSubmit.Attributes.Add("onclick", "return false;");
    >
    > causes next html to be rendered:
    > ... onclick="return false;if (typeof(Page_ClientValidate) ==
    > 'functio...
    >
    > The "return" breaks off the next code execution. So you should write
    > your script in the way that does not interrupt the validation if
    > everything is ok.
    > Try something like this:
    >
    > "if (!(" & sClientSideValidate.ToString & ") || confirm('...?'))
    > return false;"
     
    Peter Afonin, Mar 20, 2007
    #7
    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. Matt
    Replies:
    14
    Views:
    4,220
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  2. =?Utf-8?B?ZGh1cndpdHo=?=
    Replies:
    1
    Views:
    490
    Teemu Keiski
    Apr 10, 2004
  3. =?Utf-8?B?dmlkeWE=?=
    Replies:
    1
    Views:
    787
    Kevin Spencer
    Jun 2, 2005
  4. baroque Chou
    Replies:
    4
    Views:
    459
    Peter Blum
    Jan 24, 2006
  5. Peter Afonin

    Using Javascript and Validation controls

    Peter Afonin, Mar 14, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    429
    Peter Afonin
    Mar 20, 2007
Loading...

Share This Page