当选择值设置为服务器端时,不会触发下拉列表的onchange

时间:2018-04-26 21:06:26

标签: javascript c# jquery asp.net

我有一个下拉列表,其中包含用于填充jQuery数据表的客户端onchange。如果更改了另一个下拉列表的值,并且在服务器端重新填充第一个下拉列表并设置其选定值,则不会触发其onchange并且数据表不会使用新数据进行更新。

$(document).on('change', '#ddlFacility', function () {debugger
    var facilityID = this.value;

    if (facilityID > -1) {
        var facCertUrl = "services/easg.asmx/GetSomethingByFacilityID";
        var facCertParams = "{ 'FacilityID': '" + facilityID + "', 'Date': '" + $("#hfStartDate").val() + "' }";
        populteTable(facCertUrl, facCertParams, tblFacCert);
    }
});

function populteTable(ws_url, parameters, table) {
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: ws_url,
        cache: false,
        data: parameters,
    }).done(function (result) {debugger
        table.clear().draw();
        if (!result || result.d === "") {
            $('#divGrid').hide();
        }
        else {
            jResult = JSON.parse(result.d);
            table.rows.add(jResult).draw();
            table.columns([4, 5, 6, 7, 8, 9, 10, 11]).visible(false);
            $('#divGrid').show();
        }
    }).fail(function (jqXHR, textStatus, errorThrown) {debugger
        $('#divGrid').hide();
        alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
    });
}

var tblFacCert = $("#fcTable").DataTable({
    ....
});

服务器端,当第二个下拉值改变时:

protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
{
    string sFacID = somehow_get_FacID();
    ListItem li = ddlFacility.Items.FindByValue(sFacID);

    if (null != li)
    {
        li.Selected = true; // This does not trigger onchange

        // This changes the selection and runs the onChange() event; but data table is now empty!
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "ddlFacility", "$(\"#ddlFacility\").val(\"" + li.Value + "\").change();", true);
    }
}

0 个答案:

没有答案