asp.net回调更新gridview
以前写过关于回调的代码,今天想用的时候又找不到了,费了好大劲才搞定,纪录一下,以备后用。
其实比较简单,关键是最后一行调用的函数renderGrid(_grid)。
public string sCallBackFunctionInvocation;
string returnstring;
protected void Page_Load(object sender, EventArgs e)
{
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "processMyResult", "context", "postMyError", true);
}
#region ICallbackEventHandler Members
string ICallbackEventHandler.GetCallbackResult()
{
return returnstring;
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
if (eventArgument == "getservertime")
{
//returnstring = DateTime.Now.ToString();
_grid.DataSource = _sampleData;
_grid.DataBind();
renderGrid(_grid);
}
}
利用ASP.NET中最为强大的特性 - RenderControl。该方法能使我们方便地在服务器端 通过HtmlTextWriter和StirngWriter对象访问到控件的HTML代码。
private void renderGrid(GridView _grid)
{
using ( StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
_grid.RenderControl(htw);
htw.Flush();
returnstring = sw.ToString();
}
}
前台响应函数是:
function processMyResult(returnmessage, context){
window.document.getElementById('Gridview').innerHTML = returnmessage;
}
这样就实现了通过回调函数局部更新gridview。
相关文档:
<connectionStrings>
<add name="qxConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Web/App_Data/db2.mdb;" providerName="System.Data.OleDb"/>
</connectionStrings> ......
记取记录集
create procedure getArticle
as
select * from Article_Content
GO
asp.net 调用方法
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = Data.Connstr();
Conn.Open();
......
第一步:创建对象
第二步:设置属性
①pdsBooks.AllowPaging=true;
②pdsBooks.PageSize=5;
③pdsBooks.CurrentPageIndex=CurrentPage-1;
④pdsBooks.DataSource=BookManage.GetAllBooks();
第三步:绑定数据源
DataList1.DataList=pdsBooks;
DataList1.DataBind(); ......