Repeater的显示问题
我有两个表一个是USER 一个是MESS ,USER中实用户的信息 MESS是用户的留言信息表
MESS表中的字段 userid,title,content userid为user表中的相对值
USER表中的字段 userid,username userid为自增长字段
程序是这样的=======================================================
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = connclass.connection();
SqlCommand cmd = new SqlCommand( "select * from bookmess order by id desc ", conn);
showmess.DataSource = cmd.ExecuteReader();
showmess.DataBind();
}
前台是这样的 ======================================== ==============
<asp:Repeater ID= "showmess " runat= "server ">
<ItemTemplate>
//我想在这里把user表中的username显示出来该怎么写呢。也就是把发这条信息的用户名显示出来
标题: <%#DataBinder.Eval(Container.DataItem, "title ") %> <br>
内容: <%#DataBinder.Eval(Container.DataItem, "content ") %>
</ItemTemplate>
</asp:Repeater>
请问该如何写程序呢 谢谢了
[解决办法]
<asp:Repeater ID= "RptReport " DataSource= ' <%# ((DataRowView)Container.DataItem).Row.GetChildRows( "ReportRelation ") %> ' Runat= "server ">
[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = connclass.connection();
SqlCommand cmd = new SqlCommand( "select a.userid,b.username,title,
content from bookmess a join user b on a.userid=b.userid order by a.id desc ", conn);
showmess.DataSource = cmd.ExecuteReader();
showmess.DataBind();
}
前台是这样的 ======================================== ==============
<asp:Repeater ID= "showmess " runat= "server ">
<ItemTemplate>
用戶名: <%#DataBinder.Eval(Container.DataItem, "username ") %> <br>
标题: <%#DataBinder.Eval(Container.DataItem, "title ") %> <br>
内容: <%#DataBinder.Eval(Container.DataItem, "content ") %>
</ItemTemplate>
</asp:Repeater>
[解决办法]
用个连接查询就行了
SqlCommand cmd = new SqlCommand( "select * from bookmess order by id desc ", conn);
to
SqlCommand cmd = new SqlCommand( "select username ,title,content from user,miss where user.userid=miss.userid order by user.userid desc ", conn);
前台,
<asp:Repeater ID= "showmess " runat= "server ">
<ItemTemplate>
//我想在这里把user表中的username显示出来该怎么写呢。也就是把发这条信息的用户名显示出来
人名: <%#DataBinder.Eval(Container.DataItem, "username ") %> <br>
标题: <%#DataBinder.Eval(Container.DataItem, "title ") %> <br>
内容: <%#DataBinder.Eval(Container.DataItem, "content ") %>
</ItemTemplate>
</asp:Repeater>
[解决办法]
楼上提醒的对,前面加个
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = connclass.connection();
SqlCommand cmd = new SqlCommand( "select username ,title,content from user,miss where user.userid=miss.userid order by user.userid desc ", conn);
conn.open();
showmess.DataSource = cmd.ExecuteReader();
showmess.DataBind();
conn.close();
}