【问题标题】:use foreach loop to dynamically generate HTML elements based on number of records in database使用 foreach 循环根据数据库中的记录数动态生成 HTML 元素
【发布时间】:2016-12-29 19:52:47
【问题描述】:

Unitime.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="cph2" Runat="Server">
   <div id="userPost" style="width:560px">
     <div id="firstPart">
        <div class="firstLeft">
            <asp:Image ID="imgProfile" runat="server" Height="50px"         ImageUrl="~/Images/ProfilePic/andre.jpg" Width="50px" />
        </div>
        <div class="firstRight">
            <asp:ImageButton ID="imgbtnRemove" runat="server" Height="15px" Width="15px" ImageUrl="~/Images/Icons/wrong.png" />
        </div>
        <div class="firstCenter">
            <asp:Label ID="lblUsername" runat="server" Text="Label"></asp:Label>
            <br /><asp:Label ID="lblTime" runat="server" Text="Label"></asp:Label>
        </div>

    </div>

    <div id="secondPart">
        <asp:Label ID="lblDescription" runat="server" Text="Label"></asp:Label>
        </div>

    <div id="thirdPart">
        <asp:Image ID="imgPost" runat="server" Height="357px" Width="476px" />
        <br />
    </div>

    <div id="fourthPart">
        By:<asp:Label ID="lblSociety" runat="server" Text="Label"></asp:Label>
    </div>
</div>
</asp:Content>

unitime.aspx.cs

public partial class ActivityBoard : System.Web.UI.Page
{
    WooooDataContext db = new WooooDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            foreach(UniTime u in db.UniTimes)
            {
                lblSociety.Text = u.society;

                var time = u.timeCreated;
                lblTime.Text = time.ToString();
                lblTime.Text = timeAgo.relativeTime(u.timeCreated);

                var studentid = u.studentID;
                var un = from n in db.Students
                         where n.studentID == studentid
                         select new
                         {
                             n.username,
                             n.profilePic
                         };

                foreach (var x in un)
                {
                    string xx = x.username.ToString();
                    lblUsername.Text = xx;
                    string profile = x.profilePic.ToString();
                    imgProfile.ImageUrl = string.Concat("Images/ProfilePic/", profile);
                }


                imgPost.ImageUrl = string.Concat("Images/UniTime/", u.photoURL);
                lblDescription.Text = u.description;
            }
        }
    }

我正在构建类似于 facebook 模型的东西。所以我想从数据库中检索相关数据并插入到这个 HTML 元素中以显示它。但现在我在这里面临的问题是我只能显示其中一条记录。我想要做的是使用 foreach 循环根据我在数据库中的记录数循环出用户的帖子。无论如何我可以将 HTML 代码添加到 C# 文件中的 foreach 循环中以达到预期的效果吗?谢谢你。

【问题讨论】:

标签: c# html linq


【解决方案1】:

有几种方法可以做到这一点。基本上你需要将你的“userPost” div 包装在像“列表视图”这样的转发器中。 “userPost”将是您在列表视图中的 itemTemplate。 看看这个enter link description here

【讨论】:

    猜你喜欢
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多