dropdownlist autopostback with javascript confirm

Discussion in 'ASP .Net' started by robert, Oct 11, 2006.

  1. robert

    robert Guest

    I have a dropdownlist with the autopostback set to true. I want the
    user to be confirm whether they do indeed want to change the value,
    which on post back fires a server side event (selectedindexchanged).

    I have tried adding an onchange attribute "return confirm('sure u wanna
    change this?';") but it will not postback regardless of the confirm
    result and the value in the list does not revert back if cancel

    Is there a solution?
    robert, Oct 11, 2006
    1. Advertisements

  2. Since OnChange event is already defined with the DropDownList control,
    you need to preserve it and call your action as well.

    One way to do that is with the Prototype Javascript framework.


    But you do ont want attach a second event. You want to wrap the
    current even inside a confirm block. To do that you could use
    Javascript to take the value of the onclick attribute and change it
    with your confirm code.

    How are your skills with Javascript and DOM?

    Brennan Stehling
    Brennan Stehling, Oct 11, 2006
    1. Advertisements

  3. Hi Robert,

    Try the code below?

    Microsoft MVP [ASP.NET]

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    <script runat="server">

    Protected Sub DropDownList1_SelectedIndexChanged _
    (ByVal sender As Object, ByVal e As System.EventArgs)
    Label1.Text = DropDownList1.SelectedValue.ToString
    End Sub

    Protected Sub Page_Load _
    (ByVal sender As Object, _
    ByVal e As System.EventArgs)
    DropDownList1.Attributes.Add _
    ("OnChange", "if (!confirm('Change this?')){return};")
    End Sub

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Confirm Dropdownlist</title>
    <form id="form1" runat="server">
    <asp:dropdownlist id="DropDownList1" runat="server"
    <asp:listitem selected="True">Red</asp:listitem>
    </asp:dropdownlist><br />
    <br />
    <asp:label id="Label1" runat="server"></asp:label>&nbsp;</div>
    Ken Cox [Microsoft MVP], Oct 11, 2006
  4. the autopostback is done by calling client script that is attached to the
    onchange.only return on false;

    if (!confirm('do this')) return;

    note: your approach has really bad behavior for keyboard users. if they
    select the downdown, and use the arrow keys to select a value, each arrow
    press fires your alert.

    -- bruce (sqlwork.com)
    bruce barker \(sqlwork.com\), Oct 11, 2006
  5. Ken,

    I did not realize it worked that way. I tried your example and see
    that ASP.NET appends the postback code after the Javascript set in the
    attribute above so that you can cancel the postback by calling return.

    I thought that once the OnChange value was set you could not change it.

    Brennan Stehling, Oct 11, 2006
  6. robert

    robert Guest

    I like this idea, easlier I got round it by not using autopostback at
    all and just submitting the form using document.forms[0].submit().
    Crude I know!

    Just need to reset the selectedindex back to the original (using
    variable) if the user cancels.

    Later I'm going to try the atlas control toolkit's confirm control to
    see how it works on the dropdownlist with autopostback....

    robert, Oct 11, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.