how to assign asp.net dropdownlist id and text in jquery using ajax service?

Jquery


<script type="text/javascript">

    $(document).ready(function () {
        getCat();

    });

    function getCat() {

        $.ajax({
            type: "POST",
            url: "Pro.aspx/Select",

            dataType: "json",
            data: {},
            contentType: "application/json; charset=utf-8",
            success: function (data) {

                var jsdata = JSON.parse(data.d);

                $.each(jsdata, function (key, value) {
                    $('#DDLCat').append($
("<option</option>").val(value.id_fk).html(value.Cat));

                });
            },
            error: function (data) {
                alert("Problom on Loading....");
            }
        });
    }

</script>


ASPX Code


<asp:DropDownList ID="DDLCat" class="input-xlarge" runat="server" ClientIDMode="Static" />



C# Code

you can return data to datatable.

     dt = b1.DDL(d1);

   List<Def_SubCat> listcat = new List<Def_SubCat>();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Def_SubCat scat = new Def_SubCat();

                    string dctcode = dt.Rows[i]["Cat_code"].ToString();

                    scat.id_fk = Convert.ToInt32(dt.Rows[i]["Category_Id"].ToString());
                    scat.Catecode = dt.Rows[i]["Cat_code"].ToString();
                    scat.Cat = dt.Rows[i]["Category"].ToString() + " [ "+ dctcode + " ] " ;
                    listcat.Insert(i, scat);
                }
            }
            JavaScriptSerializer JSS = new JavaScriptSerializer();
            return JSS.Serialize(listcat);



Comments

Popular posts from this blog

Unable to perform operation on the item is locked in workspace

Maximum Stored Procedure Function Trigger or View Nesting Level Exceeded (limit 32) in SQL Server

Insecure cookie setting: missing Secure flag