-->

How to bind dropdownlist in ASP.NET Using Jquery and Get Selected Value from Code behind.

Introduction

In this post, I am explaining how to bind dropdownlist in ASP.NET Using Jquery and Get Selected Value from Code behind.

Features:

  • Bind dropdownlist from database data in ASP.NET Using Jquery.
  • Get dropdown selected value from Code behind.
  • Retain dropdownlist selected value after postback.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create a table for fetch data from Jquery function.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Webpage and Design for bind database data to dropdown list.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code
  1. <h3>Bind dropdownlist from database data in ASP.Net using Jquery.</h3>
  2. <table>
  3. <tr>
  4. <td>Select From Dropdown : </td>
  5. <td>
  6. <asp:DropDownList ID="ddItems" runat="server"></asp:DropDownList>
  7. </td>
  8. <td>
  9. <asp:Button ID="btnGetValue" runat="server" Text="Get Selected Value" OnClick="btnGetValue_Click" />
  10. </td>
  11. </tr>
  12. <tr>
  13. <td>
  14. <asp:HiddenField ID="hfSelectedValue" runat="server" />
  15. </td>
  16. <td colspan="2">
  17. <asp:Label ID="lblMsg" runat="server" />
  18. </td>
  19. </tr>
  20. </table>
JS Code
  1. <script src="Scripts/jquery-1.7.1.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function () {
  4.  
  5. $('#<%=ddItems.ClientID%>').append(
  6. $('<option></option>').val('0').html('Please Wait...')
  7. );
  8. $.ajax({
  9. url: "Default.aspx/GetCountryList",
  10. data: "{}",
  11. type: "POST",
  12. dataType: "json",
  13. contentType: "application/json; charset=utf-8",
  14. success: OnSuccess,
  15. error: OnError
  16. });
  17.  
  18. // for get selected value from code behind
  19. $('#<%=ddItems.ClientID%>').change(function () {
  20. $('#<%=hfSelectedValue.ClientID%>').val($('#<%=ddItems.ClientID%>').val());
  21. });
  22.  
  23. });
  24.  
  25. function OnSuccess(data) {
  26. $('#<%=ddItems.ClientID%>').empty();
  27. var d = data.d;
  28. var dropdown = $('#<%=ddItems.ClientID%>');
  29. for (var i = 0; i < d.length; i++) {
  30. dropdown.append(
  31. $('<option></option>').val(d[i].CountryID.toString()).html(d[i].CountryName.toString())
  32. );
  33. }
  34. //for keep data after postback
  35. $('#<%=ddItems.ClientID%>').val($('#<%=hfSelectedValue.ClientID%>').val());
  36. }
  37. function OnError() {
  38. alert("Failed!");
  39. }
  40.  
  41. </script>

Step-6: Write this function into your page code behind.


Must have to do this EnableEventValidation="false" into your page.
  1. [WebMethod]
  2. public static List<CountryMaster> GetCountryList()
  3. {
  4. List<CountryMaster> country = new List<CountryMaster>();
  5. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  6. {
  7. country = dc.CountryMasters.ToList();
  8. }
  9. return country;
  10. }

Step-7: Write this code into button_click event for get selected value from code behind.


  1. protected void btnGetValue_Click(object sender, EventArgs e)
  2. {
  3. lblMsg.Text = "Selected Value is : " + hfSelectedValue.Value;
  4. }

Step-8: Run Application.




Related Posts:




Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: