Ajax和页面的方法 [英] ajax with page method

查看:109
本文介绍了Ajax和页面的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UpdatePanel内的GridView和我有使用jquery调用页面方法的JavaScript。我想在页面方法根据从Ajax调用接收参数刷新GridView的。

I have a gridview inside an updatepanel and I have a javascript that calls a page method using jquery. I'd like the page method to refresh the gridview based on the parameter it receives from the ajax call.

到目前为止,我有以下几点:

So far, I have the following:

1)在JavaScript中,有调用页面方法的功能:

1) in the javascript, there's a function that calls the page method:

function GetNewDate(thedateitem) {

    DateString = (valid json date format that works)

    $.ajax({
        type: "POST",
        url: "./CallHistory.aspx/ResetDate",
        contentType: "application/json; charset=utf-8",
        data: DateString,
        dataType: "json",
        success: successFn,
        error: errorFn
    }) 
};

2)在aspx页面我有:

2) In the aspx page I have:

    <asp:UpdatePanel ID="MyPanel" runat="server">
        <ContentTemplate>
                <asp:GridView ID="MyGrid">

3)在code背后:

3) In the code behind:

public partial class Pages_CallHistory : System.Web.UI.Page
{
    List<ViewCallHistoryModel> TheCallHistory;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TheDate = new DateTime(2011, 1, 13);
            LoadCallHistory(TheDate);
            MyGrid.Datasource = TheCallHistory;
            MyGrid.Databind;
        }
    }

    protected void LoadCallHistory(DateTime TheDate)
    {
        linq query that fills the variable TheCallHistory
    }

    [WebMethod]
    public static void ResetDate(DateTime TheNewDate)
    {
        var test = new Pages_CallHistory();
        var test2 = test.LoadCallHistory(TheNewDate.Date);
        //test2 loads fine

        test.GridCallHistory.DataSource = test2; 
        //this is not underlined but bugs at runtime
        //Object reference not set to an instance of an object.

        test.GridCallHistory.DataBind();
        test.MyPanel.Update(); 
        //this is not underlined but doesn't get executed because
        //because it stops at the line above

        //I'd like to update the content of 
        //the gridview on the page with the updated gridview.
    }

我想什么页面的方法做的是1)调用LoadCallHistory与新的日期参数和2)告诉gridview的MyGrid与在TheCallHistory新的数据重新绑定。

What I'd like to do in the page method is 1) call LoadCallHistory with the new date parameter and 2) tell the gridview MyGrid to rebind with the new data that's in TheCallHistory.

我在这个页面的方法挣扎;它不工作,我卡住了。如何做到这一点?

I'm struggling with this page method; it's not working and I'm stuck. How is this done?

推荐答案

确定这样的解决方案是在JavaScript中使用_doPostBack:

ok so the solution is to use _doPostBack in javascript:

   __doPostBack('MyPanel', DateString);

该页面的方法是发送且仅接收数据,而不是在做的UpdatePanel回发。

The page method is for sending and receiving data only, not for doing postbacks on updatepanels.

这篇关于Ajax和页面的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆