How to Bind-Get-Set value to dropdownlist using jquery


1. Add an option to list:

 $("<option value='0'>Select</option>").appendTo("#ddlCategory");

--------------------------------------------------------------------------

2. Remove all the Options from drop down:

$("#ddlCategory> option").remove();

--------------------------------------------------------------------------

3. Get the selected item Text:

$("#ddlCategory option:selected").text();

--------------------------------------------------------------------------

4. Get the selected item Value:

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

--------------------------------------------------------------------------

5. Set the item by Value:

$("#ddlCategory").val(2);

--------------------------------------------------------------------------

6. Selection change event:

$("#ddlCategory").change(function(e) 

{
    //do your work
});

 and here goes populating a dropdown through jquery.....


<html>

  <body>
    <form id="form1">
        <select id="ddlCategory" ></select>
    </form>
  </body>
</html>

<script type="text/javascript" language="javascript">

var ddlCategory=  $("#ddlCategory");
$.ajax ({
         type: "POST",
         contentType: "application/json; charset=utf-8",
         url: "WebService1.asmx/GetCategoryList",
         dataType: "json",
         success: function(data)
        {
            for(i=o; i< data.d.length; i++)
             {
            ddlCategory.append($("<option></option>").val(data.d[i].CategoryID']).html(data.d[i].CategoryName));
            });
        
        }
 });  
</script>

 and here goes webservice code......


 public class WebService1 : System.Web.Services.WebService

    {
        [WebMethod]
public IEnumerable<Category> GetCategoryList()
        {
            Collection<Category> categoryyList = new Collection<Category>();

            categoryyList .Add(new Category(1, "Electronics"));

            categoryyList .Add(new Category(2, "Automobiles"));
            categoryyList .Add(new Category(2, "Books"));

            categoryyList .Add(new Category(2, "Furniture"));


            categoryyList .Add(new Category(2, "Cosmetics"));



            return categoryyList;

        }
}
--------------------------------------------------------------------------

Comments

Popular posts from this blog

GROUP BY, CUBE, ROLLUP and SQL SERVER 2005

How to get content of Ckeditor & Fckeditor using Javascript

How to Fix Error- Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out