我正在上網聊天應用程序上完成我的學校任務。我希望使用數據列表在聊天框中顯示以前的聊天記錄,但數據列表不顯示。無法顯示C#中的datalist項目
這是我的.aspx代碼:
<asp:DataList ID="DataList3" runat="server">
<ItemTemplate runat="server">
<div class="lv-item media">
<div class="lv-avatar pull-left">
<asp:Image ID="Image4" runat="server" ImageUrl='<%# Bind("Image") %>' />
</div>
<div class="media-body">
<div class="ms-item">
<span class="glyphicon glyphicon-triangle-left" style="color: #000000;"></span>
<asp:Label ID="Message" runat="server" Text='<%# Bind("Message") %>'></asp:Label>
</div>
<small class="ms-date"><span class="glyphicon glyphicon-time"></span>
<asp:Label ID="Date" runat="server" Text='<%# Bind("Time") %>'></asp:Label></small>
</div>
</div>
</ItemTemplate>
</asp:DataList>
而且這是我的.cs代碼:
public void LoadChatbox()
{
Session["Name"] = "weiwei";
int waiwai = 1004;
//Request.QueryString["friendid"] = "waiwai";
Session["userid"] = 1005;
int userid = int.Parse(Session["userid"].ToString());
//int receiverid = int.Parse(Request.QueryString["friendid"]);
int receiverid = waiwai;
string name = Session["Name"].ToString();
DataSet ds = new DataSet();
ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
IEnumerable<DataRow> query =
(from user in db.Users.AsEnumerable()
join ch in db.ChatHistories.AsEnumerable()
on user.Userid equals ch.Senderid
where ch.Senderid == userid && ch.Receiverid == receiverid || ch.Receiverid == userid && ch.Senderid == receiverid orderby ch.id
select new
{
Image = user.image,
Message = ch.message,
Time = ch.dateTime
})
as IEnumerable<DataRow>;
DataTable chatbox = query.CopyToDataTable<DataRow>();
ds.Tables.Add(chatbox);
DataList3.DataSource = ds;
DataList3.DataBind();
}
剛剛發現我的查詢變量爲空,但我該怎麼做才能將linq查詢插入到DataTable中? –
正如我所說的,項目到數據行列表或從匿名集合向數據表中添加行。還有關於如何做到這一點的其他問題和例子。 –