之前的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()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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"];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for(var i=0; i<clsids.length && this.xmlHttp == null; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
this.xmlHttp = new ActiveXObject(clsids[i]);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch(ex)
{}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AjaxComm.prototype.open = function (metohod, url)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(this.xmlHttp)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var oThis = this;
this.xmlHttp.open(metohod, url);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.xmlHttp.onreadystatechange = function()
{ oThis.readyStateChange(); };
this.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.xmlHttp.send(null);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AjaxComm.prototype.abortCallBack = function()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(this.xmlHttp)this.xmlHttp.abort();
}
AjaxComm.prototype.onLoading = function()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
AjaxComm.prototype.onLoaded = function()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
AjaxComm.prototype.onInteractive = function()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
AjaxComm.prototype.onComplete = function(responseText, responseXml)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
AjaxComm.prototype.onAbort = function()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
AjaxComm.prototype.onError = function(status, statusText)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AjaxComm.prototype.readyStateChange = function ()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if( this.xmlHttp.readyState == 1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.onLoading();
}
else if( this.xmlHttp.readyState == 2 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.onLoaded();
}
else if( this.xmlHttp.readyState == 3 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.onInteractive();
}
else if( this.xmlHttp.readyState == 4 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected override void OnInit(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//注册一个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);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected override void OnUnload(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(m_ValidateKey != null && m_ValidateKey != "" && !m_xx)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(ViewState["Text"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch(m_ReturnStyle)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected override void OnLoad(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string strCode = GetValidateCode();
ViewState["Text"] = strCode;
string cacheid = BuildImage();
if(Page.Request.QueryString["GetValidateNewCode"] == this.ClientID)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.Response.Clear();
if(m_ReturnStyle == ValidateStyle.Client)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.Response.Write(cacheid + ";" + strCode);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.Response.Write(cacheid);
}
Page.Response.End();
}
else if(Page.Request.QueryString["GetValidateNewCode"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_xx = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(m_ReturnStyle == ValidateStyle.Client)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}