我有一個更新面板內的gridview,我有一個JavaScript調用使用jQuery的頁面方法。我希望頁面方法根據從ajax調用收到的參數來刷新gridview。ajax與頁面方法
到目前爲止,我有以下幾點:
1)在JavaScript中,有調用頁面方法的功能:
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.
}
我想在頁面方法中做的事情是:1)用新的日期參數調用LoadCallHistory,2)告訴GridView MyGrid用TheCallHistory中的新數據重新綁定。
我在爲這個頁面方法而努力;它不工作,我卡住了。這是如何完成的?
爲什麼它失敗?你得到了什麼結果?您是否試過在調試模式下放置斷點並逐步執行代碼以確保'TheCallHistory'具有它應該具有的數據?另外,你確定一個gridview可以綁定到一個源,如List? –
2011-02-10 00:45:03
還有更多的頁面比我可以發佈,但簡而言之,是的,一切工作在頁面加載。在updatepanel內部還有排序和分頁功能,並且無刷新工作。到目前爲止在頁面方法我有:\t \t var test = new Pages_CallHistory(); \t \t test.LoadCallHistory(TheNeDDate.Date); \t \t \t \t我需要綁定MyGrid並將其獲取到頁面。 – frenchie 2011-02-10 00:48:56