Json Dropdown Postback Response

时间:2016-07-19 12:35:06

标签: c# jquery asp.net

我的Ajax电话:

$(document).ready(function()
{
    $.ajax({
        type: "POST",
        url : "HazzardsDashBoards.aspx/GetReport",
        data: "{}",
        contentType: 'application/json',
        dataType: 'json',

        complete: function (jqXHR)
        {
           var data = JSON.parse(jqXHR.responseText);
           TrendChart(data);
        },
        error: function (error) {
           alert("error");
        }
    });

    $(".ddlChange").change(function () {
        $.ajax({
            type: "POST",
            url : "HazzardsDashBoards.aspx/GetReport1",
            data:  JSON.stringify({ company: $("#ddl_PermitCmpny").val(), dept: $("#ddl_Agency").val() }),
            contentType: 'application/json',
            dataType: 'json',
            complete: function (jqXHR)
            {
                var data = JSON.parse(jqXHR.responseText);
                TrendChart(data);
            },
            error :function(jqXHR,textStatus,errorThrown)
            {
                alert("An error occurred whilst trying to contact the server: " + jqXHR + " " + textStatus + " " + errorThrown);
            }
        });
    });
});

服务器方法:

[WebMethod]
public static List<Dictionary<string, object>> GetReport()
{
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();

    try
    {

            string strQry = string.Empty;

            strQry = " select YEAR(dt_ModifiedOn)[date],COUNT(*) [HazardCount] from tbl";
            strQry += " where int_PluginID = 4 and int_FeatureID=35  ";
            strQry += " group by year(dt_ModifiedOn) ";

            using (commonManager)
            {
                DataTable dt = commonManager.ExecuteDataTable(strQry);

                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
            }
        }


    catch (Exception ex)
    {

        throw ex;
    }
    return rows;
}

[WebMethod]
public static List<Dictionary<string, object>> GetReport1(string company, string dept)
{
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();

    try
    {
        string strQry = string.Empty;

        strQry = " select YEAR(mwl.dt_ModifiedOn)[date],COUNT(*) [HazardCount] from tbl";
        strQry += " left outer join tbl1 thzd on thzd.int_HAZARD_ID = mwl.str_ObjectID";
        strQry += " where int_PluginID = 4 and int_FeatureID=35 ";
        if (company != "")
        {
            if (company == "1")
            {
                strQry += " and  str_ReportFromType = 'E'";
            }
            else
            {
                strQry += " and  str_ReportFromType = 'C'";
            }
        }

        if (dept != null && dept != string.Empty)
        {
            if (company == "1")
            {
                strQry += " and thzd.str_72_ME = '" + dept + "' ";
            }
            if (company == "2")
            {
                strQry += " and thzd.smallint_5865_ME = '" + dept + "' ";
            }

        }


        strQry += " group by year(mwl.dt_ModifiedOn) ";

        using (commonManager)
        {
            DataTable dt = commonManager.ExecuteDataTable(strQry);

            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
        }
    }
    catch (Exception ex)
    {

        throw ex;
    }
    return rows;
}

我对两个ajax调用都获得相同的json响应,在下拉选择之后我也得到相同的json响应。 下拉选择后数据被覆盖,数据未被反映。

Ajax调用正在触发并转到相应的方法,但数据未被反映并显示相同的数据。 我需要在下拉列表更改和页面加载时为不同的ajax调用获取不同的json响应。

1 个答案:

答案 0 :(得分:0)

请注意,您的第一个AJAX调用将在每次页面加载时运行。我怀疑在您调用GetReport1()之后发生了回发和jQuery加载事件通过调用TrendChart()来覆盖GetReport()逻辑的运行。

您可以在alert('Page Loading...');之后放置$(document).ready(function(){语句来测试此理论。

您可能正在使用导致回发的ASP.NET DropDownList控件,如果是这样的话,请尝试将其替换为简单的HTML <select>并查看是否可以解决您的问题