【问题标题】:ASP.NET checkbox not being checked after getting data from webmethod via Jquery AJAX通过 Jquery AJAX 从 webmethod 获取数据后未检查 ASP.NET 复选框
【发布时间】:2018-03-21 22:24:50
【问题描述】:

我正在使用 Jquery ajax 调用一个 webmethod,这个 webmethod 访问我的数据库并返回一个 true 或 false 值。我希望根据返回的布尔值来检查复选框。

下面是服务器端代码。

[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);
}

客户端:

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);
}

我已经调试了服务器端代码,它确实按预期工作。它正确地返回真或假。我已经尝试了所有我能想到的替代方案,我什至尝试过使用会话变量。

我确信这很容易解决,但我对 Jquery 缺乏经验,而且我只是在最初外包的应用程序中实现一个功能。

我们将不胜感激。

【问题讨论】:

  • 你的javascript函数的“checked”参数中有什么?你确定它是小写的“真”还是“假”?
  • chkbxSubscriptionUser 是否缺少 .ClientID?
  • 为函数 checkCheckBox 检查的参数可以是具有属性 d 的对象,该属性为真或假。
  • FillData 方法是否会被调用?
  • 是的,它确实被调用了,我现在添加了 .ClientID ,但它仍然不起作用,我将尝试 .d 部分。谢谢大家的建议。

标签: javascript c# jquery asp.net ajax


【解决方案1】:

在Application_Start 下一行评论

//RouteConfig.RegisterRoutes(RouteTable.Routes);

确保您的EnablePageMethods 在您的ScriptManager 中

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

更新checkCheckBox函数如下

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

更新 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);
    }
});

【讨论】:

  • 谢谢,但不幸的是,这对我来说不起作用。
  • 你能分享 WebMethod JSON 响应吗
  • 当然,它返回以下内容:'undefined'
  • Application_Start 在哪里?在整个网站的任何地方都找不到该方法或您注释掉的内容,除了更新后的答案仍然无法正常工作。
  • 在 Global.asax 文件位于应用程序根目录中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多