Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Generating site "posts"

Generating site "posts"

Scheduled Pinned Locked Moved ASP.NET
database
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    teejayem
    wrote on last edited by
    #1

    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

    Y T P 3 Replies Last reply
    0
    • T teejayem

      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

      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      So, what is your question?

      Yusuf May I help you?

      T 1 Reply Last reply
      0
      • Y Yusuf

        So, what is your question?

        Yusuf May I help you?

        T Offline
        T Offline
        teejayem
        wrote on last edited by
        #3

        what i can't figure out is how to create dynamically create div's for each post (as opposed to hardcoding them in the aspx file).

        Don't be overcome by evil, but overcome evil with good

        1 Reply Last reply
        0
        • T teejayem

          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

          T Offline
          T Offline
          teejayem
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • T teejayem

            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

            P Offline
            P Offline
            PauloCastilho
            wrote on last edited by
            #5

            Use asp.net repeater control.

            <asp:Repeater id="rPOSTS" runat="server">
            <ItemTemplate>
            </ItemTemplate>
            asp:Repeater

            On 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[^].

            T 1 Reply Last reply
            0
            • P PauloCastilho

              Use asp.net repeater control.

              <asp:Repeater id="rPOSTS" runat="server">
              <ItemTemplate>
              </ItemTemplate>
              asp:Repeater

              On 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[^].

              T Offline
              T Offline
              teejayem
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups