how to set(assign) dropdownlist selected value & radiobutton selected value in jquery from a json data

<asp:RadioButtonList ID="rblGender" runat="server" RepeatDirection="horizontal" Height="30px" Width="130px">
    <asp:ListItem Selected="True" Value="M">Male</asp:ListItem>
    <asp:ListItem Value="F">Female</asp:ListItem>
    </asp:RadioButtonList>    

<asp:DropDownList ID="ddlDepartment" runat="server" Height="20px" Width="100px"  >
    </asp:DropDownList>

Based on a selected id (Webservice returns the details of the employee and i need to show it in the form) Id, Code , Name is working good

$.ajax({
      type: "POST",
      url: "EMService.asmx/GetEmployeeById",
      data: "{ 'Id':'" + id + "' }",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
           var obj = data.d;
           $("#<%=txtId.ClientID%>").val(obj.Id);
           $("#<%=txtCode.ClientID%>").val(obj.Code);
           $("#<%=txtName.ClientID%>").val(obj.Name);
           $("#<%=rblGender.ClientID%>").valueOf(obj.Gender);
           $("#<%=ddlDepartment.ClientID%>").value(obj(Department.Id));
 },
                error: function (result) {
                    alert("Error");
                }
            })
            return false;
        }

Answers


This could be a type while posting but you have missed the } closing brace of ajax success.

try to change this:

$("#<%=rblGender.ClientID%>").valueOf(obj.Gender);
$("#<%=ddlDepartment.ClientID%>").value(obj(Department.Id));

to this:

$("#<%=rblGender.ClientID%>").val(obj.Gender);
$("#<%=ddlDepartment.ClientID%>").val(obj.Department.Id);

you have used valueOf and .value(obj(Department.Id), instead of use just .val() and this .value(obj(Department.Id)); should be .value(obj.Department.Id);


Need Your Help

Json Exception wrong type

java json eclipse gson

I use Gson in my project. But it returns me error

How to scroll to the bottom of a UITableView on the iPhone before the view appears

ios objective-c cocoa-touch uitableview

I have a UITableView that is populated with cells of a variable height. I would like the table to scroll to the bottom when the view is pushed into view.