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

Aypn CNN 386 Reputation points
2022-12-25T15:22:12.713+00:00

Hi,

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 = ""  
        Try  
            sCon.Open()  
            sCmd.CommandType = CommandType.StoredProcedure  
            sCmd.CommandText = "LGSP_GetRegionBranch"  
            sCmd.Parameters.Clear()  
            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  
            }  
            sCmd.Parameters.Add(param)  
            dtadp.Fill(dtset)  
            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)))  
                Next  
            ElseIf sCmd.Parameters("@o_ErrorStatus").Value = 2 Then  
                ' f_retval = "2"  
            End If  
        Catch ex As Exception  
            'f_retval = "9"  
        Finally  
            sCon.Close()  
        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);  
            $.ajax({  
                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 () {  
                        ddlRegnList.append($("<option></option>").val(this['Value']).html(this['Text']));  
                    });  
                      
                    //console.log(response.d);  
                },  
  
                <%--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("");  
                        $(".modal-backdrop").remove();  
                        $("#Popu_HOApproval").modal('hide');  
                        disbl_rbt();  
                    }--%>  
                //    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);  
                    $(".modal-backdrop").remove();  
                    $("#Popu_HOApproval").modal('show');  
                }  
  
            });  
            //return false;  
        }  
    </script>  
ASP.NET
ASP.NET
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
    2022-12-27T04:41:48.457+00:00

    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  
      
        <WebMethod()>  
        Public Shared Function getLists() As IEnumerable(Of List(Of ListItem))  
            Dim result As List(Of List(Of ListItem)) = New List(Of List(Of ListItem))()  
            InitialDataSource()  
      
            result.Add(list1)  
            result.Add(list2)  
            Return result  
        End Function  
    

    And get the lists in ajax function:
    274177-image.png

    Result:
    274105-result.gif

    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