⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 forumdatalistcontrol.cs

📁 微软的.NET论坛的源代码(COOL!!!)
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetForums;
using AspNetForums.Components;
using System.ComponentModel;
using System.IO;
using System.Web.Security;

namespace AspNetForums.Controls.BaseClasses {

    [
    ParseChildren(true)	
    ]
    public abstract class ForumDataListControl : DataList {
        User user = null;
        int forumID = -1;
        int forumGroupID = -1;
        int postID = -1;
        int threadID = -1;
        string skinName = null;

        public ForumDataListControl() {

            // Attempt to get the current user
            user = Users.GetLoggedOnUser();

            // Is the user not availabe - must be anonymous
            if (user == null)
                Users.TrackAnonymousUsers();

            // Set the siteStyle for the page
            if (user != null)
                skinName = user.SiteStyle;
            else
                skinName = Globals.SiteStyle;

            // If we have an instance of context, let's attempt to
            // get the ForumID so we can save the user from writing
            // the code
            if (null != Context) {
                GetPostIDFromRequest();
                GetForumIDFromRequest();
                GetForumGroupIDFromRequest();
            }            
        }

        
        
        // *********************************************************************
        //  CreateChildControls
        //
        /// <summary>
        /// This event handler adds the children controls.
        /// </summary>
        // ***********************************************************************/
        protected override void CreateChildControls() {
            // Apply the templates to this control. We'll use a default template if
            // we don't find a skinned template
            ApplyTemplates();

        }

        
        // *********************************************************************
        //  ApplyTemplates
        //
        /// <summary>
        /// Applies templates to control the ui generated by the control. If no
        /// template is specified a custom template is used. If a template is found
        /// in the skins directory, that template is loaded and used. If a user defined
        /// template is found, that template takes priority.
        /// </summary>
        /// 
        // ********************************************************************/
        private void ApplyTemplates() {
            string pathToHeaderTemplate;
            string pathToFooterTemplate;
            string pathToItemTemplate;
            string pathToAlternatingItemTemplate;
            string keyForHeaderTemplate;
            string keyForItemTemplate;
            string keyForAlternatingItemTemplate;
            string keyForFooterTemplate;

            // Are we using skinned template?
            if (Page != null) {

                // Set the file paths to where the templates should be found
                keyForHeaderTemplate = Globals.Skin + "/Templates/UserList-Header.ascx";
                keyForItemTemplate = Globals.Skin + "/Templates/UserList-Item.ascx";
                keyForAlternatingItemTemplate = Globals.Skin + "/Templates/UserList-AlternatingItem.ascx";
                keyForFooterTemplate = Globals.Skin + "/Templates/UserList-Footer.ascx";

                // Set the file paths to where the templates should be found
                pathToHeaderTemplate = Globals.ApplicationVRoot + "/Skins/" + keyForHeaderTemplate;
                pathToItemTemplate = Globals.ApplicationVRoot + "/Skins/" + keyForItemTemplate;
                pathToAlternatingItemTemplate = Globals.ApplicationVRoot + "/Skins/" + keyForAlternatingItemTemplate;
                pathToFooterTemplate = Globals.ApplicationVRoot + "/skins/" + keyForFooterTemplate;

                // Attempt to get the skinned header template
                if (HeaderTemplate == null)
                    HeaderTemplate = Globals.LoadSkinnedTemplate(pathToHeaderTemplate, keyForHeaderTemplate, Page);

                // Attempt to get the skinned item template
                if (ItemTemplate == null)
                    ItemTemplate = Globals.LoadSkinnedTemplate(pathToItemTemplate, keyForItemTemplate, Page);

                // Attempt to get the skinned alternating item template
                if (AlternatingItemTemplate == null)
                    AlternatingItemTemplate = Globals.LoadSkinnedTemplate(pathToAlternatingItemTemplate, keyForAlternatingItemTemplate, Page);

                // Attempt to get the footer template
                if (FooterTemplate == null)
                    FooterTemplate = Globals.LoadSkinnedTemplate(pathToFooterTemplate, keyForFooterTemplate, Page);
            }

            // If the item template is null we force our view
            if (ItemTemplate == null) {
                ExtractTemplateRows = true;
                HeaderTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildHeaderTemplate));
                ItemTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildItemTemplate));
                FooterTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildFooterTemplate));
            }
        }

        protected abstract void BuildHeaderTemplate(Control template);
        protected abstract void BuildItemTemplate(Control template);
        protected abstract void BuildFooterTemplate(Control template);

        // *********************************************************************
        //  GetPostIDFromRequest
        //
        /// <summary>
        /// Retrieves the PostID from the request querystring/post.
        /// </summary>
        // ***********************************************************************/
        private void GetPostIDFromRequest() {
            // Attempt to get the post id, throw if it is invalid
            try {
                if (null != Context.Request.QueryString["PostID"]) {
                    string postID = Context.Request.QueryString["PostID"];

                    // Contains a #
                    if (postID.IndexOf("#") > 0)
                        postID = postID.Substring(0, postID.IndexOf("#"));

                    PostID = Convert.ToInt32(postID);
                } else if (null != Context.Request.Form["PostId"]) {
                    PostID = Convert.ToInt32(Context.Request.Form["PostID"]);
                }
            } catch (Exception e) {
                HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.PostDoesNotExist));
                HttpContext.Current.Response.End();
            }
        }


        // *********************************************************************
        //  GetForumIDFromRequest
        //
        /// <summary>
        /// Retrieves the ForumID from the request querystring/post.
        /// </summary>
        // ***********************************************************************/
        private void GetForumIDFromRequest() {

            // Attempt to get the forum id, throw if it is invalid
            try {
                if (null != Context.Request.QueryString["ForumID"]) {
                    string forumID = Context.Request.QueryString["ForumID"];

                    // Contains a #
                    if (forumID.IndexOf("#") > 0)
                        forumID = forumID.Substring(0, forumID.IndexOf("#"));

                    ForumID = Convert.ToInt32(forumID);
                } else if (null != Context.Request.Form["ForumId"]) {
                    ForumID = Convert.ToInt32(Context.Request.Form["ForumID"]);
                }
            } catch (Exception e) {
                HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.UnknownForum));
                HttpContext.Current.Response.End();
            }
        }


       
        // *********************************************************************
        //  GetForumGroupIDFromRequest
        //
        /// <summary>
        /// Retrieves the ForumID from the request querystring/post.
        /// </summary>
        // ***********************************************************************/
        private void GetForumGroupIDFromRequest() {

            // Attempt to get the forum id, throw if it is invalid
            try {
                if (null != Context.Request.QueryString["ForumGroupID"])
                    ForumGroupID = Convert.ToInt32(Context.Request.QueryString["ForumGroupID"]);
                else if (null != Context.Request.Form["ForumGroupID"])
                    ForumGroupID = Convert.ToInt32(Context.Request.Form["ForumGroupID"]);
            } catch (Exception e) {
                HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.UnknownForum));
                HttpContext.Current.Response.End();
            }
        }

        // *********************************************************************
        //  ForumUser
        //
        /// <summary>
        /// Returns an instance of User or null if the user is not logged in.
        /// </summary>
        // ***********************************************************************/
        protected User ForumUser {
            get {
                return user;
            }
        }
        
        // *********************************************************************
        //  ForumID
        //
        /// <summary>
        /// If available returns the forum id value read from the querystring.
        /// </summary>
        /// 
        // ********************************************************************/ 
        public int ForumID  {
            get  {
                return forumID;
            }
            set  {
                forumID = value;
            }
        }

        
        // *********************************************************************
        //  ForumGroupID
        //
        /// <summary>
        /// If available returns the forum group id value read from the querystring.
        /// </summary>
        /// 
        // ********************************************************************/ 
        public int ForumGroupID  {
            get  {
                return forumGroupID;
            }
            set  {
                forumGroupID = value;
            }
        }

        // *********************************************************************
        //  PostID
        //
        /// <summary>
        /// If available returns the post id value read from the querystring.
        /// </summary>
        /// 
        // ********************************************************************/ 
        public int PostID {
            get {
                return postID;
            }
            set {
                postID = value;
            }
        }

        // *********************************************************************
        //  ThreadID
        //
        /// <summary>
        /// If available indicates a top level post
        /// </summary>
        /// 
        // ********************************************************************/ 
        public int ThreadID {
            get {
                return threadID;
            }
            set {
                threadID = value;
            }
        }

        
        // *********************************************************************
        //  SkinName
        //
        /// <summary>
        /// Used to construct paths to images, etc. within controls.
        /// </summary>
        /// 
        // ********************************************************************/ 
        protected string SkinName {
            get {
                return skinName;
            }
            set {
                skinName = value;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -