Guten Tag. Ich habe dieses Inhaltsformular, aber ich weiß nicht, wie ich es automatisch generieren kann, abhängig von der Anzahl der Ergebnisse aus der Datenbank. Ich bin ziemlich neu in ASP.NetWie erstelle ich automatisch Inhalte basierend auf meiner Datenbank?
<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>
Hier ist, was ich damit füllen werde:
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();
}
}
Ich schätze den Eingang und die Hilfe.
Ich schlage vor, dass Sie ASP.NET MVC anstelle von WebForms verwenden - es ist viel einfacher zu verstehen - WebForms ist sowieso effektiv veraltet. – Dai
Ich weiß es ist aber es ist wirklich nicht meine Entscheidung zu treffen. – SlicedBread