ASP.NET使用jQuery AJAX更新列表

时间:2015-06-17 14:44:23

标签: jquery asp.net ajax setinterval webmethod

我在Default.aspx页面上显示一个列表,其中包含从数据库中提取的对象(人员):

$($(".test").get(3)).before(newElement);

填写表格的方法:

[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static void getPersonList()
{
    DatabankService dbs = new DatabankService();
    persons = dbs.getPersons());
    // fillTable(persons) is not possible here (not possible in static method).
    // fillTable code also cannot be place here because it uses UI Elements from default.aspx to create the table (same problem as above).
}

每10秒钟应该再次调用此方法而不刷新页面,以便更新列表:

public void fillTable(List<Person> persons)
{
    // make colums and rows and will with data (foreach person in..)
}

问题是我无法从静态[WebMethod] getPersonList()调用fillTable函数。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

我最终找到了一个很好的解决方案。因为看起来无法在静态[WebMethod] getPersonList()方法中运行fillTable()方法(非静态),所以我无法使用Code-Behind刷新内容,我需要在静态中访问UI元素方法。我所做的是将person-list转换为JSON对象并将其发送到我的jQuery成功函数(所需的UI元素可定位):

// getPersonList()
...
string result = JsonConvert.SerializeObject(persons);
return result;   

// jQuery success

success: function (data) {
    response = JSON.parse(data.d);

    $.each(response, function (i, item) {
        // fill table using jQuery
    });
}