Run JavaScript before Form Submits

Discussion in 'ASP .Net' started by Scott M., Nov 17, 2003.

  1. Scott M.

    Scott M. Guest

    Instead of using a submit button on your form, use a regular HTML button
    that has an onClick event handler on it that calls a function.

    In that function, do whatever you like and when you are done invoke
    formName.submit()


    "Kenneth Keeley" <> wrote in message
    news:uI9La$...
    > Hi
    > How can I run a java script function before a form is submited back to

    the
    > server.
    > What I wish to do is have a upload form that when a user clicks the submit
    > button a javascript function opens a second window after this the buttons
    > onclick event should be run.
    >
    > Is any of this possible.
    >
    >
     
    Scott M., Nov 17, 2003
    #1
    1. Advertising

  2. Hi
    How can I run a java script function before a form is submited back to the
    server.
    What I wish to do is have a upload form that when a user clicks the submit
    button a javascript function opens a second window after this the buttons
    onclick event should be run.

    Is any of this possible.
     
    Kenneth Keeley, Nov 17, 2003
    #2
    1. Advertising

  3. call the javascript function and then return true.
    the form will be submitted as normal.
    eg
    myButton.Attributes.add("onclick","doThisFirst();");

    on the page

    <script>
    function doThisFirst() {
    alert('hello');
    return true;
    }
    </script>
    "Kenneth Keeley" <> wrote in message
    news:uI9La$...
    > Hi
    > How can I run a java script function before a form is submited back to

    the
    > server.
    > What I wish to do is have a upload form that when a user clicks the submit
    > button a javascript function opens a second window after this the buttons
    > onclick event should be run.
    >
    > Is any of this possible.
    >
    >
     
    Andrew de la Harpe, Nov 17, 2003
    #3
  4. Scott M.

    dingo Guest

    Yep, this is why I am having such hard time seeing the
    benefits of ASP.NET. So far I feel like they turned ASP
    development with .NET into a beefed up version of
    FrontPage. Where as on the windows side of things I think
    it is a huge improvement.

    >-----Original Message-----
    >Instead of using a submit button on your form, use a

    regular HTML button
    >that has an onClick event handler on it that calls a

    function.
    >
    >In that function, do whatever you like and when you are

    done invoke
    >formName.submit()
    >
    >
    >"Kenneth Keeley" <> wrote in

    message
    >news:uI9La$...
    >> Hi
    >> How can I run a java script function before a form is

    submited back to
    >the
    >> server.
    >> What I wish to do is have a upload form that when a

    user clicks the submit
    >> button a javascript function opens a second window

    after this the buttons
    >> onclick event should be run.
    >>
    >> Is any of this possible.
    >>
    >>

    >
    >
    >.
    >
     
    dingo, Nov 17, 2003
    #4
  5. Scott M.

    Scott M. Guest

    Oh, I think you haven't worked with it long then. It is a completely new
    paradigm. Much more powerful. Much more scaleable. Not even close to
    anything that FP can do.

    It just so happens that your question wasn't really a .NET question. It was
    a client-side JavaScript question.


    "dingo" <> wrote in message
    news:039901c3acbb$ecd72390$...
    > Yep, this is why I am having such hard time seeing the
    > benefits of ASP.NET. So far I feel like they turned ASP
    > development with .NET into a beefed up version of
    > FrontPage. Where as on the windows side of things I think
    > it is a huge improvement.
    >
    > >-----Original Message-----
    > >Instead of using a submit button on your form, use a

    > regular HTML button
    > >that has an onClick event handler on it that calls a

    > function.
    > >
    > >In that function, do whatever you like and when you are

    > done invoke
    > >formName.submit()
    > >
    > >
    > >"Kenneth Keeley" <> wrote in

    > message
    > >news:uI9La$...
    > >> Hi
    > >> How can I run a java script function before a form is

    > submited back to
    > >the
    > >> server.
    > >> What I wish to do is have a upload form that when a

    > user clicks the submit
    > >> button a javascript function opens a second window

    > after this the buttons
    > >> onclick event should be run.
    > >>
    > >> Is any of this possible.
    > >>
    > >>

    > >
    > >
    > >.
    > >
     
    Scott M., Nov 17, 2003
    #5
  6. Scott M.

    Scott M. Guest

    In the page_load event...

    If IsPostBack() Then
    'Second or subsequent trip to the server

    Else
    'Fist time data was submitted

    End If

    "Kenneth Keeley" <> wrote in message
    news:...
    > Ok then,
    > But what Serverside code is required to make sure the submit button was
    > pressed and not a clear or browser refresh button.
    >
    > "Scott M." <> wrote in message
    > news:...
    > > Instead of using a submit button on your form, use a regular HTML button
    > > that has an onClick event handler on it that calls a function.
    > >
    > > In that function, do whatever you like and when you are done invoke
    > > formName.submit()
    > >
    > >

    >
    >
     
    Scott M., Nov 17, 2003
    #6
  7. Scott M.

    lewi Guest

    > Instead of using a submit button on your form, use a regular HTML button
    > that has an onClick event handler on it that calls a function.

    At the HTML level form objs have a OnSubmit() and OnReset() events
    handlers...

    With...

    1) In you script tag...
    function verifyForm(IncomingForm)
    {
    if(FORMISVALID)
    return true;
    else
    return false;
    }

    2) In form tag(1 or more forms supported cause of IncomingForm param above
    just send verifyForm(...) this)...
    <form ... onSubmit="return verifyForm(this);">...</form>

    Now you just need to add you form processing code and your ready to go...
     
    lewi, Nov 17, 2003
    #7
  8. Ok then,
    But what Serverside code is required to make sure the submit button was
    pressed and not a clear or browser refresh button.

    "Scott M." <> wrote in message
    news:...
    > Instead of using a submit button on your form, use a regular HTML button
    > that has an onClick event handler on it that calls a function.
    >
    > In that function, do whatever you like and when you are done invoke
    > formName.submit()
    >
    >
     
    Kenneth Keeley, Nov 17, 2003
    #8
  9. "Scott M." <> wrote in message
    news:...
    > In the page_load event...
    >
    > If IsPostBack() Then
    > 'Second or subsequent trip to the server
    >
    > Else
    > 'Fist time data was submitted
    >
    > End If
    >


    OK but this doesn't stop the submit code from running if you press a
    different button or refresh the page.
     
    Kenneth Keeley, Nov 17, 2003
    #9
  10. Scott M.

    SSW Guest

    Use Javscript below
    <script language="javascript">
    function ConfirmOnSummit()
    {
    if (typeof(Page_ClientValidate) == 'function')
    {
    if (Page_ClientValidate())
    {
    if(confirm("What Ever u what to confirm or run a Java Script
    code?")==1)
    return true;
    else
    return false;
    }
    }
    }
    </script>
    Create tage in aspx page.
    <span onclick="javascript: return ConfirmOnSummit();"><asp:button
    id="btnSubmit" runat="server" CausesValidation="False"
    Text="Submit"></asp:button></span>

    This Will surely work. It take all the advantage of Asp.Net

    Thanks,

    sswalia
    MCAD, MCAD, OCA

    "Kenneth Keeley" <> wrote in message
    news:uI9La$...
    > Hi
    > How can I run a java script function before a form is submited back to

    the
    > server.
    > What I wish to do is have a upload form that when a user clicks the submit
    > button a javascript function opens a second window after this the buttons
    > onclick event should be run.
    >
    > Is any of this possible.
    >
    >
     
    SSW, Nov 17, 2003
    #10
  11. Scott M.

    SSW Guest

    Sorry Please igonre above solution. It's not for you.
    "Kenneth Keeley" <> wrote in message
    news:uI9La$...
    > Hi
    > How can I run a java script function before a form is submited back to

    the
    > server.
    > What I wish to do is have a upload form that when a user clicks the submit
    > button a javascript function opens a second window after this the buttons
    > onclick event should be run.
    >
    > Is any of this possible.
    >
    >
     
    SSW, Nov 17, 2003
    #11
  12. Scott M.

    Joe Gass Guest

    try
    Page.RegisterOnSubmitStatement

    "Kenneth Keeley" <> wrote in message
    news:uI9La$...
    > Hi
    > How can I run a java script function before a form is submited back to

    the
    > server.
    > What I wish to do is have a upload form that when a user clicks the submit
    > button a javascript function opens a second window after this the buttons
    > onclick event should be run.
    >
    > Is any of this possible.
    >
    >
     
    Joe Gass, Nov 17, 2003
    #12
  13. Scott M.

    Scott M. Guest

    "Kenneth Keeley" <> wrote in message
    news:uBJx$...
    > "Scott M." <> wrote in message
    > news:...
    > > In the page_load event...
    > >
    > > If IsPostBack() Then
    > > 'Second or subsequent trip to the server
    > >
    > > Else
    > > 'Fist time data was submitted
    > >
    > > End If
    > >

    >
    > OK but this doesn't stop the submit code from running if you press a
    > different button or refresh the page.
    >
    >


    If you refresh the page, you are not re-submitting the data, you are
    re-requesting the page with whatever data was posted previously.

    If you want to prevent ASP.NET server-side code from running you could use
    only HTML controls and the client-side JavaScript I described in my earlier
    post.
     
    Scott M., Nov 17, 2003
    #13
  14. OK then, but how do I use it, A sample would be nice.

    "Joe Gass" <> wrote in message
    news:...
    > try
    > Page.RegisterOnSubmitStatement
    >
     
    Kenneth Keeley, Nov 17, 2003
    #14
  15. Scott M.

    Joe Gass Guest

    Joe Gass, Nov 18, 2003
    #15
    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. Robert V
    Replies:
    1
    Views:
    1,179
    Jeremy Fluhmann
    Jul 21, 2003
  2. Stan
    Replies:
    1
    Views:
    2,523
    Jacob Yang [MSFT]
    Dec 3, 2003
  3. Mad Scientist Jr
    Replies:
    1
    Views:
    535
    bruce barker
    Mar 5, 2004
  4. Wysiwyg
    Replies:
    2
    Views:
    1,749
    Wysiwyg
    Apr 26, 2005
  5. TS

    Validation controls validate before form submits

    TS, Jun 24, 2005, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    198
Loading...

Share This Page