basic query on form postback

Discussion in 'ASP .Net' started by varkey.mathew@wipro.com, Jun 27, 2005.

  1. Guest

    Hi there,

    I have a basic form with a postback.

    The following is a line of dynamic code written so that when the user
    clicks on the Button control, he will receive a confirm messagebox.

    btnSubmit.Attributes.Add("onclick","return confirm('Are you sure you
    want to leave without saving your change(s)?');");

    This works perfectly fine.

    When I tried to alter the same so that the same functionality is
    achieved by a Combobox, using the "onchange" event, I seem to hit upon
    an issue.

    cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
    want to leave without saving your change(s)?');");

    Basically, when the combo box value is changed, it pops up the confirm
    box, but an "OK" response will not postback the form even though the
    AutoPostBack option is set to true for the Combo box control.

    Please help.

    Thanks and regards,
     
    , Jun 27, 2005
    #1
    1. Advertising

  2. Hi there,

    The reason your Submit button works is that what a submit button does is
    submit the form. When you add a JavaScript event handler that returns true
    or false, you either accept or cancel the event that occurred. In the case
    of a Submit button, the click event by default submits the form. That is,
    ASP.Net does not add any JavaScript to the Submit button to submit the form,
    because it doesn't need to. So, of course, there's a PostBack.

    In the case of a ComboBox, the "select" object in an HTML form has an
    "onchange" event that is fired when you change the selectedIndex of the
    object. This event is what is used by the ASP.net object model to add a bit
    of JavaScript that enters the information about the select object to the
    hidden form fields, and programmatically submit the form, which is a
    PostBack. It seems that you've overridden this event handler, so that
    instead of doing the JavaScript function, it does the confirm method
    instead. Since returning a value from a JavaScript event handler either
    accepts or cancels the event, and the "select" object doesn't, by default,
    submit the form, the form is not submitted.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Ambiguity has a certain quality to it.

    <> wrote in message
    news:...
    > Hi there,
    >
    > I have a basic form with a postback.
    >
    > The following is a line of dynamic code written so that when the user
    > clicks on the Button control, he will receive a confirm messagebox.
    >
    > btnSubmit.Attributes.Add("onclick","return confirm('Are you sure you
    > want to leave without saving your change(s)?');");
    >
    > This works perfectly fine.
    >
    > When I tried to alter the same so that the same functionality is
    > achieved by a Combobox, using the "onchange" event, I seem to hit upon
    > an issue.
    >
    > cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
    > want to leave without saving your change(s)?');");
    >
    > Basically, when the combo box value is changed, it pops up the confirm
    > box, but an "OK" response will not postback the form even though the
    > AutoPostBack option is set to true for the Combo box control.
    >
    > Please help.
    >
    > Thanks and regards,
    >
     
    Kevin Spencer, Jun 27, 2005
    #2
    1. Advertising

  3. snt Guest

    If you are using html select, then you can manually call postback by
    calling the javascript function __doPostBack('cmbGroup','').

    If you use asp:DropDownList, you can add your client javascript
    function as an attribute and you have to handle to cancel the post
    back.

    Hope this helps.

    snt
    http://www.onlinemall.com
    http://www.flexoweb.com
     
    snt, Jun 27, 2005
    #3
  4. Guest

    Thanks Kevin & SNT for your quick responses,

    I was wondering whether there is any way I can actually override the
    event handler with confirm() function and then do a postback as well..
    It is an asp:DropDownList ...

    If there is any material that you can direct me to on the net, it would
    be really very helpful.

    Thanks once again.

    Regards,

    Varkey
     
    , Jun 27, 2005
    #4
  5. Hi Varkey,

    I think the problem is just because when you using the

    cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
    want to leave without saving your change(s)?');");

    , the dropdownlist's "onchange" event will end right after we make the
    decision on the confirm dialog , so that the sequential "__doPostBack"
    function is not called. I suggest you change your code to the below:

    ==============
    private void Page_Load(object sender, System.EventArgs e)
    {
    string script =
    @"

    if(!confirm('Are you sure to postback?')){ return false};

    ";

    lstItems.Attributes.Add("onchange",script);
    }
    ===============

    that means we only return false when the user choose "no", but let the
    event continue if "yes" is choosen. This works well on my side, please let
    me know if it also works on your side.

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jun 28, 2005
    #5
  6. Guest

    Hi Steve,

    It worked a treat!!!!

    Thanks a tonne.. u r a genius.. ;)

    Varkey

    Steven Cheng[MSFT] wrote:
    > Hi Varkey,
    >
    > I think the problem is just because when you using the
    >
    > cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
    > want to leave without saving your change(s)?');");
    >
    > , the dropdownlist's "onchange" event will end right after we make the
    > decision on the confirm dialog , so that the sequential "__doPostBack"
    > function is not called. I suggest you change your code to the below:
    >
    > ==============
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > string script =
    > @"
    >
    > if(!confirm('Are you sure to postback?')){ return false};
    >
    > ";
    >
    > lstItems.Attributes.Add("onchange",script);
    > }
    > ===============
    >
    > that means we only return false when the user choose "no", but let the
    > event continue if "yes" is choosen. This works well on my side, please let
    > me know if it also works on your side.
    >
    > Hope helps. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
     
    , Jun 28, 2005
    #6
  7. You're welcome :)

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jun 29, 2005
    #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. Aleksandr Ayzin

    PostBack basic question

    Aleksandr Ayzin, Jul 15, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    318
    Eliyahu Goldin
    Jul 15, 2004
  2. KiwiBrian

    Basic CSS query

    KiwiBrian, Jul 11, 2003, in forum: HTML
    Replies:
    4
    Views:
    499
    Leif K-Brooks
    Jul 12, 2003
  3. Engineer
    Replies:
    6
    Views:
    629
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    441
  5. Rob R. Ainscough
    Replies:
    3
    Views:
    334
    Rob R. Ainscough
    Jun 14, 2005
Loading...

Share This Page