简体   繁体   English

用户控件控件为空

[英]User Control controls are null

I'm calling a webService.asmx using Jquery and inside that service i'm retrieving a usercontrol control's values to save them in the database but the user control has thrown a NullReferenceException 我正在使用Jquery调用webService.asmx并且在该服务内部,我正在检索usercontrol控件的值以将它们保存在数据库中,但是用户控件抛出了NullReferenceException

here is Ajax call 这是Ajax电话

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

and this is WebService code 这是WebService代码

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

and here is where the exception happened inside usercontrol method that retrieve the controls values 这是在获取控件值的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;
    }

it says that checkbox is null 它说复选框为空

You can't access page's controls from a webmethod. 您无法通过网络方法访问页面的控件。 Since web methods don't carry the page state. 由于网络方法不带有页面状态。 It isn't a full postback. 这不是完整的回发。 Instead just the session cookie travels with the request. 取而代之的是,会话cookie随请求一起移动。 You have to do a full page postback to get or set the control values. 您必须进行整页回发才能获取或设置控件值。 Or you have to send the values of the controls through AJAX post method. 或者,您必须通过AJAX post方法发送控件的值。

You can get more details on below link: 您可以通过以下链接获取更多详细信息:

How to access page controls inside a static web method? 如何访问静态Web方法中的页面控件?

I see one thing in jQuery what would be good to change. 我在jQuery中看到一件事,那就是改变是有好处的。 Maby is better to use $.getJSON() function inside jQuery if you only getting JSON data. 如果仅获取JSON数据,Maby最好在jQuery中使用$ .getJSON()函数。 There you can use .done() to get data and .fail() for debbug. 在那里,您可以使用.done()获取数据,并使用.fail()进行调试。

Next thing is setup POST or GET variables to pass data to your JSON file to get proper data. 接下来是设置POST或GET变量,以将数据传递到JSON文件以获取正确的数据。

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

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