ICallbackEventHandler与Microsoft.XMLHTTP
关于异步回调,最近做了一个简单的便签WebPart。用到了一些回调的东西,索性把这两个异步的知识点都用了一下。顺便巩固一下自己。
ICallbackEventHandler接口包括两个方法:
String GetCallBackResult():该方法的返回值将返回给前台等待接受信息的js代码;
Void RaiseCallBackEvent(string eventArgument):从前台的js方法接受参数:eventArgument,并进行处理,在该方法中可以对一个全局变量赋值,并且在GetCallBackResult()方法中将该值返回给前台js代码以进行结果的展示。
需要注意的一点是在页面或者底层空间Load的时候注入一段脚本。
这段代码注入了一个CallServer的js方法,可以让在.aspx/.ascx页面中的js进行调用。
皮肤文件:
ICallbackEventHandler接口包括两个方法:
String GetCallBackResult():该方法的返回值将返回给前台等待接受信息的js代码;
Void RaiseCallBackEvent(string eventArgument):从前台的js方法接受参数:eventArgument,并进行处理,在该方法中可以对一个全局变量赋值,并且在GetCallBackResult()方法中将该值返回给前台js代码以进行结果的展示。
需要注意的一点是在页面或者底层空间Load的时候注入一段脚本。
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
string callbackScript;
callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
string callbackScript;
callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
这段代码注入了一个CallServer的js方法,可以让在.aspx/.ascx页面中的js进行调用。
public class NotePadWebPart:TemplatedWebPart,ICallbackEventHandler
{
//CallBack产生的结果
private string g_Result;
public string G_Result
{
get { return g_Result; }
set { g_Result = value; }
}
private NotePadService _NotePadServices;
private NotePadData _NotePadData;
private Literal _ltlTitle;
private TextBox txtNote;
public NotePadWebPart()
{
this.ExportMode = WebPartExportMode.All;
_NotePadServices = new NotePadService();
_NotePadData = new NotePadData();
}
WebPart CustomerAttributes
protected override String ExternalSkinFileName
{
get
{
if (!string.IsNullOrEmpty(skinName))
return CreateExternalSkinFileName("MydeskTop", skinName);
return CreateExternalSkinFileName("MydeskTop");
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnsureChildControls();
if (!Page.IsPostBack)
{
DataBind();
string strCmd = Page.Request.QueryString["cmd"];
if (!string.IsNullOrEmpty(strCmd))
this.Clear();
}
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
string callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
private void Clear()
{
NotePadData data = new NotePadData();
data.UserName = GetCurrentUser();
data.Content1 = "";
data.ModifyTime = DateTime.Now;
if (_NotePadServices.Update(data))
{
Page.Response.Write("");
Page.Response.End();
}
}
public override void DataBind()
{
base.DataBind();
if (_listShowHead)
{
_ltlTitle.Text = HtmlUtil.GetTitleImage(ListImageUrl, ListImageWidth, ListImageHeight,
0, false, "", "");
}
_NotePadData = _NotePadServices.FindData(GetCurrentUser());
if (_NotePadData != null)
{
txtNote.Text = _NotePadData.Content1;
}
}
protected override void AttachChildControls()
{
_ltlTitle = FindControl("ltlTitle") as Literal;
txtNote = FindControl("txtNote") as TextBox;
txtNote.Attributes.Add("onblur", "TextOnBlur('" + txtNote.ClientID + "')");
}
private string GetCurrentUser()
{
string username = Page.User.Identity.Name;
string[] str = username.Split('\\');
if (str.Length > 1)
{
return str[1];
}
return str[0];
}
ICallbackEventHandler 成员
}
{
//CallBack产生的结果
private string g_Result;
public string G_Result
{
get { return g_Result; }
set { g_Result = value; }
}
private NotePadService _NotePadServices;
private NotePadData _NotePadData;
private Literal _ltlTitle;
private TextBox txtNote;
public NotePadWebPart()
{
this.ExportMode = WebPartExportMode.All;
_NotePadServices = new NotePadService();
_NotePadData = new NotePadData();
}
WebPart CustomerAttributes
protected override String ExternalSkinFileName
{
get
{
if (!string.IsNullOrEmpty(skinName))
return CreateExternalSkinFileName("MydeskTop", skinName);
return CreateExternalSkinFileName("MydeskTop");
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnsureChildControls();
if (!Page.IsPostBack)
{
DataBind();
string strCmd = Page.Request.QueryString["cmd"];
if (!string.IsNullOrEmpty(strCmd))
this.Clear();
}
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
string callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
private void Clear()
{
NotePadData data = new NotePadData();
data.UserName = GetCurrentUser();
data.Content1 = "";
data.ModifyTime = DateTime.Now;
if (_NotePadServices.Update(data))
{
Page.Response.Write("");
Page.Response.End();
}
}
public override void DataBind()
{
base.DataBind();
if (_listShowHead)
{
_ltlTitle.Text = HtmlUtil.GetTitleImage(ListImageUrl, ListImageWidth, ListImageHeight,
0, false, "", "");
}
_NotePadData = _NotePadServices.FindData(GetCurrentUser());
if (_NotePadData != null)
{
txtNote.Text = _NotePadData.Content1;
}
}
protected override void AttachChildControls()
{
_ltlTitle = FindControl("ltlTitle") as Literal;
txtNote = FindControl("txtNote") as TextBox;
txtNote.Attributes.Add("onblur", "TextOnBlur('" + txtNote.ClientID + "')");
}
private string GetCurrentUser()
{
string username = Page.User.Identity.Name;
string[] str = username.Split('\\');
if (str.Length > 1)
{
return str[1];
}
return str[0];
}
ICallbackEventHandler 成员
}
皮肤文件:
<%@ Control Language="C#" %>
<script type="text/javascript">
function TextOnBlur(txtId)
{
var txtContent = document.getElementById(txtId).value;
var hid = document.getElementById('txtLast');
if(txtContent == hid.value)
{
return;
}
hid.value = txtContent;
//回调服务器,即调用ICallBackEventHandler的RaiseCallBackEvent方法
CallServer(txtContent, "");
}
//rValue的值来自于GetCallBackResult返回的值
function ReceiveServerData(rValue)
{
var txt = document.getElementById('<%=txtNote.ClientID %>');
txt.value=rValue;
}
function txtClear()
{
var txt = document.getElementById('<%=txtNote.ClientID %>');
txt.value="";
var url = "?cmd=cmd";
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("POST", url, false);
xmlHttp.send("");
result = xmlHttp.responseText;
}
</script>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="4"> <asp:Literal ID="ltlTitle" runat="server"></asp:Literal>
</td>
</tr>
<tr>
<td width="89%" height="35"><span style="color:#003366;font-size:13px;"><strong>我的便签</strong></span></td>
<td width="11%">
<input id="txtLast" type="hidden" />
<a href="#" onclick="txtClear()" ><span style="color:#FF0000">[清空]</span></a></td>
</tr>
<tr>
<td colspan="2" style="line-height:180%"><asp:TextBox ID="txtNote" runat="server" Height="120px" TextMode="MultiLine" Width="98%" style="background:#fff;color:#333;border:1px #E7E7E7 solid;"></asp:TextBox></td>
</tr>
</table>
<script type="text/javascript">
function TextOnBlur(txtId)
{
var txtContent = document.getElementById(txtId).value;
var hid = document.getElementById('txtLast');
if(txtContent == hid.value)
{
return;
}
hid.value = txtContent;
//回调服务器,即调用ICallBackEventHandler的RaiseCallBackEvent方法
CallServer(txtContent, "");
}
//rValue的值来自于GetCallBackResult返回的值
function ReceiveServerData(rValue)
{
var txt = document.getElementById('<%=txtNote.ClientID %>');
txt.value=rValue;
}
function txtClear()
{
var txt = document.getElementById('<%=txtNote.ClientID %>');
txt.value="";
var url = "?cmd=cmd";
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("POST", url, false);
xmlHttp.send("");
result = xmlHttp.responseText;
}
</script>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="4"> <asp:Literal ID="ltlTitle" runat="server"></asp:Literal>
</td>
</tr>
<tr>
<td width="89%" height="35"><span style="color:#003366;font-size:13px;"><strong>我的便签</strong></span></td>
<td width="11%">
<input id="txtLast" type="hidden" />
<a href="#" onclick="txtClear()" ><span style="color:#FF0000">[清空]</span></a></td>
</tr>
<tr>
<td colspan="2" style="line-height:180%"><asp:TextBox ID="txtNote" runat="server" Height="120px" TextMode="MultiLine" Width="98%" style="background:#fff;color:#333;border:1px #E7E7E7 solid;"></asp:TextBox></td>
</tr>
</table>