【问题标题】:ajax with page method带有页面方法的ajax
【发布时间】:2011-02-11 01:33:13
【问题描述】:

我在更新面板中有一个gridview,我有一个使用jquery 调用页面方法的javascript。我希望页面方法根据它从 ajax 调用接收到的参数刷新 gridview。

到目前为止,我有以下内容:

1)在javascript中,有一个调用page方法的函数:

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页面中:

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

3) 在后面的代码中:

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.
    }

我想在 page 方法中做的是 1) 使用新的日期参数调用 LoadCallHistory 和 2) 告诉 gridview MyGrid 使用 TheCallHistory 中的新数据重新绑定。

我正在努力使用这种页面方法;它不起作用,我被卡住了。这是怎么做到的?

【问题讨论】:

  • 为什么会失败?你得到什么结果?您是否尝试过设置断点并在调试模式下单步执行代码以确保“TheCallHistory”具有应有的数据?另外,您确定 gridview 可以绑定到诸如 List 之类的源吗?
  • 页面上的内容比我可以发布的要多,但简而言之,是的,页面加载时一切正常。更新面板内还有排序和分页功能,无需刷新即可工作。到目前为止,在页面方法中我有: var test = new Pages_CallHistory(); test.LoadCallHistory(TheNewDate.Date);我需要绑定 MyGrid 并将其放到页面中。

标签: asp.net


【解决方案1】:

好的,所以解决方案是在javascript中使用_doPostBack:

   __doPostBack('MyPanel', DateString);

page 方法仅用于发送和接收数据,不用于在更新面板上进行回发。

【讨论】:

    【解决方案2】:

    看看我对这个相关问题here 的回答。简而言之,您创建一个新的网格实例并手动捕获其输出。

    【讨论】:

    • 将gridview绑定到数据源的行不起作用;我想不通。绑定发生在您的解决方案中的什么位置?
    • 我有“searchControl.Search()”这一行。这将是您执行绑定的地方,例如“myGrid.DataSource = dataSource;myGrid.DataBind();”。执行这些后,可以使用 RenderControl 函数捕获内容。
    • 这些是我已经拥有的行并引发异常:“对象引用未设置为对象的实例。”
    • 这表明你的 myGrid 对象没有被初始化,这听起来有点奇怪。当它尝试引用 DataSource 属性时,找不到要访问的对象引用。
    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 2013-03-03
    • 1970-01-01
    • 2018-09-07
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多