1.首先页面要继承ICallbackEventHandler
2.定义变量,用于传值protected string _callbackResult = "";
3.在 Page_Load里声明:
string cbReference = Page.ClientScript.GetCallbackEventReference(this,"arg","getResultFromServer","context");
string cbScript = "function useCallBack(arg,context){" + cbReference + "}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "useCallBack", cbScript, true);
4.定义ICallbackEventHandler必须实现的两个方法:
public void RaiseCallbackEvent(string str)
{
DataSet ds = prc.proBasicInfoSelect(str);
if (ds.Tables[0].Rows.Count > 0)
{ _callbackResult = ds.Tables[0].Rows[0]["proManager"].ToString(); }
}
public string GetCallbackResult()
{
return _callbackResult;
}
5.客户端处理:
客户端控件只需要调用useCallBack('','')方法即可,如果是按钮可用onclick="useCallBack('','');"调用
例外定义函数获得变量:
function getResultFromServer(arg,context)
{
var lblPM = document.getElementById("lblPM");
lblPM.innerText = arg;
}