How to Bind data to multiple dropdown <WebMethod()> VB

Aypn CNN 386 Reputation points


Please help me with the following; I would like to bind dates to multiple dropdown options at once. I can able get one and bind dropdown (ddlRegnList), wanna bind another one dropdown (ddlBranchList), how to do simply?

<WebMethod()> <ScriptMethod()>  
    Public Shared Function getRegnList(ByVal recid As String) ', As List(Of ListItem)  
        Dim retMessage As String = String.Empty  
        Dim DCStr As String = ConfigurationManager.ConnectionStrings("MyCs").ConnectionString  
        Dim selectSQL As String  
        Dim sCon As New SqlConnection(DCStr)  
        Dim sCmd As New SqlCommand(selectSQL, sCon)  
        Dim param As SqlParameter  
        Dim dtadp As New SqlDataAdapter(sCmd)  
        Dim dtset As New DataSet  
        Dim f_regnlist As New List(Of ListItem)()  
        Dim f_brlist As New List(Of ListItem)()  
        Dim f_retval As String = ""  
            sCmd.CommandType = CommandType.StoredProcedure  
            sCmd.CommandText = "LGSP_GetRegionBranch"  
            sCmd.Parameters.Add(New SqlParameter("@i_SP_AttributeId", SqlDbType.Int)).Value = 1  
            sCmd.Parameters.Add(New SqlParameter("@i_TblRowid", SqlDbType.VarChar)).Value = recid ' f_db.tblrowid  
            param = New SqlParameter("@o_ErrorStatus", SqlDbType.Int) With {  
            .Direction = ParameterDirection.Output  
            If sCmd.Parameters("@o_ErrorStatus").Value = 1 Then  
                Dim i As Integer  
                For i = 0 To dtset.Tables(0).Rows.Count - 1  
                    f_regnlist.Add(New System.Web.UI.WebControls.ListItem((dtset.Tables(0).Rows(i).Item(1).ToString), (dtset.Tables(0).Rows(i).Item(0).ToString)))  
                    f_brlist.Add(New System.Web.UI.WebControls.ListItem((dtset.Tables(0).Rows(i).Item(3).ToString), (dtset.Tables(0).Rows(i).Item(2).ToString)))  
            ElseIf sCmd.Parameters("@o_ErrorStatus").Value = 2 Then  
                ' f_retval = "2"  
            End If  
        Catch ex As Exception  
            'f_retval = "9"  
        End Try  
        Return f_regnlist  
    End Function  

<script type="text/javascript">  
        function get_RegnList() {  
            var isValid = true;  
            var f_db = {};  
            var recid = $('#<%=lblrowid.ClientID%>').text();  
            f_db.tblrowid = $.trim(recid);  
                type: "POST",  
                url: "RegnBrGenerationList.aspx/getRegnList",  
                //data: '{f_db: ' + JSON.stringify(f_db) + '}',  
                data: "{'recid':'" + $.trim(recid) + "'}",  
                contentType: "application/json; charset=utf-8",  
                dataType: "json",  
                success: function (response) {  
                    var ddlRegnList = $("[id*=ddl_RegionName]");  
                    var ddlBranchList = $("[id*=ddl_BranchName]");  
                    //ddlRegnList.empty().append('<option selected="selected" value="0"></option>');  
                    $.each(response.d, function () {  
                <%--success: function (response) {  
                    var result = response.d;  
                    if (result == '1') {  
                        alert("Demise Date data Authentication for this Member has been successfully completed!");  
                        $("#<%=txt_GeneralRemarks.ClientID %>").val("");  
                //    else if (result == '2') {  
                //        alert("Sorry, This transaction already exists, due to this unable to save this data!");  
                //    }  
                //    else {  
                //        alert("Sorry! There is an err. on Data Transmission, please contact IT Admin!");  
                //    }  
                error: function (data, ex) {  
                    alert("Sorry there is err. " + data + " Ex: " + ex);  
            //return false;  
A set of technologies in the .NET Framework for building web applications and XML web services.
1,211 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 4,576 Reputation points Microsoft Employee

    Hi @Aypn CNN ,

    It looks like you're returning the result to ajax function, and populating the items of dropdownlist. Then you just need to return the results as a collection to implement this requirement.

    Here is a example:

    aspx.cs code:

    Shared list1 As List(Of ListItem)  
        Shared list2 As List(Of ListItem)  
        Public Shared Sub InitialDataSource()  
            list1 = New List(Of ListItem)()  
            list2 = New List(Of ListItem)()  
            list1.Add(New ListItem("Item1", "Value1"))  
            list1.Add(New ListItem("Item2", "Value2"))  
            list2.Add(New ListItem("Option1", "Value1"))  
            list2.Add(New ListItem("Option2", "Value2"))  
            list2.Add(New ListItem("Option3", "Value3"))  
        End Sub  
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        End Sub  
        Public Shared Function getLists() As IEnumerable(Of List(Of ListItem))  
            Dim result As List(Of List(Of ListItem)) = New List(Of List(Of ListItem))()  
            Return result  
        End Function  

    And get the lists in ajax function:


    Best regards,
    Xudong Peng

    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments