页面中:
<input type="text" οnblur="callToServer();" name="txtUsername" style="width: 160px" maxlength="20"/> 服务器控件一样可以用Attributes.Add()添加:οnblur="callToServer();" <span id="Question">*********</span>
<script type="text/javascript"> function callToServer() { var param = document.getElementById("txtUsername").value; var context = ""; CallTheServer(param,context); } </script>
cs文件中
public
partial
class
HrUser_ForgetPassword : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
...
{ public string reString = " 您输入的用户名不存在 " ; public void RaiseCallbackEvent(String eventArgument) ... { Model.UserLogin model = new Model.UserLogin(); BLL.UserLogin bll = new BLL.UserLogin(); model = bll.GetModel(eventArgument); if (model != null ) reString = model.Answer; } public string GetCallbackResult() ... { return reString.ToString(); } protected void Page_Load( object sender, EventArgs e) ... { ClientScriptManager cs = Page.ClientScript; StringBuilder context = new StringBuilder(); context.Append( " function ReceiveServerData(arg, context) " ); context.Append( " { " ); context.Append( " Question.innerText = arg; " ); context.Append( " value = arg; " ); context.Append( " } " ); String cbReference = cs.GetCallbackEventReference( this , " arg " , " ReceiveServerData " , context.ToString()); String callbackScript = " function CallTheServer(arg, context) { " + cbReference + " ; } " ; cs.RegisterClientScriptBlock( this .GetType(), " CallTheServer " , callbackScript, true ); } }
这样在文本框失去焦点时<span id="Question">*********</span>中的文本就会被设置为服务器回传过来的数据