0

can any one help me to How to Bind Dropdown List with Web Services in ASP.NET using C#

    [WebMethod]
public DataTable dropdwnfill()
{
    DataTable ds;
    SqlCommand cmd = new SqlCommand("select itemname from tbl_pro", con);
    SqlDataAdapter dap = new SqlDataAdapter(cmd);
    ds = new DataTable();

    dap.Fill(ds);
    ds.TableName = "MyDt";
    return ds;
}

thanks in advance

  • What have you tried so far? It is best to give it a go first, then ask specific questions when you get stuck. Questions this broad may get downvoted – morgan121 Feb 22 '19 at 04:23
  • RAB this much i done in my asmx page. how to call it in my aspx page// – Binoy Babu Feb 22 '19 at 04:31

1 Answers1

0

If i understand the requirement correctly, you want to populated a drop-down control from server side, but that to be invoked from UI (e.g a ajax call).

First of all, you can do the same with page-method also. Populate the required data in server side method (say dropdwnfill()) and return a collection of row. Then consume that method through ajax call from UI and populate the drop down. see below :

/*structure to be return*/
   public class DropDownValue
    {
        public string name { get; set; }
        public string value { get; set; }
    }
/*server side page-method*/
        [WebMethod]
        public static List<DropDownValue> dropdwnfill()
        {            
           // sample data population in list
            DropDownValue dropDownValue1 = new DropDownValue();
            dropDownValue1.name = "TestName1";
            dropDownValue1.value = "1";

            DropDownValue dropDownValue2 = new DropDownValue();
            dropDownValue2.name = "TestName2";
            dropDownValue2.value = "2";

            List<DropDownValue> lst = new List<DropDownValue>();
            lst.Add(dropDownValue1);
            lst.Add(dropDownValue2);

            return lst;

        } 

/*Consume in UI*/
$(document).ready(function () {       
        $("[id*=btnLoadDropDown]").click(function () { 
            $.ajax({
                type: "POST",
                url: "default.aspx/dropdwnfill",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (jqXHR) {
                    //do required stuff for error
                    alert(jqXHR.responseText);                    
                }.bind(this),
                success: function (msg) {                    
                    var option = '';
                    if (msg.d.length > 0) {                       
                        for (i = 0; i < msg.d.length; i++) {                            
                            option += '<option value="'+ msg.d[i].value + '">' + msg.d[i].name + '</option>';
                        }                       
                        $('#drpTest').append(option);                       
                    }
                }.bind(this)
            });
            return false;

        });
    });

/*UI element Design*/
<div>
<asp:DropDownList ID="drpTest" runat="server" AutoPostBack ="false" Width="356px"> </asp:DropDownList>
</div>
<asp:Button ID ="btnLoadDropDown" runat ="server" Width="354px" Text="Load Drop Down" AutoPostBack ="false"/>

Secondly , the purpose of web service is to expose a end-point. So, you can also create a web service to get only data from server, and then populate the drop down as shown earlier. To create/consume a web-service, you can refer : How to create web service (server & Client) in Visual Studio 2012?

Abhisek
  • 16
  • 1