Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two <select></select> elements in my web-app one is pre-populated with country names and I want to populate the other one with states based on the country selected. I want to use jQuery and asp.net web methods (I don't have a <Form runat="server"> tag in my page) below is my webpage (html):

HTML
<form role="form" method="post" action="Login.aspx?action=register">

    <div class="form-group">
        <label for="exampleInputCountry">Country</label>
        
        <select class="form-control" id="exampleInputState" name="exampleInputState">
        </select>
    </div>
    <div class="form-group">
        <label for="exampleInputCity">City/Town</label>
        ^__i class="fa fa-font">
        <input type="text" class="form-control" id="exampleInputCity" name="exampleInputCity" />
    </div>
    <div class="form-actions">
        <button type="submit" class="btn btn-success">Sign Up</button>
    </div>
</form>
<script type="text/javascript">
    $("#exampleInputCountry").change(function () {
        var str;
        $("#exampleInputCountry option:selected").each(function () {
            str += $(this).text() + " ";
        });
        $.ajax({
            url: "/default.aspx/GetStates",
            dataType: 'json',
            data: { country: str },
            success: function (data) {
                $("#exampleInputState").fillSelect(data);
            }
        });

        $.fn.fillSelect = function (data) {
            return this.clearSelect().each(function () {
                if (this.tagName == 'SELECT') {
                    var dropdownList = this;
                    $.each(data, function (index, optionData) {
                        var option = new Option(optionData.Text, optionData.Value);
                        if ($.browser.msie) {
                            dropdownList.add(option);
                        } else {
                            dropdownList.add(option, null);
                        }
                    });
                }
            });
        };

        $.fn.clearSelect = function () {
            return this.each(function () {
                if (this.tagName == 'SELECT')
                    this.options.length = 0;
            });
        };

    }).change();
</script>



And this is the codebehind webmethod:

<webmethod> _
VB
Public Function GetStates(country As String) As String
    Dim ConnectionString As String
    ConnectionString = ConfigurationManager.ConnectionStrings("SqlConnectionString").ToString.Trim
    Dim SqlConnection As New Data.SqlClient.SqlConnection(ConnectionString)
    Try
        SqlConnection.Open()
    Catch ex As Exception
        Response.Redirect("ConnectionFailure.aspx")
    End Try
    Dim SqlCommand As New Data.SqlClient.SqlCommand("", SqlConnection)
    SqlCommand.CommandText = "Select * From States Where Country='" & country & "'"
    Dim tblStates As New XDataTable(SqlCommand)
    tblStates.TableName = "States"
    Return Newtonsoft.Json.JsonConvert.SerializeObject(tblStates)

End Function


It's not working, any help will be appreciated.
Posted
Updated 27-Nov-13 1:46am
v3
Comments
Going through the whole code and finding the problem is difficult.
Instead debug your code and find where exactly is the issue.
For client-side JavaScript/jQuery issues, you can check the FireBug console window in FireFox.
martene 27-Nov-13 7:27am    
I will try to make my post smaller.

1 solution

 
Share this answer
 
v2
Comments
martene 27-Nov-13 7:55am    
I've read your article and it was enlightening but tell me just one thing, Is it necessary to use <form runat="server"> to run this code or just <form action=""> would suffice.
Suvabrata Roy 27-Nov-13 8:22am    
http://www.asp.net/ajaxlibrary/ajaxcontroltoolkitsamplesite/cascadingdropdown/cascadingdropdown.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900