简体   繁体   English

通过Jquery AJAX从web方法获取数据后未选中ASP.NET复选框

[英]ASP.NET checkbox not being checked after getting data from webmethod via Jquery AJAX

I am using Jquery ajax to call a webmethod, this webmethod accesses my database and returns a true or false value. 我正在使用Jquery ajax调用Web方法,此Web方法访问我的数据库并返回true或false值。 I want the checkbox to be checked or not based on that boolean value that is being returned. 我希望根据返回的布尔值来选中或不选中该复选框。

Below is the server side code. 下面是服务器端代码。

[WebMethod]
public static bool FillData(string emailAddr)
{
    ConnectionUtility ConnectionObj = new ConnectionUtility();
    //Fill modal with correct data from database
    SqlConnection ConState = ConnectionObj.GetConnectionState();
    SqlCommand cmd = new SqlCommand("Select * from tblEmployee where Email='" + emailAddr + "'", ConState);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    string subscriptionUser = dt.Rows[0]["IsSubscriptionUser"].ToString();
    return Convert.ToBoolean(subscriptionUser);
}

Clientside: 客户端:

function editdata(id, firstname, lastname, email, phonenumber, company, role, empid) {

    document.getElementById("LblHeadderText").innerHTML = "Update";


    <%--  $('#<%=TxtEmail.ClientID%>').attr('readonly', true);--%> //Commented on 12 Dec 2016 to allow user to update username/email 

    jQuery("#<%=TxtId.ClientID%>").val(id);
    jQuery("#<%=TxtFirstName.ClientID%>").val(firstname);
    jQuery("#<%=TxtLastName.ClientID%>").val(lastname);
    jQuery("#<%=TxtEmail.ClientID%>").val(email);

    jQuery("#<%=TxtPhoneNumber.ClientID%>").val(phonenumber);
    jQuery("#<%=DdlAssignRole.ClientID%>").val(role);
    jQuery("#<%=TxtOldAssignedRole.ClientID%>").val(role);

    jQuery("#<%=DpdEmployee.ClientID%>").val(empid);

    jQuery("#<%=btnSubmit.ClientID%>").hide();
    jQuery("#<%=btnUpdate.ClientID%>").show();

    $('[Id*=HfOldEmail]').val(email);

    document.getElementById('<%=btnUpdate.ClientID%>').disabled = false;

    $.ajax({
        type: "GET",
        url: "UserList.aspx/FillData",
        data: JSON.stringify({ emailAddr: email }),
        success: checkCheckBox,
        error: function (r) {
            alert(r.responseText);
        },
        failure: function (r) {
            alert(r.responseText);
        }
    });
}

function checkCheckBox(checked)
{
    $("#<%=chkbxSubscriptionUser%>").prop("checked", checked);
}

I have debugged the server side code and it does work as expected. 我已经调试了服务器端代码,它确实按预期工作。 It returns true or false correctly. 它正确返回true或false。 I have tried every alternative I can think of, I even tried using session variables. 我尝试了所有可以想到的替代方法,甚至尝试使用会话变量。

I'm sure it's an easy fix, but I'm inexperienced with Jquery, and I'm just implementing a feature to an application that was originally contracted out. 我敢肯定这是一个简单的修复程序,但是我对Jquery缺乏经验,并且我只是对最初外包的应用程序实施一项功能。

Help would be greatly appreciated. 帮助将不胜感激。

In Application_Start comment below line Application_Start行下方的注释中

//RouteConfig.RegisterRoutes(RouteTable.Routes);

Make sure that you EnablePageMethods in your ScriptManager 确保在ScriptManager EnablePageMethods

<asp:ScriptManager runat="server" EnablePageMethods="true">

Update checkCheckBox function as below 更新如下的checkCheckBox函数

function checkCheckBox(checked)
{
    //$("#<%=chkbxSubscriptionUser%>").prop("checked", checked);
    $("#<%=chkbxSubscriptionUser.ClientID%>").prop('checked', checked);
}

Update AJAX Call 更新AJAX通话

$.ajax({
    type: "POST",
    url: "UserList.aspx/FillData",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ emailAddr: email }),
    dataType: "json",
    success: function (r) {
        checkCheckBox(r.d);
    }
    , error: function (r) {
        alert(r.responseText);
    },
    failure: function (r) {
        alert(r.responseText);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM