1.使用局部刷新,即不更新整个页面,利用ajax技术实现局部数据更新
2.首先页面需继承ICallbackEventHandler,并需实现该接口的两个方法
public void RaiseCallbackEvent(string str)
{
//该方法获取要显示的数据
}
public string GetCallbackResult()
{
//该方法返回值
return _callBackResult;
}
3.页面Page_Load事件注册脚本:
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "getResultFromServer", "context");
string cbSrcipt = "function useCallBack(arg,context){" + cbReference + "}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "useCallBack", cbSrcipt, true);
4.html页面脚本定义
function getResultFromServer(arg,context)
{
//这里可以给页面上的控件赋值,即从后台获取的数据,例如;
document.getElementById("txtPlanCost").value = arg;
}
5.调用,给页面上的某个控件设置调用,例如给也页面上一个下拉列表设置异步调用,当变更选项即局部刷新数据:
<asp:DropDownList ID="ddlStage" runat="server" Width="185px" onchange="useCallBack(this.options[this.selectedIndex].value);">
</asp:DropDownList>