repeater数据不显示,求大神们帮一下
Default.aspx.cs:
if (Session["yhm"] ==null)
{
Response.Redirect("login.aspx");
}
List<News> result = new List<News>();
Userbll ub = new Userbll();
result = ub.GetTop15();
if (result.Count > 0)
{
Repeater1.DataSource = result;
Repeater1.DataBind();
}
Default.aspx:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<ul>
<li><a href='<%#Eval("NEWSID","shownews.aspx?id={0}") %>'><%# Eval("NEWSTITLE")%></a></li>
</ul>
</ItemTemplate>
</asp:Repeater>
Userbll.cs:
public List<News> GetTop15()
{
List<News> result = new List<News>();
conn = new SqlConnection(DBhelp.StrConn);
try
{
conn.Open();
DBnews nd = new DBnews(conn, tran);
result = nd.SelectTop15();
conn.Close();
}
catch (Exception ex)
{
result = new List<News>();
}
finally
{
if (tran != null)
{
tran.Dispose();
}
conn.Dispose();
}
return result;
}
DBnews.cs:
private string selectTop15News = @"select top 15 * from [news]";//数据库没有问题,我在数据库里可以查出15条记录
/// <summary>
/// 显示前15条记录
/// </summary>
/// <returns></returns>
public List<News> SelectTop15()
{
List<News> result = new List<News>();
SqlCommand comm = new SqlCommand();
SqlDataReader dr = null;
try
{
comm.CommandText =selectTop15News;
comm.Connection = conn;
comm.Transaction = tran;
dr = comm.ExecuteReader();
while (dr.Read())
{
News n = new News();
n = ReadData(dr);
result.Add(n);
}
}
catch (Exception e)
{
}
finally
{
if (dr != null)
{
dr.Dispose();
}
comm.Dispose();
}
return result;
}
//我断点看了,select出15条数据 就是在repeater不显示出来,title是空白 .net
[解决办法]
Repeater中试试不用Eval,用强制类型转换:
((News)Container.DataItem).NEWSTITLE)