CallBack
参考资料:http://msdn.microsoft.com/en-us/library/ms178208(zh-cn).aspx
aspx.cs页继承ICallbackEventHandler接口,实现不经过回发而以编程方式实现客户端回调。以下是测试例子。
1。客户端注册CallBack事件:
aspx:
代码
<script type="text/javascript">
function HTMLControlCallServerMethod() //setp1
{
var myParam = "myValue";
<%= ClientScript.GetCallbackEventReference(this,"myParam", "ReceiveServerData",null)%>;
}
function ReceiveServerData(rtnValue) //得到GetCallbackResult的返回值,setp4
{
alert(rtnValue);
}
</script>
...
<button onclick="HTMLControlCallServerMethod()">CallServer</button>
function HTMLControlCallServerMethod() //setp1
{
var myParam = "myValue";
<%= ClientScript.GetCallbackEventReference(this,"myParam", "ReceiveServerData",null)%>;
}
function ReceiveServerData(rtnValue) //得到GetCallbackResult的返回值,setp4
{
alert(rtnValue);
}
</script>
...
<button onclick="HTMLControlCallServerMethod()">CallServer</button>
aspx.cs:
代码
private string CallBackValue = string.Empty;
string ICallbackEventHandler.GetCallbackResult() //返回值给客户端,setp3
{
return CallBackValue + ",GetCallbackResult_OK";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) //接收GetCallbackEventReference的参数,setp2
{
this.CallBackValue = eventArgument + ",RaiseCallbackEvent_OK"; //客户端定义的变量
}
string ICallbackEventHandler.GetCallbackResult() //返回值给客户端,setp3
{
return CallBackValue + ",GetCallbackResult_OK";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) //接收GetCallbackEventReference的参数,setp2
{
this.CallBackValue = eventArgument + ",RaiseCallbackEvent_OK"; //客户端定义的变量
}
2。客户端注册多个CallBack事件:
根据实际操作需要,可以单独step1和step4的函数。如果操作类似,可以共用一组函数:
aspx:
代码
<script type="text/javascript">
function HTMLControlCallServerMethod(btn) //setp1
{
var myParm = btn.id;
<%= ClientScript.GetCallbackEventReference(this,"myParm", "ReceiveServerData",null)%>;
}
function ReceiveServerData(rtnValue) //得到GetCallbackResult的返回值,setp4
{
alert(rtnValue);
}
</script>
...
<button id="btnOne" onclick="HTMLControlCallServerMethod(this)">CallServer</button>
<button id="btnTwo" onclick="HTMLControlCallServerMethod(this)">CallServer2</button>
function HTMLControlCallServerMethod(btn) //setp1
{
var myParm = btn.id;
<%= ClientScript.GetCallbackEventReference(this,"myParm", "ReceiveServerData",null)%>;
}
function ReceiveServerData(rtnValue) //得到GetCallbackResult的返回值,setp4
{
alert(rtnValue);
}
</script>
...
<button id="btnOne" onclick="HTMLControlCallServerMethod(this)">CallServer</button>
<button id="btnTwo" onclick="HTMLControlCallServerMethod(this)">CallServer2</button>
aspx.cs:
代码
private string CallBackValue = string.Empty;
string ICallbackEventHandler.GetCallbackResult() //返回值给客户端,setp3
{
return CallBackValue + ",GetCallbackResult";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) //接收GetCallbackEventReference的参数,setp2
{
if (eventArgument == "btnOne") //区分客户端定义的变量
{
this.CallBackValue = "btnOne,RaiseCallbackEvent";
}
if (eventArgument == "btnTwo")
{
this.CallBackValue = "btnTwo,RaiseCallbackEvent";
}
}
string ICallbackEventHandler.GetCallbackResult() //返回值给客户端,setp3
{
return CallBackValue + ",GetCallbackResult";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) //接收GetCallbackEventReference的参数,setp2
{
if (eventArgument == "btnOne") //区分客户端定义的变量
{
this.CallBackValue = "btnOne,RaiseCallbackEvent";
}
if (eventArgument == "btnTwo")
{
this.CallBackValue = "btnTwo,RaiseCallbackEvent";
}
}
3。服务器端注册CallBack事件:
aspx:
代码
<script type="text/javascript">
function SetControlVale(a) //a是服务器端返回的值
{
document.getElementById("tbNow").value = a; //设置tb的值为处理后的结果
}
</script>
...
<asp:TextBox ID="tbNow" runat="server"></asp:TextBox>
function SetControlVale(a) //a是服务器端返回的值
{
document.getElementById("tbNow").value = a; //设置tb的值为处理后的结果
}
</script>
...
<asp:TextBox ID="tbNow" runat="server"></asp:TextBox>
aspx.cs:
代码
private string callbackResult = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsCallback)
{
string callbackMethod = ClientScript.GetCallbackEventReference(this, "document.getElementById('tbNow').value", "SetControlVale", null);
tbNow.Attributes.Add("OnClick", callbackMethod);
// OnClick="WebForm_DoCallback('__Page',document.getElementById('tbNow').value,SetControlVale,null,null,false)" //最终的结果
// WebForm_DoCallback(eventTarget, eventArgument, eventCallback, context, errorCallback, useAsync) //函数原型
}
}
public string GetCallbackResult()
{
return callbackResult + "OK";
}
public void RaiseCallbackEvent(string eventArgument)
{
callbackResult = eventArgument + System.DateTime.Now.Millisecond;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsCallback)
{
string callbackMethod = ClientScript.GetCallbackEventReference(this, "document.getElementById('tbNow').value", "SetControlVale", null);
tbNow.Attributes.Add("OnClick", callbackMethod);
// OnClick="WebForm_DoCallback('__Page',document.getElementById('tbNow').value,SetControlVale,null,null,false)" //最终的结果
// WebForm_DoCallback(eventTarget, eventArgument, eventCallback, context, errorCallback, useAsync) //函数原型
}
}
public string GetCallbackResult()
{
return callbackResult + "OK";
}
public void RaiseCallbackEvent(string eventArgument)
{
callbackResult = eventArgument + System.DateTime.Now.Millisecond;
}
4。实际例子:重构客户端显示
aspx:
代码
<script type="text/javascript">
function getRblValue() //取得选择项作为参数传给服务器端
{
var result="";
var obj = document.getElementById("rblNewsType"); //得到一个Table
var radio= obj.getElementsByTagName("input");
for(var i=0;i<radio.length;i++)
{
if(radio[i].checked)
{
result=radio[i].value;
}
}
return result;
}
function FourStep(arg)
{
document.getElementById('lbTest').innerHTML=arg; //服务器端返回的数据
}
</script>
....
<%= DateTime.Now %>
<asp:RadioButtonList ID="rblNewsType" runat="server">
<asp:ListItem Text="选项1" Value="选项1Value"></asp:ListItem>
<asp:ListItem Text="选项2" Value="选项2Value"></asp:ListItem>
</asp:RadioButtonList>
<asp:Label ID="lbTest" runat="server">原来的字样</asp:Label>
<br />
<asp:Button ID="btnRefreshTiem" runat="server" Text="PostBack" />
function getRblValue() //取得选择项作为参数传给服务器端
{
var result="";
var obj = document.getElementById("rblNewsType"); //得到一个Table
var radio= obj.getElementsByTagName("input");
for(var i=0;i<radio.length;i++)
{
if(radio[i].checked)
{
result=radio[i].value;
}
}
return result;
}
function FourStep(arg)
{
document.getElementById('lbTest').innerHTML=arg; //服务器端返回的数据
}
</script>
....
<%= DateTime.Now %>
<asp:RadioButtonList ID="rblNewsType" runat="server">
<asp:ListItem Text="选项1" Value="选项1Value"></asp:ListItem>
<asp:ListItem Text="选项2" Value="选项2Value"></asp:ListItem>
</asp:RadioButtonList>
<asp:Label ID="lbTest" runat="server">原来的字样</asp:Label>
<br />
<asp:Button ID="btnRefreshTiem" runat="server" Text="PostBack" />
aspx.cs:
代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsCallback)
{
rblNewsType.Attributes.Add("OnClick", ClientScript.GetCallbackEventReference(this, "getRblValue()", "FourStep", null));
//因为是CallBack,参数值无法用rblNewsType.SelectedValue取得
}
}
public string GetCallbackResult()
{
return "被修改成:" + RenderControl(lbTest);
}
public void RaiseCallbackEvent(string eventArgument) //接收step1的参数
{
lbTest.Text = eventArgument;
}
private string RenderControl(Control control) //重画控件
{
StringWriter writer1 = new StringWriter(CultureInfo.InvariantCulture);
HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
control.RenderControl(writer2);
writer2.Flush();
writer2.Close();
return writer1.ToString();
}
{
if (!IsCallback)
{
rblNewsType.Attributes.Add("OnClick", ClientScript.GetCallbackEventReference(this, "getRblValue()", "FourStep", null));
//因为是CallBack,参数值无法用rblNewsType.SelectedValue取得
}
}
public string GetCallbackResult()
{
return "被修改成:" + RenderControl(lbTest);
}
public void RaiseCallbackEvent(string eventArgument) //接收step1的参数
{
lbTest.Text = eventArgument;
}
private string RenderControl(Control control) //重画控件
{
StringWriter writer1 = new StringWriter(CultureInfo.InvariantCulture);
HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
control.RenderControl(writer2);
writer2.Flush();
writer2.Close();
return writer1.ToString();
}
注:验证用户名是否存在的例子 http://www.cnblogs.com/vipcjob/archive/2009/07/29/1534064.html Part6,做法类似(增加了错误处理)。