Generating site "posts"
-
In my site i have a div to represent a blog post like below
<div class="post"> <div class="header"> <h3>Header</h3> <div class="date">May 18, 2009</div> </div> <div class="content"> Some Content </div> </div>
Basically, what i'm wanting to do is select the 10 recent posts from a database and build the page with these posts using the div tags above. I can already do the easy things like setting the header, date, and content. Thanks,
Don't be overcome by evil, but overcome evil with good
-
In my site i have a div to represent a blog post like below
<div class="post"> <div class="header"> <h3>Header</h3> <div class="date">May 18, 2009</div> </div> <div class="content"> Some Content </div> </div>
Basically, what i'm wanting to do is select the 10 recent posts from a database and build the page with these posts using the div tags above. I can already do the easy things like setting the header, date, and content. Thanks,
Don't be overcome by evil, but overcome evil with good
So, what is your question?
Yusuf May I help you?
-
So, what is your question?
Yusuf May I help you?
-
In my site i have a div to represent a blog post like below
<div class="post"> <div class="header"> <h3>Header</h3> <div class="date">May 18, 2009</div> </div> <div class="content"> Some Content </div> </div>
Basically, what i'm wanting to do is select the 10 recent posts from a database and build the page with these posts using the div tags above. I can already do the easy things like setting the header, date, and content. Thanks,
Don't be overcome by evil, but overcome evil with good
I think i figured it out. The solution i found is something like this: (i know this won't generate the markup in the original post, this is in a seperate test project)
ContentPlaceHolder leftContent = (ContentPlaceHolder)Master.FindControl("leftContent"); HtmlGenericControl div = new HtmlGenericControl("div"); div.Attributes.Add("class", "post"); HtmlGenericControl h2 = new HtmlGenericControl("h2"); h2.InnerText = "My Header"; HtmlGenericControl p = new HtmlGenericControl("p"); p.InnerText = "This is some content."; div.Controls.Add(h2); div.Controls.Add(p); leftContent.Controls.Add(div);
which will then generate this:
<div class="post"><h2>My Header</h2><p>This is some content.</p></div>
Is there a better way to achieve what i'm trying to do? I feel like i'm going against the grain here.
Don't be overcome by evil, but overcome evil with good
-
In my site i have a div to represent a blog post like below
<div class="post"> <div class="header"> <h3>Header</h3> <div class="date">May 18, 2009</div> </div> <div class="content"> Some Content </div> </div>
Basically, what i'm wanting to do is select the 10 recent posts from a database and build the page with these posts using the div tags above. I can already do the easy things like setting the header, date, and content. Thanks,
Don't be overcome by evil, but overcome evil with good
Use asp.net repeater control.
<asp:Repeater id="rPOSTS" runat="server">
<ItemTemplate>
</ItemTemplate>
asp:RepeaterOn ItemTemplate you will put the code you want to repeat X times. So, in code behind you will do something like this: rPOSTS.DataSouce = YOUR_DB_DATA; rPOSTS.DataBind(); See more about repeater control in Google[^].
-
Use asp.net repeater control.
<asp:Repeater id="rPOSTS" runat="server">
<ItemTemplate>
</ItemTemplate>
asp:RepeaterOn ItemTemplate you will put the code you want to repeat X times. So, in code behind you will do something like this: rPOSTS.DataSouce = YOUR_DB_DATA; rPOSTS.DataBind(); See more about repeater control in Google[^].
Sweet! That was easy enough :) For future readers this is the result that paulo helped me come up with in the aspx file:
<asp:Repeater runat="server" ID="postRepeater"> <HeaderTemplate> <div class="post"> </HeaderTemplate> <ItemTemplate> <h2> <%#DataBinder.Eval(Container.DataItem, "Header")%></h2> <p> <%#DataBinder.Eval(Container.DataItem, "Content")%></p> </ItemTemplate> <FooterTemplate> </div> </FooterTemplate> </asp:Repeater>
and in the code behind:
System.Data.DataTable dt = new System.Data.DataTable("Posts"); dt.Columns.Add(new System.Data.DataColumn("Header")); dt.Columns.Add(new System.Data.DataColumn("Content")); System.Data.DataRow row = dt.NewRow(); row\[0\] = "Header 1"; row\[1\] = "Content 1"; dt.Rows.Add(row); postRepeater.DataSource = dt; postRepeater.DataBind();
Thanks again :)
Don't be overcome by evil, but overcome evil with good