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();
}
}
我很欣賞的投入和幫助。
我建議你使用ASP.NET MVC而不是WebForms - 它更容易理解 - 反正WebForms被有效地棄用。 – Dai
我知道這是,但它並不是我的選擇。 – SlicedBread