Jerry's profileCoCoMan TechnologyPhotosBlogListsMore Tools Help

Weather

Loading...
Photo 1 of 176

Jerry Huang

Occupation
Location
Interests
Kiss me before eating me...
Thanks for visiting! Please leave me a message if you have any comment.
Please wait...
Sorry, the comment you entered is too long. Please shorten it.
You didn't enter anything. Please try again.
Sorry, we can't add your comment right now. Please try again later.
To add a comment, you need permission from your parent. Ask for permission
Your parent has turned off comments.
Sorry, we can't delete your comment right now. Please try again later.
You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
Complete the security check below to finish leaving your comment.
The characters you type in the security check must match the characters in the picture or audio.
Yingwrote:
a blue sky~~
June 22

CoCoMan Technology

smart handsome...
November 04

转:某bbs上的经典回复

1.论坛楼主:我和女朋友的照片,朋友轻砸~  
          论坛回复:植物的性器官插在长角偶蹄类动物的排泄物上……  
2.论坛楼主:我新买了一处庄园,有多大说出来吓死你——我开车绕一圈足足用了两个半小时!!!  
          论坛沙发:嗯,以前我也有这么一辆破车~     
3.论坛楼主:你们女人大夏天的戴胸罩不热吗?  
          论坛回复:我们不带你们会热……  
4.论坛楼主:和女友ML时,女友好像喊了另一个男人的名字……  
          论坛沙发:你日了别人的女人你还有什么不满意的!!!  
5.论坛楼主:为什么胡主席访问日本,日本方面比较冷淡,甚至机场连欢迎标语都没挂?  
          论坛沙发:怎么挂?热烈欢迎中国老朋友来日?    
6.论坛楼主:征集骂人最狠且不露脏字的一句话。  
          论坛回复31:你妈生你的时候是不是把人扔了,把胎盘养大了?  
7.论坛楼主:老爸送我老公一根鹿鞭,大家说这是啥意思?还有照片的说~  
          论坛沙发:这是老一辈对青年一代的鞭策啊~~~  
8.论坛楼主:《神雕侠侣》里小龙女胳膊上的守宫砂是什么东西,干什么用的?  
          论坛回复:守宫砂是处女的桌面快捷方式。  
9.论坛楼主:新闻说某人被蟒蛇吞了,请问在野外真的遇到蟒蛇怎么办?  
          论坛回复11:掐七寸,捅肛门,两个很有效的方法,希望大家广为传播。  
          论坛回复12:捅蛇的肛门还是自己的肛门?  
          ……  
          论坛回复47:屁话,荒郊野外的拿什么捅!  
          论坛回复48:许仙知道拿什么捅!  
10.论坛楼主:夏天得刮腋毛,否则穿短袖会影响我淑女形象的。那再弱弱问一句,阴毛用刮吗?  
          论坛沙发:不刮扎嘴!  
11.论坛楼主:帅有个屁用——到头来还不是被卒吃掉!  
          论坛回复:帅有士陪,有炮打,有马骑,有车坐,有相暗恋……帅怎么不好?!!  
12.论坛楼主:为什么生下的孩子要跟父亲一个姓?  
          论坛沙发:因为取款机里吐出的钱归插卡人所有。  
20.论坛楼主:我得了健忘症怎么办?  
          论坛回复:那岂不是很爽?每天早晨醒来发现睡在自己身旁的都是不同的女人        
21.论坛楼主:刚才不小心摔了一跤,结果JJ插进水泥地里拔不出来了,大家说我该怎么办啊?  
          论坛回复:老婆,快来看那,野生的JJ还能长出个人儿来!~(作者:gjqqkk)  
22.论坛楼主:该死的理发店把我头剪坏了!大家出点损招,要求破坏性越大越好,动静越小越好,因为是我一个人去。  
          论坛地下室:半夜三更,月黑风高,静静地、轻轻地,一个人吊死在理发店门口……  
23.论坛楼主:装B的实在是太多了,讨厌死了!  
          论坛沙发:LZ,Don’t zhuangbility,zhuangbility leads to leipility!(注释:莫装B,装B被雷劈!)  
24.论坛楼主:老娘我简直太有钱了,我该给保姆买辆什么车呢?  
         论坛回复:那就要看她跟你老公发展到什么关系了~(作者:elecon)  
25.论坛楼主:他今天山盟海誓说我是他生命中的一部分,我是他身体中的一部分,如果没了我,他就活不下去啦~  
          论坛沙发:我的前男友也是这么说的,后来我才知道,我是他盲肠、阑尾、仔耳、六指这类可有可无的玩意儿!  
26.论坛楼主(日本人):金庸阁下武侠小说里的点穴是否构想于我国的推拿按摩?  
          论坛沙发:非也,鄙国的穴是用来点的,贵国的穴是用来日的。
November 03

How to be compatible with UpdatePanel or ASP.NET AJAX

Suppose you have following function in your project:

C# Code:

        public void MessageBox(string msgText)
        {
            string scriptKey = "Message";

            if (!ClientScript.IsStartupScriptRegistered(scriptKey))
            {
                ClientScript.RegisterStartupScript(this.GetType(), scriptKey, "<script>alert('" + msgText + "')</script>");
            }
        }

VB Code:

    Protected Sub MessageBox(ByVal strMsg As String)

        Dim key As String = "Message"

            If Not ClientScript.IsStartupScriptRegistered(key) Then
                ClientScript.RegisterStartupScript(Me.GetType(), key, "<script>alert('" + strMsg + "');</script>")
            End If
        End If
    End Sub

This function is to show a client-side message box by using javascript. You might utilize this function like this:

C# Code:

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            ......code omitted 
            MessageBox("Search completed!");

        }

VB Code:

    Protected Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        ......code omitted

        MessageBox("Search completed!")

    End Sub

The code above has no problem at all if not using AJAX. With Ajax extension however, the button "btnSearch" will be placed inside an UpdatePanel control to avoid the page being post-back.  The MessageBox will not work as normal any more afterward. In such case, you might need to do some extra work on the MessageBox method.

C# Code:

        public void MessageBox(string msgText)
        {
            string scriptKey = "Message";

            if (ScriptManager.GetCurrent(this.Page)!=null && ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack {

               ScriptManager.RegisterStartupScript(this, this.GetType(), scriptKey , "alert('" + msgText+ "');", true);

            }else {

               if (!ClientScript.IsStartupScriptRegistered(scriptKey))
               {
                 ClientScript.RegisterStartupScript(this.GetType(), scriptKey, "<script>alert('" + msgText + "')</script>");
               }

            }
        }

VB Code:

    Protected Sub MessageBox(ByVal strMsg As String)

        Dim key As String = "Message"

        If ScriptManager.GetCurrent(Me.Page) IsNot Nothing AndAlso ScriptManager.GetCurrent(Me.Page).IsInAsyncPostBack Then
            'to support ajax post back
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), key, "alert('" + strMsg + "');", True)
        Else
            'normal call
            If Not ClientScript.IsStartupScriptRegistered(key) Then
                ClientScript.RegisterStartupScript(Me.GetType(), key, "<script>alert('" + strMsg + "');</script>")
            End If
        End If
    End Sub

Simply use RegisterStartupScript from ScriptManager (System.Web.Extension.dll) to generate javascript code instead of that from Page object.

Hope you enjoy with ASP.NET AJAX again!

October 22

Make StaticSiteMapProvider dynamically

The term "dynamic" has 2 meanings:
1) dynamically generate menu data when user click on the menu; and
2) generate menu in per user basis.
In this paper I only discuss the second one.
 
In ASP.NET 2.0,  together with Menu or TreeView control, you can use StaticSiteMapProvider to generate a "static" hierarchical navigation menu to the end user.  Most of the StaticSiteMapProvider sub-classes look like this:
 
 public class SiteMapProvider : StaticSiteMapProvider {
        private SiteMapNode root = null;
        public override SiteMapNode BuildSiteMap()
        {
            if (root != null) return root;
            lock (this)
            {
                base.Clear();
                ...generate your menu data here, e.g. get data from database
                root = new SiteMapNode(.......
            }
            return root;
        }
..............................
}//end of class
The BuildSiteMap function will run once and once only when the first visitor was opening the website, the root object will then be saved persistently and being reused during the lifetime of the web application. You may consider the root object as an application variable if you feel difficult to understand. As a result, other visitors will get exactly the same menu as the first one. That's why it named itself "Static".
 
In practice however, we are more prefer to get different menu items according to the login user's role or access right settings, rather than the stupid static items. A simple solution to make the SiteMapProvider dynamically is to discard the "root" - comment the first line of code, and that's it.

        public override SiteMapNode BuildSiteMap()
        {
            //if (root != null) return root;

It works as you expected, but this is extremely inefficient; "as multiple concurrent page requests can result indirectly in multiple calls to load site map information" (i.e. to run BuildSiteMap) - the MSDN explains. Set a breakpoint inside BuildSiteMap function, you will find what MSDN said is true. Loading one single page in the web project will call BuildSiteMap many times. Alright, if that is the case, the only thing we need is a session variable to make sure every users only run the BuildSiteMap once.  The final code:

public class DynamicSiteMapProvider : StaticSiteMapProvider {
//a kindly reminder that all private fields inside this class will be persistently available during the lifetime
//please remember to do extra init with these fields in the BuildSiteMap method

        private SiteMapNode root = null;
        public override SiteMapNode BuildSiteMap()
        {
            if (root != null&&HttpContext.Current.Session["AlreadySet"]!=null) return root;
            lock (this)
            {
                //next line is better to put on the first statement after "if", for concurrent reason
                HttpContext.Current.Session["AlreadySet"] = true;
                base.Clear();
                root = null;//init
                ...get "per-user" menu data here
                root = new SiteMapNode(......
 
            }
            return root;
        }
.................other code omitted....
}//end of class

 The solution above is trivial, a bit informal yet feasible. Actually SiteMapProvider has a property "securityTrimmingEnabled" to specify if the Provider equip with role. In that case, you need to override the "IsAccessibleToUser" method. More information please visit:
http://fredrik.nsquared2.com/viewpost.aspx?PostID=272&showfeedback=true
http://blogs.msdn.com/dannychen/archive/2006/03/16/553005.aspx

In order to finally solved the problem with a formal solution, we need to work with the ASP.NET 2.0 Role Manager, and Membership Provider if you want to.

First of all, define a series of roles that what menu/urls in your project they can access to, then modify your data structure to place your role information. E.g. add a "roles" field in your sitemap database table if you are using database storage. Using the following format if you are using xml file to store the sitemap:

 <siteMapNode title="Home" description="Home" url="~/default.aspx" roles="*">
  <siteMapNode title="menu 1" description="" roles="Admin,User" >
   <siteMapNode  roles="Admin" title="Admin Item" description="" url="~/admin.aspx" />
   <siteMapNode roles="User" title="User Item" description="" url="~/user.aspx" />
  </siteMapNode>
 </siteMapNode>

the menu will look like:
Home  (can be accessed by all users)
  - menu 1 (can be accessed by Admin and User
    - Admin Item (Can be accessed by Admin only)
    - User Item (can be accessed by User only)

*note: if you are not going to write your own xml SiteMapProvider, the ASP.NET 2.0 already has a simple one for you - System.Web.XmlSiteMapProvider. BUT, please be aware that the XmlSiteMapProvider only supports security trimming on menu/node who has child-node. In this case, if using XmlSiteMapProvider, the security trimming setting only works for "menu 1". In other word, "Admin Item" and "User Item" will be accessed by both "Admin" and "User" as "menu 1" specify.

Then, modify the Login.aspx:

....After verifying the login username and password
.... and get the roleName of login user
//init the roles, clean login user's role
string[] allRoles={"Admin","User"};
foreach (string role in allRoles)
{
    if (!Roles.RoleExists(role)) Roles.CreateRole(role);
    if (Roles.IsUserInRole(user, role)) Roles.RemoveUserFromRole(user, role)
}
//add the user to the role s/he belongs to
Roles.AddUserToRole(user, roleName);

Finally, modify the BuildSiteMap method of  SiteMapProvider, and override the IsAccessibleToUser. The main difference from previous version is that in the BuildSiteMap function, instead of loading "per-user" menu data, we just load a full set of menu, leave the IsAccessibleToUser to determine if a node should be shown or access to. 

public override SiteMapNode BuildSiteMap()
        {
            if (root != null) return root;

            lock (this)
            {
                base.Clear();
                ...get a full set of menu data here, but don't forget attaching role information to a node
                ... for example
                string role = .....get the roles of this menu, from database or xml, e.g "Admin,User"
                IList roles = new ArrayList();
                if (!string.IsNullOrEmpty(role)) {
                   string[] r=role.Split(",");
                   foreach (string item in r)
                       roles.Add(item)
               }
               SiteMapNode node = New SiteMapNode(this, menukey, url, title, desc, roles, null, null, null)
               ......other code omitted....
            }
            return root;
        }
public override bool IsAccessibleToUser (System.Web.HttpContext context, System.Web.SiteMapNode node)
        {
            if (!this.SecurityTrimmingEnabled) return true;
            if (node ==null || context==null || context.User==null) return false;
            if (node.Roles==null || node.Roles.Count<=0) return false;
            foreach(string role in node.Roles)
                if (role.Equals("*") || context.User.IsInRole(role))
                    return true;
            return false;
        }
 

Last step, don't forget to set SecurityTrimmingEnabled to true in web.config file.

    <siteMap defaultProvider="DynamicSiteMapProvider" enabled="true">
      <providers>
        <add name="DynamicSiteMapProvider" type="myProject.DynamicSiteMapProvidersecurityTrimmingEnabled="true" />
      </providers>
    </siteMap>

October 17

ASP.NET之缓存

以下是本人总结的一些关于缓存使用的规则和知识点
 
  • 如果对ASP.NET的缓存Cache理解有困难,请想象它是一个会自动过期的Application对象
  • 一个缓存对象的失效可以依赖于时间(超过一定时间)、文件(文件被修改、删除等)或者其他缓存对象,甚至可依赖于数据库内容被修改。异常的失效包括IIS被重启、应用程序异常终止、内存不足等。
  • HttpRuntime.Cache = Page.Cache = HttpContext.Current.Cache 获得的是同一个对象
  • 通常被缓存的对象按被缓存的时间划分可分为:
    • 超长期。通常是应用程序一开始就被缓存,到结束时才被释放,例如程序配置对象
    • 普通。通常是几分钟至几十分钟过期的对象,大部分的缓存都是这一类型
    • 超短期。这类缓存在页面被请求的时候建立,到页面请求结束时被销毁。将缓存对象存放于HttpContext.Current.Items中,每次请求结束时会被系统自动清除。通常用在服务器控件上,比如一个页面包含10个相同的服务器控件A。假设A需要到数据库中读一堆相同的数据,如果使用缓存,只需要访问数据库1次,否则需要访问数据库10次。由此可以展开联想,不同的控件如果操作相同的内容,也可以使用缓存。
  • 缓存不可过度使用,通常以下的数据是很好的缓存对象
    • 可以被重复多次使用的对象、计算结果
    • 数据是通用的,而不是属于特定用户、页面的
    • 如果是特定用户的数据,但寿命较长的,也可考虑缓存,但比较少使用

参考资料:http://msdn.microsoft.com/zh-cn/magazine/cc163854(en-us).aspx

September 20

solution for suffering from ASPNETHostPermission error

When you building web project in a company security policy environment (normally using network driver) especially a company with strict security policy, it's very common to encounter the ASPNETHostPermission exception. The error message goes like:
Request for permission of type System.Web.ASPNETHostPermission failed.
 
If you are lucky enough to read this, you will find the the follwing walkthrough is feasible.
  • Start -> Settings -> Control Panel
  • then open the Administrator tools, double click to run Microsoft .NET Framework 2.0 Configuration
  • expend My Computer, select Run Time Security Policy, finally click the task Adjust Zone Security on the right
  • if you have enough Windows privilege, the option "make change to this computer" should be selected by default, otherwise please contact your system administrator to perform this adjustment
  • change the security level of  Local Intranet to Full Trust
  • don't forget the last step, to close visual studio and start again, otherwise the adjustment will not be effected

Good luck!