2016-12-29 5 views
0

Unitime.aspx使用foreachループを動的に

<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要素を生成します。だから私はデータベースから関連するデータを取得し、このHTML要素に挿入して表示したいと考えています。しかし、今私がここで直面している問題は、私はレコードの1つしか表示できないということです。私がしたいのは、自分のデータベースにあるレコードの数に基づいてユーザーの投稿をループするためにforeachループを使用することです。とにかく、C#ファイルのforeachループにHTMLコードを追加して、目的の効果を得ることはできますか?ありがとうございました。 enter image description here

+0

[ListView](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.aspx)コントロールを参照してください。 –

答えて

0

これを行う方法はいくつかあります。基本的には、 "listview"のようなリピーターの中に "userPost" divをラップする必要があります。 "userPost"はリストビュー内のitemTemplateになります。 これを見てくださいenter link description here

関連する問題