Loading ...

How to populate dropdownlist through jquery

Who is online?  0 guests and 0 members
home  »  blogs  »  Vinay Gupta  »  How to populate dropdownlist through jquery

Product Spotlight

ASP.NET Hosting Spotlight

 

Comments (9)

mohit
mohit kumar said:

nice

5/21/2010
 · 
 
by
ajit
Ajit Gupta said:

Good one..

Keep it up...........Smile

5/21/2010
 · 
 
by
nilact
nil thakkar said:

It won't retain dropdown value across postback :)

5/26/2010
 · 
 
by
Shaitender

Don't use $.each(), use for(;;) instead. It's over ten times faster.

9/3/2010
 · 
 
by
neel
neel pannu said:

Good post Vinay.

How about response to Nil Thakkar's comment? ---> It won't retain dropdown value across postback :)

9/4/2010
 · 
 
by
raghav_khunger

My suggestion, instead of doing this 

 

  $.each(data.d, function() 

             {             $("#ddlCategory").append($("<option></option>").val(this['CategoryID']).html(this['CategoryName']));

 

         });

 

 

You should put a loop on some variable which will hold the html for dropdown instead of looping with .append on ddlCategory. Appending in loop in DOM will hit the performance.

5/21/2010
 · 
 
by
vinay_jss
Vinay Gupta said:

Thanks for your valuable feedback!! I will change it accordingly.

9/3/2010
 · 
 
by
vinay_jss
Vinay Gupta said:

I have made some changes according to your valuable feedback.
@Raghav sir

var ddlCategory=$("#ddlCategory");


@Shaitender

for(i=o; i< data.d.length; i++)
{ 
   ddlCategory.append($("").val(data.d[i].CategoryID']).html(data.d[i].CategoryName)); 
}); 

Thanks to all for update my skills.

9/3/2010
 · 
 
by
vinay_jss
Vinay Gupta said:

@nil thakkar

On postback, each time dropdown re-populated. thats why it lossed the selected value.

To retain value across postback, We can use a hidden field that hold previous selected value.

1. take a hidden field on your form.

<input  type="hidden" id="hdnCategoryID"/>

2. on change event assign the value in hidden field

$("#ddlCategory").change(function(e) 
{
$('#hdnCategoryID').val($("#ddlCategory").val());
});

3. to retain the selected value of dropdown

$("#ddlCategory").val($('#hdnCategoryID').val());

Please note that the last line (step 3) should written after the ajax calll completed. i.e. after dropdown populated.
9/4/2010
 · 
 
by

Confirm

Latest community blog posts

  • A person on the forums was struggling with jQuery tools tabs index issue on postback. On clicking the tabs and doing some postback the selected tab index was getting lost. Below is the solution on how to retaing the previous selected index which was there before postback. Complete Source: <%@ Pag...
  • I was working with jQuery tools tabs today and I had to set the initial index of tab selected by default. The solution is to set initialindex property in the options. Source: $("ul.tabs").tabs("div.panes > div", { initialIndex: 1 }) ; Complete code to test above: <html> <head> <tit...
  • A person asked me today how to get form authentication cookie and get the data present in it. I decided to write a short blog on it. Formauthentication cookie is saved with the name which we can get via FormsAuthentication.FormsCookieName property. The key here is to get the cookie via this name and...
  • While working with one of the sample application I noticed that form authentication timeout in web.config was overriding the manual ticket's (which we created via code) timeout. Below is the code which was written in the application: var ticket = new FormsAuthenticationTicket(1, "UserName", DateTime...
  • In this blog I will show how to select a range of characters in textbox with jquery. In my demo I have used one textbox and one button. On click of button arange of characters in textbox will be selected. Below is the sample code: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="ser...

Vinay Gupta's latest blog posts

Product Spotlight

ASP.NET Hosting Spotlight

 

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?