Jump to content

CMS


pulpfiction

Recommended Posts

[ASP.NET 1.1/C#]I have to design something like a CMS. Its a intranet site which has an admin part who can modify the content of that intranet site. Im stuck up in how to create dynamic menus. Need some ideas of how to proceed, should I use datagrid or datalist or anything else to list out the links in menu whose URL and text is in database... also it has to be positioned on the left side. Any ideas?? Thanks.Pulpfiction.

Link to comment
Share on other sites

Just output the needed HTML code using <asp:Literal> or something like that, then you can still write valid semantic code using <ul></ul>I prefer to have a "template" that I load on the first line of my CMs apps. Then I proceed to replace template tags with database generate HTML that way in the end I get a clean HTML page.something like this

//executed query to get link name and urlstring menuHtml = @"<div class="menu"><ul>";while(dataReader.Read()){  menuHtml += String.Format(@"<li><a href="{0}">{1}</a></li>",	dataReader["linkName"].ToString(),	dataReader["linkUrl"].Tostring());}menuHtml += "</ul></div>";templateCode = templateCode.Replace("<cms:menu/>",menuHtml);//last line of programResponse.Write(templateCode);

Link to comment
Share on other sites

I typically stick with the Repeater control:

<asp:Repeater id="LinkRepeater" runat="server"><HeaderTemplate>  <ul></HeaderTemplate><ItemTemplate>  <li>	<a href="<%# DataBinder.Eval(Container.DataItem, "URL") %>">	  <%# DataBinder.Eval(Container.DataItem, "LinkText") %>	</a>  </li></ItemTemplate><FooterTemplate>  </ul></FooterTemplate></asp:Repeater>

Then just bind the data to that repeater just like you'd bind a DataGrid or DataList.

Link to comment
Share on other sites

Thanks guys....I might have to try out both methods, but method might work for my case as there is not fixed number of links, also the links are grouped into categories. and the number of groups are variable. so if im using a asp:repeater, then I need to create repeater on the fly. and may positioning it might get difficult..... Right???Menu would look something like this.GroupName1-Link11-Link12-Link13GroupName2-Link21-Link22-Link23

Link to comment
Share on other sites

I prefer to have a "template" that I load on the first line of my CMs apps. Then I proceed to replace template tags with database generate HTML that way in the end I get a clean HTML page.
Can you give more details about templates, some links or anything......
Link to comment
Share on other sites

when I say template I mean I have a file that contains all my XHTML code just like any other XHTML file except in areas that I want the CMS to manage I put in custom tags (I make them up) like <cms:menu/> or <cms:text1/>, etc, etc.Then I load that template file into a string at the beginning of my page then work to replace all the CMS tags with content from the database (ie. the menu generate I showed above).then on the last line I do a Response.Write of the modified templateCode string. Note: In my pageName.aspx file there is only the .Net declartion line and nothing else (it is all in the template file/database).

Link to comment
Share on other sites

I might have to try out both methods, but method might work for my case as there is not fixed number of links, also the links are grouped into categories. and the number of groups are variable. so if im using a asp:repeater, then I need to create repeater on the fly. and may positioning it might get difficult..... Right???
Heh, yeah, nested Repeaters can get a little hairy. The basics of it go like this:ASPX
<asp:Repeater id="GroupRepeater" OnItemDataBound="GroupRepeater_ItemDataBound" runat="server"><HeaderTemplate></HeaderTemplate><ItemTemplate>  <%# DataBinder.Eval(Container.DataItem, "GroupName") %>  <ul>  <asp:Repeater id="LinkRepeater" runat="server">  <HeaderTemplate></HeaderTemplate>  <ItemTemplate>    <li>      <a href="<%# DataBinder.Eval(Container.DataItem, "URL") %>">        <%# DataBinder.Eval(Container.DataItem, "LinkText") %>      </a>    </li>  </ItemTemplate>  <FooterTemplate></FooterTemplate>  </asp:Repeater>  </ul></ItemTemplate><FooterTemplate></FooterTemplate></asp:Repeater>

ASPX.CS

// First, you'll have to bind the data to the GroupRepeater.// This is the ItemDataBound event handler.  This fires each time an item is bound to the repeater.protected void GroupRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e){    RepeaterItem item = e.Item;    switch (item.ItemType)    {        case ListItemType.Item:        case ListItemType.AlternatingItem:            foreach (Control control in item.Controls)            {                if (control.GetType() == typeof(Repeater))                {                     Repeater repeater = (Repeater)control;                     // Get the current group ID from the RepeaterItem.                     int groupID;                     int.TryParse(DataBinder.Eval(item.DataItem, "GroupID").ToString(), out groupID);                     // Next, get all the links for that particular group and put them into some collection                     // Maybe a DataTable?  Don't know how you have this set up.                                          // Finally, set the DataSource for the Repeater with that collection:                     repeater.DataSource = data;                     repeater.DataBind();                     break;                }            }             break;    }}

Link to comment
Share on other sites

[ASP.NET 1.1/C#]I have to design something like a CMS. Its a intranet site which has an admin part who can modify the content of that intranet site. Im stuck up in how to create dynamic menus. Need some ideas of how to proceed, should I use datagrid or datalist or anything else to list out the links in menu whose URL and text is in database... also it has to be positioned on the left side. Any ideas?? Thanks.Pulpfiction.
First, there's no earthly reason why you have to do anything in your code behind.If you can't upgrade .Net 2.0 and use the <asp:menu> control, you can create dynamic menus with bulleted list control:
<asp:BulletedList	ID="BulletedList1"	runat="server"	DisplayMode="HyperLink"	DataSourceID="SqlDataSource1"	DataTextField="Title" DataValueField="ID"></asp:BulletedList><asp:SqlDataSource	ID="SqlDataSource1"	runat="server"	ConnectionString="<%$ ConnectionStrings:ASPNetConnectionString %>"	SelectCommand="Select ID, Title From Table"></asp:SqlDataSource>

That will display a bulleted list of hyperlinks.If you want to use a repeater, its virtually the same thing:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">	<ItemTemplate>		<div><a href="page.aspx?id=<%# DataBinder.Eval(Container.DataItem, "ID") %>">		<%# DataBinder.Eval(Container.DataItem, "Title") %></a></div>	</ItemTemplate></asp:Repeater><asp:SqlDataSource	ID="SqlDataSource1"	runat="server"	ConnectionString="<%$ ConnectionStrings:ASPNetConnectionString %>"	SelectCommand="Select ID, Title From Table"></asp:SqlDataSource>

The methods above are for simple, linear menus. If you need nested lists or display a tree list, then you run into issues: SQL doesn't know how to create hiearchial data. I've run into that problem, and I found a good workaround here. Basically, you set two fields in your database, a "depth" field and a "lineage field". An example table looks like this:

Some_Blog-----ID	Category	 Parent	Depth	Lineage1	 Computers	0		 0		/1/2	 Programming  1		 1		/1/2/3	 Cooking	  0		 0		/3/4	 Parts		2		 2		/1/2/4/5	 Websites	 1		 1		/1/5/6	 Languages	2		 2		/1/2/6/7	 Monitor	  4		 3		/1/2/4/7/8	 Vegan		3		 1		/3/8/9	 VB.Net	   6		 3		/1/2/6/9/10	Soups		3		 1		/3/10/11	C++		  6		 3		/1/2/6/11/

Searching, sorting, and displaying that kind of table in a list is very easy. When you sort by Lineage ASC, you get a perfectly hiearchial list, and you can sepearate levels by indenting your fields based on the depth. Its easiest to do that with a repeater:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">	<ItemTemplate>		<div style="margin-left:"<%# DataBinder.Eval(Container.DataItem, "depth") %>em">		<a href="page.aspx?id=<%# DataBinder.Eval(Container.DataItem, "ID") %>">		<%# DataBinder.Eval(Container.DataItem, "Category") %></a>		</div>	</ItemTemplate></asp:Repeater><asp:SqlDataSource	ID="SqlDataSource1"	runat="server"	ConnectionString="<%$ ConnectionStrings:ASPNetConnectionString %>"	SelectCommand="SELECT ID, Category, Depth FROM Some_Blog ORDER BY Lineage"></asp:SqlDataSource>

I use that kind of code and table structure on my own sites. The nice part about the code above is that it displays a data tree in a SINGLE query (and its very fast), whereas using nested repeaters require dozens and dozens of queries.

Link to comment
Share on other sites

The nice part about the code above is that it displays a data tree in a SINGLE query (and its very fast), whereas using nested repeaters require dozens and dozens of queries.
Nah, I only use a single query against the database when I use nested repeaters. I then load the data into a DataTable object and use the DataView to get at the data. Sure, filters are being applied to the DataView in code behind, but there's still only one query against the database.
First, there's no earthly reason why you have to do anything in your code behind.
True, you don't have to put anything in the code behind. But I find that the pages are a thousand times more easy to manage if you separate the markup from the code by placing all the code in a code behind.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...