Navigation

Wednesday 20 January 2016

Bind ASP Dropdown by JSON

aspx Code

<table>
                                            <tr>
                                                <tr>
                                                    <td>Location :
                                                    </td>
                                                    <td>
                                                        <asp:DropDownList ID="ddlLocationRoleUp" runat="server" onChange="SelectLocationAccessUp()">
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                            </tr>

</table>


 JQuery Code
 function fnSelectLocationUp(locationGroupId, shiftroleid) {

        var Dropdown = $('#<%=ddlLocationRoleUp.ClientID %>');
        Dropdown.empty();
        if (locationGroupId > 0) {
            $.ajax({
                type: "POST",
                url: "job.aspx/FillSchedArea",
                data: "{'locationGroupId':'" + locationGroupId + "','shiftroleid':'" + shiftroleid + "'}",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: false,
                success: function (response) {
                    //alert(response.d);
                    var selVal = 0
                    Dropdown.append(new Option("Select", 0));
                    $.each(response.d, function (index, item) {
                        Dropdown.append(new Option(item.LocationName, item.LocationId));
                        selVal = item.SchedAreaId
                    });
                    Dropdown.val(selVal);
                    SelectLocationAccessUp();
                },
                failure: function () {
                    alert("Sorry,there is a error!");
                }
            });
        }
        else {
            Dropdown.append(new Option("Select", 0));
        }
    }


C# Code
[WebMethod]
        public static List<clsLocation> FillSchedArea(string locationGroupId, string shiftroleid)
        {
            List<clsLocation> strLocation = new List<clsLocation>();
            DataSet ds = LocationGroup.GetLocationByLocGroupId(Convert.ToInt32(locationGroupId), Convert.ToInt32(shiftroleid));
            if (ds.Tables[0].Rows.Count > 0)
            {
                string SchedAreaIdSel = "0";
                if (ds.Tables != null && ds.Tables[1].Rows.Count > 0)
                {
                    SchedAreaIdSel = ds.Tables[1].Rows[0]["SchedAreaId"].ToString();
                }
                foreach (DataRow item in ds.Tables[0].Rows)
                {

                    string LocationId = item["SchedAreaId"].ToString();
                    string LocationName = item["Name"].ToString();
                    strLocation.Add(new clsLocation()
                    {
                        LocationId = LocationId,
                        LocationName = LocationName,
                        SchedAreaId = SchedAreaIdSel
                    });
                }

            }
            //ddlLocationRoleUp.
            return strLocation;
        }

No comments:

Post a Comment