65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 中的基本 CMS 框架

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.29/5 (7投票s)

2007年8月7日

CPOL

1分钟阅读

viewsIcon

57402

downloadIcon

1300

构建您自己的内容管理系统的基本基础。

Screenshot - Screenshot_Small.jpg

引言

本示例旨在演示如何使用三个主要组件创建简单的内容管理系统:母版页、默认 ASPX 页面和 Web 用户控件。

使用代码

我们需要处理三个主要组件

  1. 母版页 - 将包含我们的设计
  2. Default.aspx - 将调用 Web 用户控件
  3. 用户控件 (ASCX) - 内容的模块

我们从母版页开始,它非常、非常简单。 此时我们甚至不需要放置任何后台代码。

<%@ Master Language="C#" AutoEventWireup="true" 
           CodeFile="MasterPage.master.cs" Inherits="CMMforStarters.MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html>
   <head id="Head1" runat="server">
       <title>Master Page</title>
        <link href="Main.css" rel="Stylesheet" type="text/css" />
   </head>
   <body>
   <form id="Form1" runat="server">
   <asp:table id ="MainTbl"  width = "100%" cellpadding = "0" 
                cellspacing ="0" runat="server">
   <asp:tablerow>
        <asp:tablecell ColumnSpan = "3">
        <table id="HeaderContainerTbl"  border="0"  width = "100%" 
                      cellpadding = "0" cellspacing ="0"> 
            <tr>
                <td>
                    <table id="HeaderContainerInnerTbl" width="100%" 
                                height="100" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width = "100%">
                              <img src="images/spacer.gif" width="1" height="1" alt="">
                            </td>
                        </tr>
                        <tr>
                            <td width = "100%">
                              <img src="images/spacer.gif" width="1" height="1" alt="">
                            </td>
                        </tr>
                        <tr>
                            <td bgcolor="99ccff" width = "100%">
                              <img src="images/spacer.gif" width="1" height="1" alt="">
                            </td>
                        </tr>
                    </table>   
                </td>
            </tr>
        </table>
   </asp:tablecell>
   </asp:tablerow>

   <asp:tablerow>
        <asp:tablecell VerticalAlign = "top" ColumnSpan = 3>
            <table id="TopContainerTbl"  border="0" width = "100%" 
                       cellpadding = "0" cellspacing ="0">
                <tr>
                    <td class="Normal"> 
                        <asp:contentplaceholder id="TopContainer" runat="Server">
                        </asp:contentplaceholder>   
                    </td>
                </tr>
            </table>          
        </asp:tablecell>
   </asp:tablerow>
   <asp:tablerow>
    
        <asp:tablecell VerticalAlign = "top">
            <table id="LeftContainerTbl"  border="0" width = "100%" 
                      cellpadding = "0" cellspacing ="0">
                <tr>
                    <td class="Normal"> 
                        <asp:contentplaceholder id="LeftContainer" runat="Server">
                        </asp:contentplaceholder>   
                    </td>
                </tr>
            </table>          
        </asp:tablecell>
   
        <asp:tablecell VerticalAlign = "top">
            <table id="CenterContainerTbl"  border="0"  width = "100%" 
                           cellpadding = "0" cellspacing ="0">
                <tr>
                    <td class="Normal">                                     
                        <asp:contentplaceholder id="CenterContainer" runat="Server">
                        </asp:contentplaceholder>                
                    </td>
                </tr>
            </table>
        </asp:tablecell>
    
        <asp:tablecell VerticalAlign = "top">
            <table id="RightContainerTbl"  border="0"  width = "100%" 
                           cellpadding = "0" cellspacing ="0">
                <tr>
                    <td> 
                        <asp:contentplaceholder id="RightContainer" runat="Server">
                        </asp:contentplaceholder>   
                    </td>
                </tr>
             </table>
        </asp:tablecell>          
        
    </asp:tablerow>
   <asp:tablerow>
        <asp:tablecell VerticalAlign = "top" ColumnSpan = 3>
            <table id="BottomContainerTbl"  border="0" width = "100%" 
                           cellpadding = "0" cellspacing ="0">
                <tr>
                    <td class="Normal"> 
                        <asp:contentplaceholder id="BottomContainer" runat="Server">
                        </asp:contentplaceholder>   
                    </td>
                </tr>
            </table>          
        </asp:tablecell>
   </asp:tablerow>
    <asp:TableRow>
        <asp:TableCell ColumnSpan = "3" >
         <table id="FooterTbl"  border="0"  width = "100%" 
                           cellpadding = "0" cellspacing ="0">
                <tr>
                    <td> 
                        <table id="FooterTblInner" width="100%" height="29" 
                                    border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td bgcolor="99ccff" width = "100%">
                                  <img src="images/spacer.gif" width="1" height="1" alt="">
                                </td>
                            </tr>
                            <tr>
                                <td bgcolor="99ccff" width = "100%">
                                  <img src="images/spacer.gif" width="1" height="1" alt="">
                                </td>
                            </tr>

                        </table>
                        
                    </td>
                </tr>
             </table>
        </asp:TableCell>
    </asp:TableRow>
   </asp:table>
                
     
   </form>
   </body>
   </html>

母版页就像一个普通的 HTML 页面,其中包含我们需要的一些设计内容;只需注意 <asp:contentplaceholder>,因为这是我们将渲染 CMS 控件的部分。

接下来是 default.aspx,它将渲染控件。 让我们进入 ASPX 部分,这是一个非常简单、朴素的代码。

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" 
  AutoEventWireup="true" CodeFile="Default.aspx.cs" 
  Inherits="CMMforStarters.ControlLoader" Title="Untitled Page" %>

<asp:content id="ContentTop" contentplaceholderid="TopContainer" runat="server">
    <asp:PlaceHolder ID="PlaceHolderTop" runat="server"></asp:PlaceHolder>
</asp:content>

<asp:content id="ContentLeft" contentplaceholderid="LeftContainer" runat="server">
    <asp:PlaceHolder ID="PlaceHolderLeft" runat="server"></asp:PlaceHolder>
</asp:content>

<asp:content id="ContentCenter" contentplaceholderid="CenterContainer" runat="server">
    <asp:PlaceHolder ID="PlaceHolderCenter" runat="server"></asp:PlaceHolder>
</asp:content>

<asp:content id="ContentRight" contentplaceholderid="RightContainer" runat="server">
    <asp:PlaceHolder ID="PlaceHolderRight" runat="server"></asp:PlaceHolder>
</asp:content>


<asp:content id="ContentBottom" contentplaceholderid="BottomContainer" runat="server">
    <asp:PlaceHolder ID="PlaceHolderBottom" runat="server"></asp:PlaceHolder>
</asp:content>

只需注意它具有 MasterPageFile="~/MasterPage.master",这就是我们引用母版页的方式,以便由 default.aspx 使用;除此之外还有 <asp:PlaceHolder>,我们稍后会将控件放置在其中。

接下来是 default.aspx.cs

SqlConnection conn = null;
SqlDataReader rdr = null;

/// Variable initialization
string sWhereCondition = "PortalID = 1 and PageID = " + iPageID.ToString();
string sOrderByExpression = "SortOrder";
try
{
conn = new SqlConnection(ConnStr);
conn.Open();

//Get the contents of the website from a SQL Database
SqlCommand cmd = new SqlCommand("SelectPageContentsDynamic", conn);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@WhereCondition", sWhereCondition));
cmd.Parameters.Add(new SqlParameter("@OrderByExpression", sOrderByExpression));

rdr = cmd.ExecuteReader();
while (rdr.Read())
{
    string ControlPath = 
       GetControlLocation(int.Parse(rdr["ControlID"].ToString()));

    UserControl ucControl = (UserControl)Page.LoadControl(ControlPath);
    Type ucType = ucControl.GetType();
    
    // Getting the property of the User Control and set its value
    PropertyInfo ucProperty_PropertyID = ucType.GetProperty("PropertyID");
    ucProperty_PropertyID.SetValue(ucControl, 
               int.Parse(rdr["ContentID"].ToString()), null);

    //Placing the Controls to their proper Placeholders    
    switch (rdr["ControlLocation"].ToString())
    {
        case "Top":
            PlaceHolderTop.Controls.Add(ucControl);
            break;
        case "Left":
            PlaceHolderLeft.Controls.Add(ucControl);
            break;
        case "Center":
            PlaceHolderCenter.Controls.Add(ucControl);
            break;
        case "Right":
            PlaceHolderRight.Controls.Add(ucControl);
            break;
        case "Bottom":
            PlaceHolderBottom.Controls.Add(ucControl);
            break;
    }
}
conn.Close();
conn.Dispose();

正如您所见,我们正在动态渲染控件并一路分配属性值;您可以通过引用 System.Reflection; 来实现这一点。 最后是一个示例 Web 用户控件,在本例中,它将 HTML 渲染到 Literal 控件。

public partial class Controls_HTML : System.Web.UI.UserControl
{
    private int _PrimaryID = 0;

    public int PropertyID
    {
        get { return _PrimaryID; }
        set { _PrimaryID = value; }
    }

    string ConnStr = 
      System.Configuration.ConfigurationManager.AppSettings.Get("GlobalConn").ToString();

    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection conn = null;
        SqlDataReader rdr = null;


        conn = new SqlConnection(ConnStr);
        conn.Open();

        SqlCommand cmd = new SqlCommand("SelectHTML", conn);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@ID", _PrimaryID));

        rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            HTMLLiteral.Text = rdr["HTMLContent"].ToString();
        }
        conn.Close();
        conn.Dispose();
    }
}
© . All rights reserved.