如何根据我的数据库自动生成内容?

时间:2017-06-13 04:45:27

标签: asp.net webforms

美好的一天。我有这个内容表单,但我不知道如何根据数据库的结果数自动生成它。我是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();                
        }



        }

我很感激输入和帮助。

1 个答案:

答案 0 :(得分: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++;
        }