Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have bound dropdown using jquery at the time of page load; but after selecting a text of that dropdown and submit a button to get the value of that dropdown it shows blank on the server side.
Can anyone help me to solve this problem.
Thanks in advance.

Here is the web service through which i am binding dropdown
[WebMethod]
    public static CountryDetails[] BindDatatoDropdown()
    {
        DataTable dt = new DataTable();
        List<countrydetails> details = new List<countrydetails>();

        using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT ID,Name FROM test", con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                foreach (DataRow dtrow in dt.Rows)
                {
                    CountryDetails country = new CountryDetails();
                    country.ID = Convert.ToInt32(dtrow["ID"].ToString());
                    country.Name = dtrow["Name"].ToString();
                    details.Add(country);
                }
            }
        }
        return details.ToArray();
    }
public class CountryDetails
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

and through this following JQuery i am binding dropdown and it works perferctly
XML
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "AllAccomodation.aspx/BindDatatoDropdown",
            data: "{}",
            dataType: "json",
            success: function (data) {
                $.each(data.d, function (key, value) {
                    $("#ContentPlaceHolder1_typelisting").append($("<option></option>").val(value.ID).html(value.Name));
                });
            },
            error: function (result) {
                alert("Error");
            }
        });
    });
Posted
Updated 21-Jul-12 0:14am
v5

1 solution

No, we can't fix code that you're keeping a secret. All we can do is say that the code you didn't post, is broken. You should break your task in to steps. Are you using AJAX to post the data ? If not, then how the item was selected should not matter. It's not clear to me at all what jquery is doing here ? It's filling the list with data ? I don't think that will work, I think you need to set it up on the server, for postback data to work. Also, if your page behind code is setting a data source before you check the value, that resets the selection.
 
Share this answer
 
Comments
sahabiswarup 21-Jul-12 5:07am    
i have added the code above please check..
Christian Graus 21-Jul-12 5:25am    
So explain to me, you populate this using AJAX, on page load. What on earth is the function of that ? You've just finished getting data from the server, and you ask for more. Why not just send the form to the client populated ? Your viewstate has no idea that this object ever contained anything and so it has no idea that it's expected to find a selected index. This is the most useless use of AJAX I've ever seen. If your drop down list contents changed based on user selection, this is the code I'd use.
sahabiswarup 21-Jul-12 6:07am    
why you are you commenting like that? i have bound dropdown to increase web page performance. is there any thing wrong??
Christian Graus 21-Jul-12 6:21am    
Yes. you made it slower. There's a whole packet being generated for no good reason.

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