xiaofeiliu

导航

 
上次项目中在首页中要求显示最新的六条公告,我采用了DataList来处理,但当数据少于六条时就显示不按常规来显示了,后经处理得以解决,现记录下来以供参考:
     
<td width="504" bgColor="#ffffff" height="167" valign="top"><asp:datalist id="DataList1" runat="server" Width="504px">
                                                    
<ItemTemplate>
                                                        
<FONT face="宋体">
                                                            
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="500" border="0">
                                                                
<TR>
                                                                    
<TD>
                                                                        
<asp:Image id="Image1" runat="server" Width="4px" height="4" ImageUrl="Image/index-icon1.gif"></asp:Image>&nbsp;
                                                                    
</TD>
                                                                    
<TD width="367" height="10"><FONT face="宋体"><A  title='<%# DataBinder.Eval(Container.DataItem,"Title")%>' href='javascript:viewNotice("Admin/Board_Popup.aspx?Seq=<%#DataBinder.Eval(Container.DataItem,"Seq")%>");'><font color="Black"><%# SubString(DataBinder.Eval(Container.DataItem,"Title").ToString(),28%></font></A></FONT></TD>
                                                                    
<TD><FONT face="宋体">&nbsp;&nbsp;&nbsp;</FONT>
                                                                        
<asp:Label id=Label1 runat="server" Width="100px" height="10" Text='<%# ChangeDate(DataBinder.Eval(Container.DataItem, "InputDate").ToString()) %>'>
                                                                        
</asp:Label></TD>
                                                                
</TR>
                                                            
</TABLE>
                                                        
</FONT>
                                                    
</ItemTemplate>
                                                
</asp:datalist></td>


在后台代码中添加:

  DataList1.Height=ds.Tables[0].Rows.Count*27;

public string SubString(string str,int i)
  
{  
   
string temp;
   
if(str.Length>i)
   
{
    temp
=str.Substring(0,i);
    
    
    temp
=temp+"";

    
return temp;
   }

   
else
    
return str;  
  }


public string ChangeDate(string date)
  {
      //string temp=Convert.ToDateTime(date).ToShortDateString();

   string temp=DateTime.Parse(date).ToString("yyyyMMdd");
   temp=temp.Substring(0,4)+"-"+temp.Substring(4,2)+"-"+temp.Substring(6,2);
   return temp;
  }


即设定DataList高度,这样就使得数据显示时有几条显示几条而且按固定高度显示。其他的数据绑定就和平常一样。

posted on 2006-02-06 11:30  小飞  阅读(957)  评论(0)    收藏  举报