2017-06-13 56 views
0

美好的一天。我有这种内容形式,但我不知道如何自动生成它取决于数据库结果的数量。我是相当新的ASP.Net如何根据我的数据库自动生成内容?

<asp:Content ID="Content1" ContentPlaceHolderID="AdminMain" runat="server"> 

<h1 class="container"><asp:Label runat="server" ID="PageTitle"></asp:Label></h1> 
<div class="container"> 
<hgroup class="title"> 
    <h1 class="post-title"> 
     <a href="/archive/post/@Model.Slug/"><asp:Label runat="server" ID="PostName"></asp:Label></a> 
    </h1> 
    <h6>Posted by <asp:Label runat="server" ID="AuthorName"></asp:Label> on <asp:Label runat="server" ID="PublushedDate"></asp:Label></h6> 


</hgroup> 
<div class="post-content"> 
    <asp:Label runat="server" ID="PostContent"></asp:Label> 
</div> 
<div class="post-tags"> 
    <h6>Categorized as:<a href="/archive/category/{CategoryUrlFriendlyName}/"><asp:Label runat="server" ID="PostCategory"></asp:Label></a><br /></h6> 
    <h6>Tagged as:<a href="/archive/tag/{TagUrlFriendlyName}/"><asp:Label runat="server" ID="PostTag"></asp:Label></a></h6> 


</div> 

这里就是我要去跟来填充它:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if(!IsPostBack) 
     { 
     GetAllPosts(); 
     } 


    } 

    private void GetAllPosts() 
    { 
     var sql = "SELECT p.*, t.Id as TagId, t.Name as TagName, " + 
     "t.UrlFriendlyName as TagUrlFriendlyName, u.Username FROM Posts p " + 
     "LEFT JOIN PostsTagsMap m ON p.Id = m.PostId " + 
     "LEFT JOIN Tags t ON t.Id = m.TagId " + 
     "INNER JOIN Users u ON u.Id = p.AuthorId"; 

     SqlConnection SQLConn = new SqlConnection(con); 
     SqlCommand SQLComm = new SqlCommand(sql,SQLConn); 
     SqlDataAdapter SQLAd = new SqlDataAdapter(SQLComm); 
     DataTable dt = new DataTable(); 
     SQLAd.Fill(dt); 



     foreach (DataRow row in dt.Rows) 
     {       
       PageTitle.Text = "All Posts"; 
       PostName.Text = row["Title"].ToString(); 
       AuthorName.Text = row["Username"].ToString(); 
       PublishedDate.Text = Convert.ToDateTime(row["DatePublished"]).ToString(); 
       PostContent.Text = row["Content"].ToString(); 
       PostCategory.Text = "Events"; 
       PostTag.Text = row["TagName"].ToString();     
     } 



     } 

我很欣赏的投入和帮助。

+0

我建议你使用ASP.NET MVC而不是WebForms - 它更容易理解 - 反正WebForms被有效地弃用。 – Dai

+0

我知道这是,但它并不是我的选择。 – SlicedBread

回答

0

Webforms是可怕的。

int i = 1; 

     PageTitle.Text = "All Posts"; 



      foreach (DataRow row in dt.Rows) 
     { 
      Label newlabel = new Label(); 
      PlaceHolder newplaceholder = new PlaceHolder(); 


      newlabel.ID ="PostName" + i.ToString() + "</a>"; 
      //html.Append(@"<hgroup class=""title"">"); 
      //html.Append(@"<h1 class=""post-title"">"); 
      PanelPosts.Controls.Add(newlabel); 
      newlabel.Text = @"<hgroup class=""title""><h1 class=""post-title""><a href=""#"">" + row["Title"].ToString() + "</a></h1>"; 

      newlabel = new Label(); 

      newlabel.ID = "AuthorName" + i.ToString(); 
      html.Append(@"<h6>"); 
      PanelPosts.Controls.Add(newlabel); 
      newlabel.Text ="Posted by: "+row["Username"].ToString()+" on "; 

      newlabel = new Label(); 

      newlabel.ID = "PublishedDate" + i.ToString(); 
      PanelPosts.Controls.Add(newlabel); 
      newlabel.Text = row["DatePublished"].ToString()+"</h6>"; 
      html.Append("</hgroup>"); 

      newlabel = new Label(); 

      newlabel.ID = "PostContent" + i.ToString(); 
      PanelPosts.Controls.Add(newlabel); 
      newlabel.Text = @"<div class=""post-content""><h3>" + row["Content"].ToString() + "</h3><br /></div>";    

      newlabel = new Label(); 

      newlabel.ID = "PostCategory" + i.ToString(); 
      PanelPosts.Controls.Add(newlabel); 
      newlabel.Text = @"<div class=""post-tags""><h6>Categorized as: <a href=""#"">" + "Category" + "</a> <br/> </h6>"; 


      newlabel = new Label(); 

      newlabel.ID = "PostTag" + i.ToString(); 
      PanelPosts.Controls.Add(newlabel); 
      newlabel.Text = @"<h6>Tagged as: <a href=""/archive/tag/{TagUrlFriendlyName}/"">"+row["TagName"].ToString()+"</div>"; 
      PanelPosts.Controls.Add(new LiteralControl("<br />"));   

      i++; 
     } 
+0

WebFormsy的做法越多,就是将数据绑定到服务器控件(如Repeater,ListView或GridView) - 但当然对生成的标记没有太多的控制权。 – kman

相关问题