如何使用Repeater控件的模板


显示结果

页眉模板--人员信息

<>Ms. Davolio Nancy
Dr. Fuller Andrew
Ms. Leverling Janet
Mrs. Peacock Margaret
Mr. Buchanan Steven
Mr. Suyama Michael
Mr. King Robert
Ms. Callahan Laura
Ms. Dodsworth Anne

页脚模板--显示结束



源代码

     private   void  Page_Load( object  sender, System.EventArgs e)
        
{
            
//页面初试化时进行数据绑定
            if(!IsPostBack)
                RepeaterDataBind();
        }


        
private   void  RepeaterDataBind()
        
{
            
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
            
//创建数据适配器对象
            SqlDataAdapter da = new SqlDataAdapter("select LastName,FirstName,TitleOfCourtesy from Employees",conn);
            
//创建DataSet对象
            DataSet ds = new DataSet();
            
try
            
{
                
//填充数据集
                da.Fill(ds,"testTable");
                
//进行数据绑定
                RepeaterTem.DataSource = ds.Tables["testTable"];
                RepeaterTem.DataBind();
            }

            
catch(Exception error)
            
{
                Response.Write(error.ToString());
            }
        
        }


页面代码 < asp:Repeater id = " RepeaterTem "  runat = " server " >
                
< HeaderTemplate >
                    
< h3 > 页眉模板--人员信息 </ h3 >
                    
< table >
                
</ HeaderTemplate >
                
< ItemTemplate >
                    
< font color = " blue " >
                        
<% #DataBinder.Eval(Container.DataItem, " TitleOfCourtesy " ) %>
                        
<% #DataBinder.Eval(Container.DataItem, " LastName " ) %>
                        
<% #DataBinder.Eval(Container.DataItem, " FirstName " ) %>
                    
</ font >
                
</ ItemTemplate >
                
< AlternatingItemTemplate >
                    
< font color = " red " >
                        
<% #DataBinder.Eval(Container.DataItem, " TitleOfCourtesy " ) %>
                        
<% #DataBinder.Eval(Container.DataItem, " LastName " ) %>
                        
<% #DataBinder.Eval(Container.DataItem, " FirstName " ) %>
                    
</ font >
                
</ AlternatingItemTemplate >
                
< SeparatorTemplate >
                    
< hr color = " #green "  width = " 100% " >
                
</ SeparatorTemplate >
                
< FooterTemplate >
                    
</ TABLE >
                    
< h3 > 页脚模板--显示结束 </ h3 >
                
</ FooterTemplate >
            
</ asp:Repeater >


你可能感兴趣的:(模板)