作者: Keefe Dunn

  • Dynamic Tree Construction according to User Role and Page

    In the developing environment of C# and .NET, dynamic data binding isn’t anything new at all. I’m here just to make a summary of how to do the job to a TreeView object, and add a few contents depending on my practical experience.

    Keywords: dynamic tree construction, user role, page.

    Problem Description

    In the classic three-layer architecture adopted by my project, we have

    1. data tables that are dealt with by a DAL-located class database.cs;

    2. a series of corresponding BLL classes to manipulate data retrieved from those tables;

    3. aspx pages and aspx.cs code files in the presentation layer.

    which means following elements are involved:

    1. data table User, UserRole, RoleTree;

    2. class User.cs, UserRole.cs, RoleTree.cs;

    3. example page sampleTree.aspx and its code file sampleTree.aspx.cs.

    A picture speaks more than a thousand words. Let’s first take a look at the chart below to get an overview of how this work is going to be done.

    Solution Pt1: User Login

    By the input username and password, we first get the userId, through which user role(s) are obtained and stored in a Session object. In case of more than one role for a specific user, we take an ArrayList as the role container.

    if (Page.IsValid)
    {
        User usr = new User();
        string userId = "";
        SqlDataReader recs = usr.GetUserLogin(txtUserName.Text.Trim(), txtPassword.Text.Trim());

        if (recs.Read())
        {
            userId = recs["UserID"].ToString();
        }
        recs.Close();

        if ((userId != String.Empty) && (userId != ""))
        {
            // get user roles, put them into session
            ArrayList alRoleIDs = new ArrayList();
            UserRole ur = new UserRole();
            SqlDataReader dr = ur.GetRoleByUserID(Int32.Parse(userId));
            while (dr.Read())
            {
                alRoleIDs.Add(Int32.Parse(dr["RoleID"].ToString()));
            }
            Session.Add("RoleIDs", alRoleIDs);
            dr.Close();
        }

    Solution Pt2: Tree Construction

    Then we take the RoleIDs of ArralyList above and some PageID of int as arguments to get the tree. Codes below are sample codes from sampleTree.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindTreeData(treeSidebar, (ArrayList)Session["RoleIDs"]);
        }
    }

    private void BindTreeData(TreeView tv, ArrayList al)
    {
        RoleTree rt = new RoleTree();
        // when applied to different pages, take RoleIDs of ArrayList and PageID of int as arguments
        DataTable dt = rt.GetTreeByRoleIDandPageID(al, 3); // 3 is an exmaple of pageId
        tv.Nodes.Clear();

        foreach (DataRow row in dt.Select())
        {
            TreeNode tempNode = new TreeNode();
            tempNode.Text = row["Title"].ToString();
            tempNode.Value = row["NodeID"].ToString();
            tempNode.Expanded = false;
            tempNode.NavigateUrl = row["Url"].ToString();

            tv.Nodes.Add(tempNode);
        }
    }

    Further Notice

    //It is especially useful when applied to MasterPages.

    //This solution does not take node’s parent into account, which means the TreeView object constructed is not a real tree, strictly speaking.

  • 日省吾身081112

    GIVE AND IT WILL BE GIVEN UNTO YOU

    给予,你也终将得到
    LUKE 6:38

    Kindness and love to all I owe
    No other debt doth God allow
    Kindness and love then I must pay
    To everybody every day.

    将友善与爱给我所亏欠的,上帝不会允许你付出其它情义

    友善与爱,给每个人,在每一天
    -ANON

    (更多…)

  • DataFormatString in GridView

    I’ve been working on an ASP.NET project lately, which means it’s high time I write something down to build up my own knowledge base and track down my trail of learning how to code effectively. Hence this article, just another ASP.NET record.

    (更多…)

  • Here Comes the New President of United States

    Whoever the author of Yimmys Yayo is, he’s a genius. I don’t know if he drew the cartoon below. Anyway, it’s thought-provoking.

    (via)

    Since Obama has made the first step, which means history has been rewritten. I start looking forward to the first female President now.

  • 启用新域名和新标题

    一直用一个域名管理两个博客实在不是办法,今天一狠心又买进一个域名,还是在 GoDaddy 。GoDaddy 的服务确实不错,可惜好端端的主页忽然被封了。生生祭起 Hotspot Shield 这杆 VPN 大旗才强行登录到其页面。说起来 Hotspot Shield 确实好用,在我这儿速度比 Vidalia + Firefox + FoxyProxy 要快得多,用 VFF 方案试过好几次都超时,HS一次性就搞定了,速度飞快。于是本站正式启用新域名 infia.org ,顺带把标题也改了,应景。

    在 GoDaddy 购物一定记得使用 coupon ,不管是 buy 或者 renew 都能够省下不少银子,promo-code.net 上有很多现成的可用。如果还有其它分享优惠码的好站,大家尽管补充。

  • Domain Name Reconstruction and Title Change

    I’m not so satisfied to run 2 blog under one domain name, which means I have to use sub-domain or sub-directory. So I got a new domain infia.org today and assigned it to my other blog in Chinese. This way I got ralphdunn.com pointing back to this blog again, which was great. The only drawback is, the connection between these 2 blogs doesn’t seem to be that tense anymore.

    Also, I changed the title used for half a year. No particular reason, I just need something fresh. Do you like the word “ralify”? ^^

    Hope GoDaddy gets unblocked soon. It sucks that I can’t connect to the homepage directly.

  • Hotspot Shield – Protection and Breaking through

    GoDaddy’s homepage turned unreachable from yesterday, when I was just about to get a new domain name there. Then I realized it was high time I do something to retain my right surf the Internet freely. I don’t like being fooled again and again, or being another victim of the Great Wall, if you know what I mean.

    Solution 1: Vidalia bundle + Firefox + FoxyProxy plugin

    This one comes in handy if you were a user of Tor. When the tor circle is established, you just need to configure the FoxyProxy and add some URL patterns to use it. I used to use Torpark to do that, during which time IE-core based browser was my primary choice. To be honest, I was not content with the speed surfing with Torpark. Howerver, It’s not tor’s fault, since the network environment is so choky all along. Unfortunately, the low-speed connection still drives me crazy that I have to give it up. Pretty good project though, Vidalia.

    BTW, you don’t need to install Privoxy in the bundle, Vidalia and Tor is enough for normal user to get FoxyProxy to work properly.

    Solution 2: Hotspot Shield (VPN)

    VPN = Virtual Private Network, which tends to create another wide-area network through Internet with users of this technology. This way you could stay secure and anonymous while being connected to the Net. Hotspot Shield is a typical application of VPN produced by AnchorFree. It not only hides your IP while you’re online, but also ensures your access to all the content without censorship. (Oh, yeah, that’s what I’m looking for. Break through the Wall!) What’s more, you don’t need to know how it works because no configuration is required at all. Install and run, that’s it!

    BTW, I heard that there’s a bandwidth limit to this app: 3GB per month. I’m not quite sure if it is still the case, but 3GB is way more than enough as long as you’re not downloading media files through it.

  • 三人成虎与忽悠群众

    我就奇了,怎么一夜之间国内几个名气很大的博客同时推荐起一个新站点来。起初没在意,后来多次看到还是忍不住 grab 了 feed 。进入网站一看,这站还没怎么建设完成吧?主页 post 的图片好多一样的。三人成虎果然不虚,我是稀里糊涂就被忽悠进去了。

    (更多…)

  • 为什么说中文总是省略重要信息

    今儿上班一同事打趣说让我请吃饭,什么原因忘了,只记得回道:“行,改天吧。”我们从这句话里得到什么信息呢?改天?具体改哪天?不知。如此重要的信息就此丢失。当然,这么说其实就是推脱的意思。婉转,或者说含蓄。同样的话英文的习惯表达是怎样的呢?"Maybe some other day."瞧,人家至少会提到"other",虽然仍然没给出具体时间,却更直接。

    再以本文的标题为例,咱直接把主语省略掉了,谁“说”的?多关键的问题。英文怎么说?"Why would I say essential information is always omitted in Chinese expressions."这不,"I"出来了,绝不藏头露尾。还是直白好,一如为人处世,虚与委蛇没意思。

    这么说绝没有贬低咱们母语的意思,相反,中文的博大精深绝不是其它语言可以比拟的,几千年的沉淀(虚与委蛇的“蛇”可别念she)。提出这个问题纯属闲得蛋疼,真的。

  • OpenID Enabled in RiWiA

    The OpenID logo

    Image via Wikipedia

    Just finished integrating OpenID support into this blog. It’s even more convenient to make comments here now. Without registration or leaving username and email information, a simple line of URL is sufficient for you to say anything you want.

    What is OpenID anyway?

    OpenID is a simple registration solution across Internet. Register once with a trusted Provider and you’ll get a unique URL that identifies you. Then you’ll be able to login to a bunch of sites (including RiWiA) using that URL. No more registration, no more username and password. For a complete list of organizations which support OpenID, check this page out (Wow, look what we’ve got, Digg, Microsoft, AOL, Yahoo, LiveJournal, MediaWiki, WordPress, 37Signals, etc). In a word,

    OpenID eliminates the need for multiple usernames across different websites, simplifying your online experience.

    How to add OpenID support for self-hosted WordPress weblog?

    It’s easy. The OpenID plugin will help you out with this. It’s as easy-to-use as other WordPress plugins. Upload the necessary files and activate it. Then go to “Users” –> “Your Profile OpenIDs” in the admin panel and add your OpenID URL to your account. Now you’ll be able to login to your blog with that URL, and your OpenID Provider will take care of the authentication.

    If you’d like to use your own blog URL as your OpenID instead of the one with Provider info in it, just add following lines of code to the <header> of your theme (take myopenid.com as the Provider for example):

    <link rel="openid.server" href="http://www.myopenid.com/server" />
    <link rel="openid.delegate" href="http://yourname.myopenid.com" />
    <meta content=‘http://www.myopenid.com/xrds?username=yourname.myopenid.com’ http-equiv=‘X-XRDS-Location’ />

    That’s it.

    Why would I use OpenID?

    Er… It’s a good question. I think

    1. It’s fun.
    2. It simplify the process of leaving a comment for those who have an OpenID, so it’s an enjoyable experience for them to comment in your blog.
    3. Everybody else are using it, so why shouldn’t we?
    Enhanced by Zemanta