后台的代码````
public void con_news()
{
Class1 con_yj = new Class1();
DataSet ds = new DataSet();
DataTable dt = new DataTable("testif");
//创建表列名
DataColumn dcdate = new DataColumn("date", typeof(System.String));
DataColumn dcpic = new DataColumn("pic", typeof(System.String));
DataColumn dctitle = new DataColumn("title", typeof(System.String));
DataColumn dcid = new DataColumn("id", typeof(System.String));
DataColumn dcauthor = new DataColumn("author", typeof(System.String));
//添加表列名
dt.Columns.Add(dcid);
dt.Columns.Add(dctitle);
dt.Columns.Add(dcpic);
dt.Columns.Add(dcdate);
dt.Columns.Add(dcauthor);
//添加内容
for (int i = 0; i < con_yj.con("select top 5 * from news_info").Tables[0].Rows.Count; i++)
{
DataRow row = dt.NewRow();
if (Convert.ToInt32(con_yj.con("select datediff(dd,new_date,getdate()) from news_info order by new_date desc").Tables[0].Rows[i][0].ToString()) <= 1)
{
row[2] = "img/news.jpg";
}
else
{
row[2] = "img/new.jpg";
}
row[0] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_number"].ToString();
if (con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_rubric"].ToString().Length > 25)
{
row[1] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_rubric"].ToString().Substring(0, 25) + "......";
}
else { row[1] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_rubric"].ToString(); }
row[3] = con_yj.con("select * from news_info order by new_date desc").Tables[0].Rows[i]["new_date"].ToString().Substring(0, 10);
dt.Rows.Add(row);
}
DataView dv = new DataView(dt);
//dv.Sort = "date desc";
dl1.DataSource = dv;
dl1.DataBind();
}
前台代码....
<table border="0" cellpadding="0" cellspacing="0" width="624px" bgcolor="#F2F2F2"><tr><td>
<asp:DataList ID="dl1" runat="server">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="622px" bgcolor="white">
<tr>
<td width="30px">●</td>
<td align="left" width="450px"><a href="newsitem.aspx?id=<%# DataBinder.Eval(Container.DataItem,"id") %>"><%# DataBinder.Eval(Container.DataItem,"title") %></a><img src="<%# DataBinder.Eval(Container.DataItem,"pic") %>" /></td>
<td align="left">[<%# DataBinder.Eval(Container.DataItem,"date") %>]</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td></tr></table>