.net之 repeater

一:概括
//在aspxl文件中代码:使用...将自动循环处理其绑定的数据源内的数据.
 <asp:Repeater id="RptAnswers" runat="server">
        <ItemTemplate>
<asp:Repeater id="RptAnswers" runat="server">
                <%# DataBinder.Eval(Container.DataItem, "newsTitle")%>
            </ItemTemplate>
</asp:Repeater>
代码
<!--国际.国内临床进展:开始-->
                        
<table cellSpacing="0" cellPadding="0" width="390" border="0">
                            
<asp:repeater id="clinicProgress" runat="server">
                                
<ItemTemplate>
                                    
<tr>
                                        
<td class="k004" height="28">
                                            
<table cellSpacing="0" cellPadding="0" width="390" border="0">
                                                
<tr>
                                                    
<td align="center" width="14" height="26"><span class="bl012">·</span></td>
                                                    
<td width="331" height="26"><span class="k012">
                                                            
<style="color:#515050" href='../news/news3.aspx?moduleId=500&NewsID=<%# DataBinder.Eval(Container.DataItem, "newsId") %>&title=<%# System.Web.HttpUtility.UrlEncode(((string)DataBinder.Eval(Container.DataItem, "newsTitle")))%>' title='<%# DataBinder.Eval(Container.DataItem, "newsTitle"%>'>
                                                                
<%# ((string)DataBinder.Eval(Container.DataItem, "newsTitle")).Substring(0, ((string)DataBinder.Eval(Container.DataItem, "newsTitle")).Length > 22 ? 22 : ((string)DataBinder.Eval(Container.DataItem, "newsTitle")).Length)%>
                                                            
</a>
                                                        
</span></td>
                                                    
<td width="45"><span class="k012">[<%# DataBinder.Eval(Container.DataItem, "inDate","{0:MM-dd}")%>]</span></td>
                                                
</tr>
                                            
</table>
                                        
</td>
                                    
</tr>
                                
</ItemTemplate>
                            
</asp:repeater></table>
                        
<!--国际.国内临床进展:结束-->
//在.aspx.cs源代码中要对其定义: 并对其进行数据源绑定

protected System.Web.UI.WebControls.Repeater RptAnswers;

DaoService subjectFac = new  DaoService();
DataTable    dt = subjectFac.GetList_ByPage("*",sqlselect,"inDate desc,newsID desc",1,1,15,out pagecount,out rowcount);
if(dt != null)
{
    RptAnswers.DataSource = dt;
    RptAnswers.DataBind();

代码
 protected System.Web.UI.WebControls.Repeater clinicProgress;

 
//国际.国内临床进展 500
private void BindData()
{
    dt4 
= mynews.GetList_ByPage("*","moduleCode=500 and isPass=1","inDate desc,newsId desc",1,1,9,out pagecount,out rowscount);
    clinicProgress.DataSource 
=dt4; // ds1.Tables[0].DefaultView;
    clinicProgress.DataBind();
}

二:使用方法(注意点)

(1) 数据的绑定用

<%# DataBinder.Eval(Container.DataItem, "NewsID") %> 

(2)只要放在合适的位置即可,(如作为超链接的参数传递)
<a href='ExpertDetails.aspx?NewsID=<%# DataBinder.Eval(Container.DataItem, "NewsID") %>'
(3)可进行数据格式转换(如日期格式转换)
<%# ((DateTime)DataBinder.Eval(Container.DataItem, "editDate")).ToString("MM-dd") %>
(4)可限定字符串输出长度(可使用C#内置函数)
<%# ((string)DataBinder.Eval(Container.DataItem, "NewsTitle")).Substring(0, ((string)DataBinder.Eval(Container.DataItem, "NewsTitle")).Length > 10 ? 10 : ((string)DataBinder.Eval(Container.DataItem, "NewsTitle")).Length)%>
(5)可限定循环逻辑(Container.ItemIndex表示当前为第几次循环)
<%# ((Container.ItemIndex+1) %3 == 0 )? "</tr><tr><td height='8' width='660'colspan='13'  bgcolor='#ffffff'></td></tr><tr>":"" %>

(6)页面间传递汉字时,注意要使用System.Web.HttpUtility.UrlEncode转换,放置乱码

href='../news/news3.aspx?moduleId=500&NewsID=<%# DataBinder.Eval(Container.DataItem, "newsId") %>&title=<%# System.Web.HttpUtility.UrlEncode(((string)DataBinder.Eval(Container.DataItem, "newsTitle")))%>' 

两个疑问点:(使用的是vs2003)

1.

 

 当代码中:<a></a>对应的href ="" 用双引号时,提示"未能在设计视图打开,在<% 值 %>快中,以不同方式将值括起来"...改为单引号则没有该提示

<a  href='recordDetailInfo.aspx?newsId=<%#  Int32.Parse(DataBinder.Eval(Container.DataItem, "newsId").ToString()) %> '> </a>

    有时还会有这样的提示.....

2.有时vs2003自动为html添加了一些代码,(如在外层又加了table)--刚没能把问题重现

 
原文地址:https://www.cnblogs.com/9421/p/1629218.html