【问题标题】:User Control controls are null用户控制控件为空
【发布时间】:2018-01-14 13:49:17
【问题描述】:

我正在使用Jquery 调用webService.asmx,在该服务中我正在检索usercontrolcontrol 的值以将它们保存在数据库中,但用户控件抛出了NullReferenceException

这里是Ajaxcall

function SaveEdit()
    {
        $.ajax({
            type: "POST",
            url: "Services/RatiosSettingsService.asmx/UpdateRatios",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) { }
        });
    }

这是WebServicecode

[WebMethod]
    public void UpdateRatios()
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Crs2"].ConnectionString))
        {
            ADO ado = new ADO();
            List<SqlParameter> parameters = new List<SqlParameter>();
            ucOtherRatios2 obj = new ucOtherRatios2();

            Dictionary<string, int> hs = obj.GetHsChks();

            foreach (KeyValuePair<string, int> item in hs)
            {
                SqlParameter para = new SqlParameter(item.Key, item.Value);
                parameters.Add(para);
            }

            con.Open();
            ado.CUDSp("UpdateRatios", "spUpdateClientRatios",parameters,con);
            con.Close();
        }
    }

这是在检索控件值的 usercontrol 方法中发生异常的地方

public Dictionary<string, int> GetHsChks()
    {
        Dictionary<string, int> chks = new Dictionary<string, int>();

        chks.Add("@siAnalysisOtherRatiosHistorical1", Convert.ToInt32(chkOthWcHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical2", Convert.ToInt32(chkOthWiHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical3", Convert.ToInt32(chkOthTlgHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical4", Convert.ToInt32(chkOthEiHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical5", Convert.ToInt32(chkOthEcHs.Checked));

        chks.Add("@siAnalysisOtherRatiosHistorical6", Convert.ToInt32(chkOthEicHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical7", Convert.ToInt32(chkOthEsHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical8", Convert.ToInt32(chkOthEtHs.Checked));

        return chks;
    }

它说复选框为空

【问题讨论】:

    标签: c# jquery asp.net web-services


    【解决方案1】:

    您无法从 web 方法访问页面的控件。 由于 web 方法不携带页面状态。这不是一个完整的回发。相反,只有会话 cookie 随请求一起传播。您必须执行整页回发才能获取或设置控件值。 或者您必须通过 AJAX post 方法发送控件的值。

    您可以通过以下链接获取更多详细信息:

    How to access page controls inside a static web method?

    【讨论】:

    • 我已经更改了我的代码..我正在从无 webMethod 调用用户控制方法,我使用回发按钮单击并仍然给我那个空异常
    • 在回发调用的情况下,您无法访问控件,因为当您进行 AJAX 回发时,它不是整页回发。因此,您需要以 JSON 形式从 AJAX 回发方法向您的 .net 方法发送数据。
    【解决方案2】:

    我在 jQuery 中看到一件事有什么好改变的。如果您只获取 JSON 数据,Maby 最好在 jQuery 中使用 $.getJSON() 函数。在那里您可以使用.done() 获取数据并使用.fail() 进行调试。

    接下来是设置 POST 或 GET 变量以将数据传递到您的 JSON 文件以获取正确的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 2020-01-03
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多