1

hi i need to pass the selected dropdown list value to the jqueryui dialog box , when the user clicks on the link, for instance , if the use user selects product one and click the link , the dialog box should popup displaying product 1 selected, I can display the dialog box:

<script>
    $(document).ready({
     $('a.login').click(function(){

        $( "#dialog:ui-dialog" ).dialog( "destroy" );

        $( "#product" ).dialog({
            height: 140,
            modal: true
        });
    });
    </script>

 <div class="login-homepage">
    <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="product1" Value="product1">Camera</asp:ListItem>
    <asp:ListItem Text="product2" Value="product2">DVD</asp:ListItem>
    <asp:ListItem Text="product3" Value="product3">LCD</asp:ListItem>
    </asp:DropDownList>
 <a href="#" id="login">login</a>
<div id="product" title="product-container">
    //show the selected dropdownlist
</div> 

The issue is i am not able to pass the selected value of dropdown list, I tried using ajax json to pass it with no success due to lack of knowledge, Can anyone assist me or provide any suggestions on how to work this out.

Thanks

Mr A
  • 6,448
  • 25
  • 83
  • 137

5 Answers5

2

I guess you want to pass the selected value from the dropdown to another server page using ajax and get the response and show that in the Dialog. This is how i will do that.

$(function(){

 $('a.login').click(function(){

     var selectedVal=$("#DropDownList1").val();
     var url="myserverpage.aspx?product="+selectedVal;

        var dialog = $("#dialog");
        if ($("#dialog").length == 0) {
            dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
        }
        dialog.load(
            url,
            {}, // omit this param object to issue a GET request instead a POST request, otherwise you may provide post parameters within the object
            function (responseText, textStatus, XMLHttpRequest) {
                dialog.dialog({                       
                    close: function (event, ui) {                            
                        dialog.remove();
                    },
                    modal: true,
                    width: 460
                });
            }
        );
    }); 

});

And In myserverpage.aspx, Read the value from querystring product and return the relevant HTML markup you want to show to the user in the dialog. It will work if you have jQuery and jQuery UI loaded to your page properly.

The script will create a div for the popup itself on the fly. You don't need to add one page for that.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • well i want to use one more drop down list within the dialog box and want to populate that with the parameter passed, so if camera[product1] is selected fill the dropdown with camera1 , camera2, camera3, vice versa , is that possible? – Mr A Apr 25 '12 at 13:51
  • @MrA. Yes. pass the value as querystring to the server page, and read the sub categories from your database and then return the markup contains that sub category dropdown. For testing, you can just copy the url and test it in normal browser (without loading it into Dialog). some thing like localhost:yourpost/yourserverpage=product=3&someothervalle=3 – Shyju Apr 25 '12 at 13:53
  • thats exactly i want to acheive , passing a querystring and perform some check on query string then pass the list to dropdownlist on the dialog box – Mr A Apr 25 '12 at 13:54
  • @MrA: Make sure you sanitize your querystring input before using that on a query. Otherwise you will be a victim of SQL injection. http://www.unixwiz.net/techtips/sql-injection.html – Shyju Apr 25 '12 at 13:55
  • tried on the test website www.myfirth.co.uk with no success , i cant see the dialogue box – Mr A Apr 25 '12 at 14:31
  • @MrA : Are you getting any script error in firebug console ? the url appears to be invalid. Do you have a live url i can take a look ? – Shyju Apr 25 '12 at 14:33
  • under the group scheme login , there are two login buttons , the first one is the one i m working on , the second 'click here to login' has same functionality but not in dialog box , thats what i need same functionality but in dialogue box – Mr A Apr 25 '12 at 14:45
  • @MrA: Your `a` tag does not have the css class login.so your jquery selector is wrong. Either add class to a tag or change the selector to `$("#login")` – Shyju Apr 25 '12 at 14:46
  • @MrA: Because it is calling group-schemes.aspx page and it might have a master page to load all contents. You may try returning the specific markup you want to show in the dialog. If using ASP.NET MVc,it is a partial view. – Shyju Apr 25 '12 at 14:56
  • @MrA. because you are passing undefined as the querystring value. Go to your Home page and do a ViewSource. you will see that your Dropdown id is not just `DropDownList1` .Asp.NET added some prefix with that. So probably you want to apply a css class to dropdown and select using that or reading the correct id of dropdown . Read this link for more.http://encosia.com/robust-aspnet-control-referencing-in-javascript/. Use firebug console to see what request you are sending from your browser.use an alert to see what value you are getting from using the jQuery selector – Shyju Apr 25 '12 at 15:01
  • thnxs a lot for the effort u putting to sort out my issue , i have changed the clientmodeid to static which generate the actual id , but still no difference – Mr A Apr 25 '12 at 15:08
  • @MrA: I checked your page source again. It has invalid HTML markup. you have a span inside your class property value. That is invalid. fix your Markup first. – Shyju Apr 25 '12 at 15:37
  • i check using alert , the selectedVal always return one value , even if you change the dropdownlist value – Mr A Apr 25 '12 at 15:39
  • @MrA: My bad. I did dropdown select code outside the click event. Fixed now. Pls check my updated answer. working sample here http://jsfiddle.net/vMKRG/7/ – Shyju Apr 25 '12 at 15:47
1

Your drop-down is part of your page whether it's displayed or not. So you can manipulate it the same way you would any drop-down box. Something like:

Set the value like this:

$("#DropDownList1").val("product2");

Populate the text of #product like this, based on the value selected from the drop-down:

$("#product").text( $("#DropDownList1").val() );
Marc
  • 11,403
  • 2
  • 35
  • 45
  • i just want to show the selected value in the dialogue box , so that i can use that value when submiting – Mr A Apr 25 '12 at 13:46
  • well i want to use one more drop down list within the dialog box and want to populate that with the parameter passed, so if camera[product1] is selected fill the dropdown with camera1 , camera2, camera3, vice versa , is that possible? – Mr A Apr 25 '12 at 13:49
  • Yes, it's possible. My solution describes how to set the value of a dropdown, whether it's in a dialog or not. As long as it's part of the page (even if hidden), you can manipulate its contents. – Marc Apr 25 '12 at 13:51
1

Have you tried using the .html() method? You could do something like:

$(document).ready({

    $("a#login").click(function(e) {

        $("#dialog:ui-dialog").dialog("destroy");

        $("#product").dialog({

            height: 140,
            modal: true

        }).html($("#DropDownList1 option:selected").attr("Text"));

        e.preventDefault();

    });

});

See jsFiddle Demo

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • well i want to use one more drop down list within the dialog box and want to populate that with the parameter passed, so if camera[product1] is selected fill the dropdown with camera1 , camera2, camera3, vice versa , is that possible? – Mr A Apr 25 '12 at 13:49
  • Sure, where are you getting the camera1, camera2, camera3 from? – Code Maverick Apr 25 '12 at 13:55
  • Rather than edit my answer, I'll just point you to [Shyju's answer](http://stackoverflow.com/a/10317100/682480), as that does what you are saying you want now. – Code Maverick Apr 25 '12 at 14:00
1

Since your on the same page you can access the DOM without the need of Ajax.

$(document).ready({
     $('a.login').click(function(){

        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#product .product-info" ).html('You have selected product ' +  $("#DropDownList1").val() + ' from the list.');
        $( "#product" ).dialog({
            height: 140,
            modal: true
     });
});


<div id="product" title="product-container">
    <div class="product-info"></div>
</div> 
Michael D. Irizarry
  • 6,186
  • 5
  • 30
  • 35
  • well i want to use one more drop down list within the dialog box and want to populate that with the parameter passed, so if camera[product1] is selected fill the dropdown with camera1 , camera2, camera3, vice versa , is that possible? – Mr A Apr 25 '12 at 13:49
  • In that case since your using asp.net you can use either jQuery or MS Ajax. It should be relatively easy to do. Heres a good answer using jQuery http://stackoverflow.com/questions/815103/jquery-best-practice-to-populate-drop-down – Michael D. Irizarry Apr 25 '12 at 13:58
1

First off, there are issues with the script in your question.

  • The document ready is improperly formed.
  • The login selector uses a class and it should be an id "a#login" but better as simply "#login" as an id selector will be faster than the tag.
  • I cannot see why you do the dialog destroy, and that id is not even in your markup.
  • I think you need to NOT auto open the dialog at first, then, after injecting in your new select values, open it.

You wish to pick from a list, then dynamically populate another list, inside a dialog.

None of this is too difficult, but requires some rework to make it do what you wish. Thus I present this sample with your stated desires.

You will want to enhance this to meet your final offering, likely some ajax stuff to populate the product list in the dialog etc., but it should get you started.

See a working copy of the below here: http://jsfiddle.net/MarkSchultheiss/u8TMh/1/

$(document).ready(function() {
    $("#productdialog").dialog({
        height: 140,
        modal: true,
        autoOpen: false
    });

    $('a#login').click(function() {
        var pick = $('#DropDownList1 option:selected').text();
        var pickVal = $('#DropDownList1 option:selected').val()
        $('#showem').text($('#DropDownList1 option:selected').text());
        var insertCount = 4;
        var optionList = '';
        while (insertCount--) {
            optionList = optionList + '<option value="' + pickVal + insertCount + '">' + pick + insertCount + '</option>';
        };
        $('#DialogList').html(optionList);
        $("#productdialog").dialog("open");
    });
});

and the markup for my "sample":

<select id="DropDownList1">
     <option value="product1">Camera</option>
     <option value="product2">DVD</option> 
     <option value="product3">LCD</option> 
 </select> 
 <a href="#" id="login">login</a> 
 <div id="product" title="product-container">show the selected dropdownlist </div>  
</div>
<div title='showproducts' id='productdialog' style="display:none"><div id='showem'></div>
 <select id="DialogList">
 </select> 
</div>
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100