posts - 50, comments - 140, trackbacks - 8, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

ValidateBox控件使用Ajax改进

Posted on 2007-05-15 09:21 faib 阅读(558) 评论(1)  编辑 收藏 网摘 所属分类: C#HTML / JavascriptAjaxAsp.Net

  之前的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 ()
{
  
ifthis.xmlHttp.readyState == 1 )
  
{
    
this.onLoading();
  }

  
else ifthis.xmlHttp.readyState == 2 )
  
{
   
this.onLoaded();
  }

  
else ifthis.xmlHttp.readyState == 3 )
  
{
   
this.onInteractive();
  }

  
else ifthis.xmlHttp.readyState == 4 )
  
{
   
ifthis.xmlHttp.status == 0 )
      
this.onAbort();
    
else ifthis.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);
        }

Feedback

#1楼    回复  引用    

2007-05-17 09:30 by faib [未注册用户]
忘了BuildImage了
private string BuildImage()
{
bool blnHasPic;
if(ViewState["OldImage"] != null)
{
blnHasPic = System.IO.File.Exists(Page.Server.MapPath(ViewState["OldImage"].ToString()));
}
else
{
blnHasPic = false;
}

m_newImage = this.ClientID + ".fbs.ashx";

if(!blnHasPic && this.Width.IsEmpty)
{
this.Width = Unit.Pixel(70);
}
if(!blnHasPic && this.Height.IsEmpty)
{
this.Height = Unit.Pixel(20);
}

string strCode = ViewState["Text"].ToString();

string strFName = "System";
float floFSize = 9;
FontStyle fntstyFont = FontStyle.Regular;
if(m_Font != null)
{
if(m_Font.Name != string.Empty)
{
strFName = m_Font.Name;
}
//如果使用是的是FontSize.Large等相对值应该怎么做
if(m_Font.Size.Type != FontSize.NotSet)
{
floFSize = (float)m_Font.Size.Unit.Value;
}
else
{
floFSize = 9;
}
if(m_Font.Bold)
{
fntstyFont |= FontStyle.Bold;
}
if(m_Font.Italic)
{
fntstyFont |= FontStyle.Italic;
}
if(m_Font.Strikeout)
{
fntstyFont |= FontStyle.Strikeout;
}
if(m_Font.Underline)
{
fntstyFont |= FontStyle.Underline;
}
}

Font fntOut = new Font(strFName, floFSize, fntstyFont);

#region 生成图片
MemoryStream ms = null;
Bitmap objBitmap;
if(ViewState["OldImage"] != null)
{
objBitmap = (Bitmap)Bitmap.FromFile(Page.Server.MapPath(ViewState["OldImage"].ToString()));
}
else
{
objBitmap = new Bitmap(int.Parse(Width.Value.ToString()), int.Parse(Height.Value.ToString()));
}
Graphics objGraphics = Graphics.FromImage(objBitmap);

if(ViewState["OldImage"] == null)
{
objGraphics.FillRectangle(new SolidBrush(BackColor == Color.Empty ? Color.White : BackColor), 0, 0, objBitmap.Width, objBitmap.Height);
}

SizeF wh = objGraphics.MeasureString(strCode, fntOut);
int l = (objBitmap.Width - (int)wh.Width) / 2;
int t = (objBitmap.Height - (int)wh.Height) / 2;

for(int i = 0; i < strCode.Length; i++)
{
if(i == 0)
{
l -= (int)(objGraphics.MeasureString(strCode.Substring(i, 1), fntOut).Width / 3);
}
Matrix myMatrix = new Matrix();
myMatrix.RotateAt(i % 2 == 0 ? TextAngle : 0 - TextAngle, new PointF(l + wh.Width / 2, wh.Height / 2), MatrixOrder.Append);
objGraphics.Transform = myMatrix;

objGraphics.DrawString(strCode.Substring(i, 1), fntOut, new SolidBrush(ForeColor == Color.Empty ? Color.Black : ForeColor), l, t);
l += (int)objGraphics.MeasureString(strCode.Substring(i, 1), fntOut).Width;
}

ms = new MemoryStream();
objBitmap.Save(ms, ImageFormat.Jpeg);

AshxHandler.RegisterCacheFile(this.Page, m_newImage, ms.ToArray());

objGraphics.Dispose();
objBitmap.Dispose();
#endregion

m_IsLaod = true;
string cacheid = "." + DateTime.Now.ToString("mmfffMMyyddssHH");
this.ImageUrl = m_newImage + "?" + cacheid;
m_IsLaod = false;
return cacheid;
}

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接: