How do I combine a Dropdown List control & Javascript Confirm

Last post 04-12-2007 4:39 PM by kblackwell. 11 replies.

Sort Posts:

  • How do I combine a Dropdown List control & Javascript Confirm

    02-21-2007, 12:41 PM
    • Loading...
    • zoopnfunk
    • Joined on 12-20-2005, 3:06 PM
    • Posts 41

    Hi,

     On my aspx page I have a asp:dropdown list that is populated from the database with a listing of Chapters and their corresponding ID fields.  The page also contains a datagrid that is being populated on the basis of the Chapters dropdown list selection.  I am calling the dropdown list ddlChapter.  DDL chapter also has one blank element that does not contain a chapter.  Selecting the blank element does not populate the datagrid with any values.

    What I would like to do is to add a javascript (or otherwise) confirmation window to the dropdown list.  When a user makes a selection other than the blank item I would like to display a custom message notifying them of which selection they are about to make and asking them if they would like to proceed. 

    If the user selects to continue the selectedIndexChange event should fire -- populating my datagrid.  If the user selects to cancel the index change the even should not execute.

    Can anyone help me with this code.  I have tried using

    ddlChapter.Attributes.Add("onchange", "return ConfirmApply();")

    This code does call my javascript method, but the datagrid is never repopulated -- ie the selectedIndexChange event never fires no matter what the choice is when this attribute is added to the control.  My javascript ConfirmApply function is as follows.  At the moment it just returns the value of the selecteditem in the dropdown list. 

     function ConfirmApply() { var doc = document.forms[0]; var myValue = doc.ddlChapter.options[doc.ddlChapter.selectedIndex].value; alert(myValue); return false; }

     Does anyone have any ideas on how I can make this form function so that I can display a confimation dialogue on the ddlChapter indexChange event but still repopulate the datagrid if the users selection is to proceed?

     Any advice is appreciated!  Thanks.

     

  • Re: How do I combine a Dropdown List control & Javascript Confirm

    02-21-2007, 12:50 PM
    • Loading...
    • shados
    • Joined on 07-07-2006, 7:24 PM
    • Posts 1,644

    Instead of: alert(myValue); return false;, put:

    return confirm( 'You are selecting' + myvalue + ' Is that really what you want to do? Blah blah blah' );

    if the user click yes, the function will return true and the onchange event will continue to do the postback. If the user click no, it will return false, thus stopping the event in its path.

    Is that what you wanted?

  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-01-2007, 2:55 PM
    • Loading...
    • zoopnfunk
    • Joined on 12-20-2005, 3:06 PM
    • Posts 41
    shados:

    Instead of: alert(myValue); return false;, put:

    return confirm( 'You are selecting' + myvalue + ' Is that really what you want to do? Blah blah blah' );

    if the user click yes, the function will return true and the onchange event will continue to do the postback. If the user click no, it will return false, thus stopping the event in its path.

    Is that what you wanted?

     Thanks for the reply, I think we're close now but I am having trouble with the SelectedIndexChnged event not firing no matter whether I confirm OK or CANCEL.

    Here is the code I am using in my javascript:

    function confirm_submit()
    		{
    			var doc = document.forms[0];
    			var myValue = doc.ddlChapter.options[doc.ddlChapter.selectedIndex].text;
    		
    			if (confirm("Process dues for " + myValue + "?")==true)
    			return true;
    			else
    			doc.ddlChapter.selectedIndex = 0;
    			return false;
    		}

     In my page_Load event I have

    If Not page.isPostBack then

    ddlChapter.Attributes.Add("onchange", "return confirm_submit();")

     End If

    The name of my dropdown list is ddlChapter.  Naturally, I have some code in:

    Private Sub ddlChapter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlChapter.SelectedIndexChanged
    
    dgChapterMembers.CurrentPageIndex = 0		'reset selected page index
    		If NOT ddlChapter.SelectedIndex = 0 Then
    		     populateDataGridByChapter(dgChapterMembers.Attributes("SortExpression"))		
                    Else
                         Me.Server.Transfer(strAppPath & "subpages/chapters/addEditDues.aspx")
    		End If
    END SUB
    

     

    When I change the chapter dropdown the javascript confirmation pops up -- but the ddlChapter_SelectedIndexChanged never fires -- no matter which selection is made.

    Any ideas?  Thanks!

    Filed under: ,
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-01-2007, 3:41 PM
    • Loading...
    • SuperGhost
    • Joined on 12-29-2005, 4:58 PM
    • Palmdale CA, USA
    • Posts 404
    Can you paste the rendered HTML of your select list?
    ** Hope this helps!

    DennyDotNet.com
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-02-2007, 7:24 AM
    • Loading...
    • sandesh
    • Joined on 11-28-2006, 12:30 PM
    • Delhi
    • Posts 20

    hi,

    you have to call the _doPostback function of javascript to call the selected index change event of combo

    box.

    function is below

    function ConfirmApply()

    {

    var ddl=document.GetElementbyID("<% = ddlChapter.ClientID %>");

    var rep=Confirm("You have selected"+ddl.selectedIndex+")";

    if (rep=='Yes')

    {

    _doPostback(ddl,"");

    return false;

    }

    else

    {

    return false;

    }

    sandesh
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-02-2007, 7:24 AM
    • Loading...
    • sandesh
    • Joined on 11-28-2006, 12:30 PM
    • Delhi
    • Posts 20

    hi,

    you have to call the _doPostback function of javascript to call the selected index change event of combo

    box.

    function is below

    function ConfirmApply()

    {

    var ddl=document.GetElementbyID("<% = ddlChapter.ClientID %>");

    var rep=Confirm("You have selected"+ddl.selectedIndex+")";

    if (rep=='Yes')

    {

    _doPostback(ddl,"");

    return false;

    }

    else

    {

    return false;

    }

     

    sandesh khare

    sandesh
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-02-2007, 7:51 AM
    • Loading...
    • zoopnfunk
    • Joined on 12-20-2005, 3:06 PM
    • Posts 41

    Thanks for the advice.   I tried to the function above and am receiving an "Object Expected" error.

    I believe the error is being caused because the

    var ddl = document.GetElementbyID("<%=ddlChapter.ClientID %>");

     is not picking up the dropdown list.  The dropdown list is an asp:dropdown list and I am declaring it as follows:

      

    Protected WithEvents ddlChapter As DropDownList
     

    Filed under: ,
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-02-2007, 7:59 AM
    • Loading...
    • zoopnfunk
    • Joined on 12-20-2005, 3:06 PM
    • Posts 41

    Oh -- Here is how the control is rendering in HTML (minus the values etc.)

     

    <select name="ddlChapter" onchange="return ConfirmApply();__doPostBack('ddlChapter','')" language="javascript" id="ddlChapter" class="mainContent" style="width:350px;">
     
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-02-2007, 8:24 AM
    • Loading...
    • zoopnfunk
    • Joined on 12-20-2005, 3:06 PM
    • Posts 41

    Excessive googling finally paid off: http://msdn2.microsoft.com/en-us/library/aa479302.aspx

     I ended up using the javascript function:

    function confirm_submit()
    		{
    			var doc = document.forms[0];
    			var myValue = doc.ddlChapter.options[doc.ddlChapter.selectedIndex].text;
    		
    			if (confirm("Process dues for " + myValue + "?")==true)
    			return true;
    			else
    			doc.ddlChapter.selectedIndex = 0;
    			return false;
    		}

     and I register it to the dropdown list as follows:

    ddlChapter.Attributes("onchange") = "if(confirm_submit());"

    Thanks for helping me to get moving in the right direction.

     

  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-02-2007, 9:28 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    A better, more flexible alternative would be to setup the function to accept the DDL as a paramter.  This way, you could use the function on many different DDLs for different pages.

        function confirm_submit(elem) {
            if(elem != null) {
                var myValue = elem.options[elem.selectedIndex].text;
    		
                if(confirm("Process dues for " + myValue + "?"))
                    return true;
                else {
                    doc.ddlChapter.selectedIndex = 0;
                    return false;
                }
            }
        }
      

    Then, of course, you will have to change your code-behind attribute assignment like this: 

        ddlChapter.Attributes("onchange") = "if(confirm_submit(this));"
      


    Hope this helps!  Please mark the most helpful post(s) as Answer so future readers know how you resolved your issue.  Thanks!

    Josh Stodola ← Come check out my blog!
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    03-02-2007, 9:31 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    JoshStodola:
       
                if(confirm("Process dues for " + myValue + "?"))
                    return true;
                else {
                    doc.ddlChapter.selectedIndex = 0;
                    return false;
                }

     

    My apologies, one line has to be changed:  ( doc.ddlChapter.selectedIndex = 0; )  should be  (  elem.selectedIndex = 0;  )

    Hope this helps!

    Josh Stodola ← Come check out my blog!
  • Re: How do I combine a Dropdown List control & Javascript Confirm

    04-12-2007, 4:39 PM
    • Loading...
    • kblackwell
    • Joined on 02-19-2007, 4:26 PM
    • Posts 46

    Hi

    This looks like what I need to get working... but how do I have it execute more code-behind code if it evaluates to true?

    I've already got a OnSelectedIndexChange event for my dropdown list; I just want to add a confirmation dialog like the original poster did.

    Thanks

     

Page 1 of 1 (12 items)
Microsoft Communities
Page view counter