日期:2014-05-18  浏览次数:20506 次

我想问一个关于新闻显示的问题,希望大家能帮帮我,真的很重要,我快不行了
我用GridView显示了数据库中的news表,显示的是新闻标题,我帮新闻标题列转换成了模板列,加了个image控件,我想实现七天内的新闻image会显示图片,其他的不显示,请问该怎么显示呢?我一点都没有思路,下面是乱写的,不知道能不能有人帮忙该一下
  OleDbConnection   conn   =   new   OleDbConnection(@ "Provider=Microsoft.Jet.OLEDB.4.0;Data   Source= "   +   Server.MapPath( "db1.mdb "));
                string   sql   =   "select   *   from   news "   ;
                OleDbCommand   cmd   =   new   OleDbCommand(sql,   conn);
                conn.Open();
                OleDbDataReader   dr   =   cmd.ExecuteReader();
                if   (dr.Read())
                {
                        if   (Convert.ToDateTime(dr[ "newstime "].ToString()).AddDays(7)   >   DateTime.Now)
                        {
                                foreach   (GridViewRow   row   in   GridView1.Rows)
                                {
                                        Image   im   =   (Image)row.Cells[0].FindControl( "Image2 ");
                                        im.ImageUrl   =   "~/image/car.gif ";
                                }
                        }
                     
                }
                dr.Close();
                conn.Close();
结果果然不行,请问该怎么实现啊,谢谢你们啊

------解决方案--------------------
在绑定的时候写
<%#GetNewsImg(DataBinder.Eval(Container.DataItem, "newstime "))%>
在后台写一个方法

public string GetNewsImg(object newstime)
{
DateTime dt = DateTime.Parse(newstime.ToString());
if(dt.AddDays(7) > DateTime.Nos)
{
return " <img src= 'image/car.gif '/> ";
}else
{
return " ";
}
}

------解决方案--------------------
发问的哥哥,能帮小第个忙吗?
------解决方案--------------------
顶一下