这是一个初学者的问题。我想从数据库(MSSQL)动态创建div。例如,我想在条目下方显示评论。 Comment表通过EntryID与Entry表连接。我的aspx代码如下:
<div class="commentBody" runat="server">
<asp:Label ID="commentSender" runat="server" Text=""></asp:Label>
<asp:Label ID="commentDate" runat="server" Text=""></asp:Label>
<asp:Label ID="commentText" runat="server" Text=""></asp:Label>
</div>
对于所有评论,将重复此操作。我正在隐藏所有代码(无评估)。我的C#代码:
protected void YorumlariGetir()
{
string selectComments = "SELECT * FROM Comment WHERE Comment.EntryID = @EntryID";
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(selectComments, conn);
cmd.Parameters.AddWithValue("@EntryID", Session["EntryID"].ToString());
try
{
conn.Open();
// HERE I WANT TO CALL A LOOP FOR COMMENTS
}
catch (Exception ex)
{
Response.Write("Hata: " + ex.Message);
}
finally
{
conn.Close();
}
}
我可以使用转发器或foreach循环。但是我不知道怎么做,在这一点上需要一个例子。
感谢帮助。
最佳答案
编辑:答案完全修订。
在您的问题中,您需要一种循环方法并在每次循环迭代中添加注释。您可以执行此操作,但是使用内置的ASP.NET控件有更好的方法。首先,我将向您展示一个基本示例,该示例简单地迭代SqlDataReader对象并手动创建HTML。然后,我将展示一个更好的解决方案。如果您能够实现第二个选项,则不建议使用第一个选项。
在这两种解决方案上,我强烈建议您在选择查询中专门为您的字段命名,而不要使用星号选择所有字段。如果表结构发生更改,使用SELECT *
可能会导致问题。另外,您可能正在选择不需要的数据列,这会浪费资源。
首先,这是使用SqlDataReader类的非常简单的示例。这可以工作,但是请记住有更好的方法。
try
{
conn.Open();
// HERE I WANT TO CALL A LOOP FOR COMMENTS
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// create the div to wrap around the comment
HtmlGenericControl div = new HtmlGenericControl("div");
div.Attributes.Add("style", "commentBody");
// create three labels and add them to the div
// 1,2,3 are the ordinal positions of the column names, this may need corrected since I have no idea what your table looks like.
div.Controls.Add(new Label() { Text = reader.GetString(1) });
div.Controls.Add(new Label() { Text = reader.GetString(2) });
div.Controls.Add(new Label() { Text = reader.GetString(3) });
// add the div to the page somehow, these can be added to any HTML control that can act as a container. I would suggest a plain old div.
MyMainDiv.Controls.Add(div);
}
}
现在,上述方法可以使用,但是它是处理显示数据的笨拙,老式的方法。现代.NET应用程序应在可用的地方使用更好的解决方案。更好的解决方案是使用Data Binding。互联网上有很多关于此的文章和教程,因此,如果这是一个新主意,则可以做一些教程来学习数据绑定的精髓。
若要使用Repeater类,请首先将Repeater控件添加到ASPX页面:
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<div class="commentBody">
<span class="commentSender"><%# DataBinder.Eval(Container.DataItem,"aucommentSenderid") %></span>
<span class="commentDate"><%# DataBinder.Eval(Container.DataItem,"aucommentDateid") %></span>
<span class="commentText"><%# DataBinder.Eval(Container.DataItem,"aucommentTextid") %></span>
</div>
</ItemTemplate>
</asp:Repeater>
接下来,在代码后面添加一些代码以创建数据源,并将其附加到Repeater控件:
SqlDataAdapter da = new SqlDataAdapter(cmd); // use your existing SqlCommand here (don't use select *)
DataSet ds = new DataSet(); // create a DataSet object to hold you table(s)... this can contain more than 1 table
da.Fill(ds, "Comment"); // fill this dataset with everything from the Comment Table
Repeater1.DataSource = ds.Tables["Comment"]; // attach the data table to the control
Repeater1.DataBind(); // This causes the HTML to be automatically rendered when the page loads.