使用c#ashx处理程序调用$ .ajax不会在第二次调用时刷新结果,但会有成功返回代码

时间:2016-07-08 11:22:27

标签: javascript c# asp.net ajax ashx

我已经坐了一段特别毛茸茸的问题,现在一个组合框在页面上其他地方的ajax调用后没有重新填充。我使用DevExpress控件,但唯一的区别是我如何得到字段的值。一些(已消毒的)代码段可能会有所帮助:

aspx页面:

///Populates the Combobox on demand using an AJAX call
function PopulateCombo(){
        $.ajax({
            url: '/MyHandler.ashx',
            contentType: 'application/json; charset=utf-8',
            responseType : 'json',
            data : { userID: ASPxHiddenFieldData.Get('userID') },
            success: function(result, status){
                doPopulateCombo(result, status);
            },
            failure: onPopulateComboFailure //The call doesn't fail so this isn't used
        }); 
}

function doPopulateCombo(result,status){
alert(status); //this always ends up saying success
var obj = JSON.parse(result); //this parses fine, but on the second and subsequent calls it remains the old result set, so the combobox content never changes
    <%=ASPxComboBoxOrigin.ClientInstanceName%>.ClearItems();
    for(var k in obj){
        var listitem = new Array(obj[k].description,obj[k].detail);
        <%=ASPxComboBoxOrigin.ClientInstanceName%>.AddItem(listitem);
    }

}

C#

public class MyHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int UserID = int.MinValue;
        int.TryParse(context.Request["userID"], out UserID);
        List<AddressObject> returnlist = new List<AddressObject>();
        List<UserAddress> addresslist = UserAddress.GetAddressByUser(UserID);
        if (favouritelist != null)
        {
            foreach (UserAddress address in favouritelist)
            {
                returnlist.Add(GenerateAddress(address.AddressDescription, address.AddressDetail));
            }
        }
        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
        string addresses = javaScriptSerializer.Serialize(returnlist);
        context.Response.ContentType = "text/html";
        context.Response.Write(addresses);
    }

    public bool IsReusable
    { get  { return false;} } //I have tried both true and false

    private AddressObject GenerateAddress(string description, string detail)
    {
        var address = new AddressObject();
        address.description = description;
        address.detail = detail;
        return address;
    }
    private class AddressObject
    {
        public string description { get; set; }
        public string detail { get; set; }
    }
}

所以最终发生的事情是,我第一次选择用户时,会填充正确的结果。但是当我更改用户时,我仍然以旧用户列表结束。我已经调试过,可以看到处理程序中第一次绕过breakboint被触发,而在第二次调用时,处理程序根本没有被击中,但是ajax调用仍然返回成功结果。虽然结果集仍然是旧的。任何想法如何让我总是运行处理程序,而不是使用一些我不知道的地方存储的结果?

编辑:

似乎事件处理程序仅在我第一次导航到页面时被调用。即使导航然后再返回也不会调用它。我怀疑它与会话有关。欢迎任何想法。

此致

0 个答案:

没有答案