之前的ValidateBox控件单击更换图片时,整个页面都要刷新,所以想使用Ajax来试试可不可以改进。经过一试,Ajax还真管用!
AjaxComm.js代码:
// ********************************************************
// AjaxComm 1.1 for Asp.Net
// Designed by Faib Studio.
// Copyright 2007
// Email faib920@126.com or QQ 55570729
// ********************************************************
function AjaxComm()


{
this.xmlHttp = null;
var clsids = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0","Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];

for(var i=0; i<clsids.length && this.xmlHttp == null; i++)

{

try
{
this.xmlHttp = new ActiveXObject(clsids[i]);

} catch(ex)
{}
}
}

AjaxComm.prototype.open = function (metohod, url)


{
if(this.xmlHttp)

{
if(this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0 )

{
var oThis = this;
this.xmlHttp.open(metohod, url);

this.xmlHttp.onreadystatechange = function()
{ oThis.readyStateChange(); };
this.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.xmlHttp.send(null);
}
}
}

AjaxComm.prototype.abortCallBack = function()


{
if(this.xmlHttp)this.xmlHttp.abort();
}
AjaxComm.prototype.onLoading = function()


{
}
AjaxComm.prototype.onLoaded = function()


{
}
AjaxComm.prototype.onInteractive = function()


{
}
AjaxComm.prototype.onComplete = function(responseText, responseXml)


{
}
AjaxComm.prototype.onAbort = function()


{
}
AjaxComm.prototype.onError = function(status, statusText)


{
}

AjaxComm.prototype.readyStateChange = function ()


{
if( this.xmlHttp.readyState == 1 )

{
this.onLoading();
}
else if( this.xmlHttp.readyState == 2 )

{
this.onLoaded();
}
else if( this.xmlHttp.readyState == 3 )

{
this.onInteractive();
}
else if( this.xmlHttp.readyState == 4 )

{
if( this.xmlHttp.status == 0 )
this.onAbort();
else if( this.xmlHttp.status == 200 && this.xmlHttp.statusText == "OK" )
this.onComplete(this.xmlHttp.responseText, this.xmlHttp.responseXML);
else
this.onError(this.xmlHttp.status, this.xmlHttp.statusText, this.xmlHttp.responseText);
}
}
ValidateBox的改进:
private bool m_xx = false;

protected override void OnInit(EventArgs e)

{
//注册一个js文件到缓存中
Util.RegisterCacheFile(Page, "AjaxComm", "ajaxcomm.fbs.ashx", "Resource.AjaxComm.js");//对应到AjaxComm.js
//输出控件初始化脚本
Util.RegisterStartupScript(Page, "ValidateBox", @"
<script language=""javascript"" type=""text/javascript"">
function ValidateBox_Change(url)
{
var ajax = new AjaxComm();
ajax.onComplete = function (rText)
{
var v = rText.split(';');
var val = event.srcElement;
var src = val.src;
src = src.substr(0, src.indexOf('?'));
val.src = src + '?' + v[0];
if(v.length > 1)
{
val.text = v[1];
val.value = v[1].toLowerCase();
}
}
ajax.open(""GET"", url);
}
</script>
");
base.OnInit (e);
}

protected override void OnUnload(EventArgs e)

{
if(m_ValidateKey != null && m_ValidateKey != "" && !m_xx)

{
if(ViewState["Text"] != null)

{
switch(m_ReturnStyle)

{
case ValidateStyle.Session:
Context.Session[m_ValidateKey] = ViewState["Text"].ToString();
break;
case ValidateStyle.Cookie:
Context.Response.Cookies[m_ValidateKey].Value = ViewState["Text"].ToString();
break;
case ValidateStyle.Cache:
Context.Cache[m_ValidateKey] = ViewState["Text"].ToString();
break;
}
}
}
base.OnUnload (e);
}

protected override void OnLoad(EventArgs e)

{
string strCode = GetValidateCode();
ViewState["Text"] = strCode;
string cacheid = BuildImage();
if(Page.Request.QueryString["GetValidateNewCode"] == this.ClientID)

{
Page.Response.Clear();
if(m_ReturnStyle == ValidateStyle.Client)

{
Page.Response.Write(cacheid + ";" + strCode);
}
else

{
Page.Response.Write(cacheid);
}
Page.Response.End();
}
else if(Page.Request.QueryString["GetValidateNewCode"] != null)

{
m_xx = true;
}

if(m_ReturnStyle == ValidateStyle.Client)

{
this.Attributes.Add("Text", this.Text);
this.Attributes.Add("value", this.Text.ToLower());
}
this.Style.Add("cursor", "hand");
this.Attributes.Add("onclick", "ValidateBox_Change('" + Page.Request.FilePath + "?GetValidateNewCode=" + this.ClientID + "');");
base.OnLoad(e);
}