2007年10月22日

记录错误日志

using System.Diagnostics;

  protected 
void Application_Error(Object sender, EventArgs e)
  {
   string errMsg 
= "<link rel=\"stylesheet\" href=\"/ThePhile/Styles/ThePhile.CSS\">";
   errMsg 
+= "<h1>Application Error</h1><hr/>An unexpected error has occurred in this Application. The system " +
    
"administrators have been notified. Please feel free to contact us with the information " +
    
"surrounding this error.<br/>"+
    
"The error occurred in: "+Request.Url.ToString()+"<br/>"+
    
"Error Message: <font class=\"ErrorMessage\">"+Server.GetLastError().Message.ToString()+"</font><hr/>"+
    
"<b>Stack Trace:</b><br/>"+
    Server.GetLastError().ToString();

   Response.Write( errMsg );

   
if (!EventLog.SourceExists("ThePhile.COM"))
   {
    EventLog.CreateEventSource(
"ThePhile.COM""Application");
   }
   EventLog.WriteEntry(
"ThePhile.COM", Server.GetLastError().Message, EventLogEntryType.Error);

   Server.ClearError();
  }
web.config

<customErrors mode="RemoteOnly" defaultRedirect="error.aspx"/>

posted @ 2007-10-22 14:26 jueban's space 阅读(52) 评论(0) 编辑

javascript一个超COOL的模拟虚拟键盘--各大银行有用

posted @ 2007-10-22 14:24 jueban's space 阅读(783) 评论(0) 编辑

通用的加入收藏夹代码支持IE Firefox

<script>
function addfavorite()
{
   
switch(getOs())
   {
       
case 1:window.external.addFavorite('http://www.sosuo8.com','搜索吧');break;
       case 2:window.sidebar.addPanel('搜索吧', 'http://www.sosuo8.com/', "");break;
       case 0:alert("您的浏览器也太牛了,我都不知道如何显示了。唉。");break;
   }
}
function getOs()
{
   
if(navigator.userAgent.indexOf("MSIE")>0)return 1;
   
if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)return 2;
   
if(isSafari=navigator.userAgent.indexOf("Safari")>0)return 3;   
   
if(isCamino=navigator.userAgent.indexOf("Camino")>0)return 4;
   
if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0)return 5;
   
return 0;
}
</script>
<a href="Javascript:void(0)" onclick="addfavorite()">把Web之路加入收藏</a> 

posted @ 2007-10-22 14:21 jueban's space 阅读(156) 评论(0) 编辑

QQ空间的图片切换效果(图片左边点击向上,图片右边点击向下)

http://files.cnblogs.com/jueban/QQ(1).rar
QQ空间的图片切换效果(图片左边点击向上,图片右边点击向下)
自己看看!

posted @ 2007-10-22 14:14 jueban's space 阅读(288) 评论(1) 编辑

asp.net(c#)上传文件时检测文件类型方法小结

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {

    }
    
protected void btn_upload_Click(object sender, EventArgs e)
    {
        
try
        {
            
//判断是否已经选取文件
            if (FileUpload1.HasFile)
            {
                
if (IsAllowedExtension(FileUpload1))
                {
                    
string path = Server.MapPath("~/images/");
                    FileUpload1.PostedFile.SaveAs(path 
+ FileUpload1.FileName);
                    Response.Write(
"<script>alert('上传成功');</script>");
                }
                
else
                {
                    Response.Write(
"<script>alert('您只能上传jpg或者gif图片');</script>");
                }

            }
            
else
            {
                Response.Write(
"<script>alert('你还没有选择文件');</script>");
            }
        }
        
catch (Exception error)
        {
            Response.Write(error.ToString());
        }
    }
    
//真正判断文件类型的关键函数
    public static bool IsAllowedExtension(FileUpload hifile)
    {
        System.IO.FileStream fs 
= new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.BinaryReader r 
= new System.IO.BinaryReader(fs);
        
string fileclass = "";
        
byte buffer;
        
try
        {
            buffer 
= r.ReadByte();
            fileclass 
= buffer.ToString();
            buffer 
= r.ReadByte();
            fileclass 
+= buffer.ToString();

        }
        
catch
        {

        }
        r.Close();
        fs.Close();
        
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
        {
            
return true;
        }
        
else
        {
            
return false;
        }

    }
}

posted @ 2007-10-22 14:05 jueban's space 阅读(1434) 评论(7) 编辑

ASP.net 实现在线统计人数

 

利用Application对象和Session对象可以统计当前在线用户数量.

注意:

(1)在会话开始和结束时,一定要进行加锁和解锁操作。由于多个用户可以共享Application对象,因此加锁是必要的,这样可以保证在同一时刻只有一个客户可以修改和存取Application对象的属性。如果加锁后,迟迟不给开锁,会导致用户无法访问Application对象。我们可以使用对象的Unlock方法来解除锁定。

(2)我们是根据用户建立和退出会话来实现在线人数的增加、减少的,如果用户没有关闭浏览器,而直接进入其他URL,则这个会话在一定时间内是不会结束的,所以对在线用户的统计存在一定的偏差。当然我们可以在Web.config文件中对会话Session的失效时间Timeout来设置,默认值为20分钟,最小值为1分钟。

(3)只有在Web.config文件中的sessionstate模式设置为InProc时,才会引发Session_End事件。如果会话模式为StateServer或SQLServer,则不会引发该事件。

实现代码:

我们在网站中添加一个Global.asax全局应用程序文件.

Global.asax

<%@ Application Language="C#" %>

<script runat="server">

    
void Application_Start(object sender, EventArgs e) 
    {
        
//应用程序启动时运行的代码
        Application["count"= 0;
    }
    
    
void Application_End(object sender, EventArgs e) 
    {
        
//  在应用程序关闭时运行的代码
    }
        
    
void Application_Error(object sender, EventArgs e) 
    { 
        
// 在出现未处理的错误时运行的代码
    }

    
void Session_Start(object sender, EventArgs e) 
    {
        
//对Appliaction加锁以防止并行性
        Application.Lock();
        
        
//增加一个在线人数
        Application["count"= (int)Application["count"+ 1;
        
        
//解锁
        Application.UnLock();

    }

    
void Session_End(object sender, EventArgs e) 
    {
        Application.Lock();
        
        
//减少一个在线人数
        Application["count"= (int)Application["count"- 1;
        
        Application.UnLock();
    }
       
</script>



default.aspx

<%@ Page Language="C#"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<%Response.Write(Application["count"]); %>
    
</div>
    
</form>
</body>
</html>

posted @ 2007-10-22 14:03 jueban's space 阅读(121) 评论(0) 编辑

asp.net 生成html静态页

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.IO;

/// <summary>
/// DB 的摘要说明
/// </summary>
public class DB
{
 
public DB()
 {
  
//
  
// TODO: 在此处添加构造函数逻辑
  
//
 }

    
public static bool WriteFile(string news_title,string news_content,string news_id)
    {   

        
//申明字符编码,命名空间System.Text
        Encoding encoding = Encoding.GetEncoding("gb2312");

        
//读取模板文件
        string TemplatePath = HttpContext.Current.Server.MapPath("template/1.htm");

        
//StreamReader实现一个 TextReader,使其以一种特定的编码从字节流中读取字符,命名空间System.IO。
        StreamReader sr = null;

        
//StreamWriter实现一个 TextWriter,使其以一种特定的编码向流中写入字符,命名空间Syste.IO。
        StreamWriter sw = null;

        
string str = "";

        
try
        {
            sr 
= new StreamReader(TemplatePath, encoding);

            
//从流的当前位置到末尾读取流
            str = sr.ReadToEnd();

        }
        
catch (Exception e1)
        {
            HttpContext.Current.Response.Write(e1.Message);
            HttpContext.Current.Response.End();
        }
        
finally
        {  
            sr.Close();
            sr.Dispose();
        }

        
//生成路径
        string OutPutPath = HttpContext.Current.Server.MapPath("html/");

        
//生成的文件名
        string newName = DateTime.Now.ToString("yyyyMM_"+ news_id + ".html";

        
//替换内容
        str = str.Replace("{title}", news_title);
        str 
= str.Replace("{content}", news_content);

        
//写文件
        try
        {
            sw 
= new StreamWriter(OutPutPath + newName,false, encoding);
            sw.Write(str);

            
//清理当前编写器的所有缓存区,并将缓存数据写入基础流。
            sw.Flush();
        }
        
catch (Exception e2)
        {
            HttpContext.Current.Response.Write(e2.Message);
            HttpContext.Current.Response.End();
        }
        
finally
        {
            sw.Close();
            sw.Dispose();
        }

        
return true;

    }
}

posted @ 2007-10-22 14:00 jueban's space 阅读(81) 评论(0) 编辑

asp.net XML操作类

 

public class XmlControl 

protected string strXmlFile; 
protected XmlDocument objXmlDoc = new XmlDocument(); 

public XmlControl(string XmlFile) 

// 
// TODO: 在这里加入建构函式的程序代码 
// 
try 

objXmlDoc.Load(XmlFile); 

catch (System.Exception ex) 

throw ex; 

strXmlFile 
= XmlFile; 


public DataView GetData(string XmlPathNode) 

//查找数据。返回一个DataView 
DataSet ds = new DataSet(); 
StringReader read 
= new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml); 
ds.ReadXml(read); 
return ds.Tables[0].DefaultView; 


public void Replace(string XmlPathNode,string Content) 

//更新节点内容。 
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content; 


public void Delete(string Node) 

//删除一个节点。 
string mainNode = Node.Substring(0,Node.LastIndexOf("/")); 
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node)); 


public void InsertNode(string MainNode,string ChildNode,string Element,string Content) 

//插入一节点和此节点的一子节点。 
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode); 
XmlElement objChildNode 
= objXmlDoc.CreateElement(ChildNode); 
objRootNode.AppendChild(objChildNode); 
XmlElement objElement 
= objXmlDoc.CreateElement(Element); 
objElement.InnerText 
= Content; 
objChildNode.AppendChild(objElement); 


public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content) 

//插入一个节点,带一属性。 
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); 
XmlElement objElement 
= objXmlDoc.CreateElement(Element); 
objElement.SetAttribute(Attrib,AttribContent); 
objElement.InnerText 
= Content; 
objNode.AppendChild(objElement); 


public void InsertElement(string MainNode,string Element,string Content) 

//插入一个节点,不带属性。 
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); 
XmlElement objElement 
= objXmlDoc.CreateElement(Element); 
objElement.InnerText 
= Content; 
objNode.AppendChild(objElement); 


public void Save() 

//保存文檔。 
try 

objXmlDoc.Save(strXmlFile); 

catch (System.Exception ex) 

throw ex; 

objXmlDoc 
= null



========================================================= 

实例应用: 

string strXmlFile = Server.MapPath("TestXml.xml"); 
XmlControl xmlTool 
= new XmlControl(strXmlFile); 

// 数据显视 
// dgList.DataSource = xmlTool.GetData("Book/Authors[ISBN=\"0002\"]"); 
// dgList.DataBind(); 

// 更新元素内容 
// xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp"); 
// xmlTool.Save(); 

// 添加一个新节点 
// xmlTool.InsertNode("Book","Author","ISBN","0004"); 
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa"); 
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii"); 
// xmlTool.Save(); 

// 删除一个指定节点的所有内容和属性 
// xmlTool.Delete("Book/Author[ISBN=\"0004\"]"); 
// xmlTool.Save(); 

// 删除一个指定节点的子节点 
// xmlTool.Delete("Book/Authors[ISBN=\"0003\"]"); 
// xmlTool.Save();

 

posted @ 2007-10-22 13:56 jueban's space 阅读(286) 评论(0) 编辑

ASp.NET 2.0中Page事件的执行顺序

 
Page 执行中将按照如下顺序激活事件:

Page.PreInit 
Page.Init 
Page.InitComplite 
Page.PreLoad 
Page.Load 
Page.LoadComplete 
Page.PreRender 
Page.PreRenderComplete 

如果页面从令一个页面继承,如BasePage:System.Web.UI.Page,在BasePage中做了一些扩展,如权限检查,而其他页面从BasePage继承,则BasePage和最终Page的事件激活顺序是:

UI.PreInit 
Page.PreInit 
UI.Init 
Page.Init 
UI.InitComplite 
Page.InitComplite 
UI.PreLoad 
Page.PreLoad 
UI.Load 
Page.Load 
UI.LoadComplete 
Page.LoadComplete 
UI.PreRender 
Page.PreRender 
UI.PreRenderComplete 
Page.PreRenderComplete 

如果使用了MasterPage,则MasterPage中的事件和ContentPage中的事件按照下面顺序激活:

ContentPage.PreInit 
Master.Init 
ContentPage.Init 
ContentPage.InitComplite 
ContentPage.PreLoad 
ContentPage.Load 
Master.Load 
ContentPage.LoadComplete 
ContentPage.PreRender 
Master.PreRender 
ContentPage.PreRenderComplete 

更进一步,如果ContentPage继承BasePage,那么,各事件的执行顺序将变成:

UI.PreInit 
ContentPage.PreInit 
Master.Init 
UI.Init 
ContentPage.Init 
UI.InitComplite 
ContentPage.InitComplite 
UI.PreLoad 
ContentPage.PreLoad 
UI.Load 
ContentPage.Load 
Master.Load 
UI.LoadComplete 
ContentPage.LoadComplete 
UI.PreRender 
ContentPage.PreRender 
Master.PreRender 
UI.PreRenderComplete 
ContentPage.PreRenderComplete 

posted @ 2007-10-22 13:54 jueban's space 阅读(56) 评论(0) 编辑

防止一个用户重复登陆

string key = TextBox1.Text; //用户名文本框设为cache关键字  
string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值 
//判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆 
if (uer == null || uer == String.Empty) 

 
//定义cache过期时间 
 TimeSpan SessTimeout = new TimeSpan(00, System.Web.HttpContext.Current.Session.Timeout, 00); 
 
//第一次登陆的时候插入一个用户相关的cache值, 
 HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null); 
 Session[
"ADMINID"= TextBox1.Text; 
 Response.Redirect(
"main.ASPx"); 

else 

 
//重复登陆 
 Response.Write(""); 

posted @ 2007-10-22 13:52 jueban's space 阅读(80) 评论(0) 编辑