使用Repeater模板一

(注)下面的代码是页面的代码,因为要绑定数据,如(<%# DataBinder.Eval

(Container.DataItem,"CategoryName") %>)所以在使用之前要先导入名空间<%@ Import Namespace="System.Data" %>

后置代码部分:
先导入名空间:using System.Data.SqlClient和using System.Windows.Forms;其余系统默认导入

下面是连接库的代码:

<% @ Import Namespace = " System.Data "   %>
<% @ Page language = " c# "  Codebehind = " WebForm1.aspx.cs "  AutoEventWireup = " false "  Inherits = " Test1.WebForm1 "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN "   >
< HTML >
    
< HEAD >
        
< title > WebForm1 </ title >
        
< meta content = " True "  name = " vs_snapToGrid " >
        
< meta content = " True "  name = " vs_showGrid " >
        
< meta content = " Microsoft Visual Studio .NET 7.1 "  name = " GENERATOR " >
        
< meta content = " C# "  name = " CODE_LANGUAGE " >
        
< meta content = " JavaScript "  name = " vs_defaultClientScript " >
        
< meta content = " http://schemas.microsoft.com/intellisense/ie5 "  name = " vs_targetSchema " >
    
</ HEAD >
    
< body MS_POSITIONING = " GridLayout " >
        
< form id = " Form1 "  method = " post "  runat = " server " >
            
< FONT face = " 宋体 " >
                
< asp:button id = " Button1 "  style = " Z-INDEX: 102; LEFT: 472px; POSITION: absolute; TOP: 472px "  runat = " server "
                    Width
= " 96px "  Height = " 32px "  Text = " Button " ></ asp:button ></ FONT >< asp:repeater id = " Repeater1 "  runat = " server " >
                
< HeaderTemplate >
                    
< table border = " 0 " >
                        
< tr bgcolor = " #999999 " >
                            
< td >
                                产品类型ID
                            
</ td >
                            
< td >
                                产品类型名称
                            
</ td >
                            
< td >
                                产品类型描述
                            
</ td >
                            
< td >& nbsp; </ td >
                        
</ tr >
                
</ HeaderTemplate >
                
< ItemTemplate >
                    
< tr bgcolor = " #ffffff " >
                        
< td >
                            
< img src = " .iamgesattachtb.gif " >
                            
<% # DataBinder.Eval(Container.DataItem, " CategoryID " %>
                        
</ td >
                        
< td >
                            
<% # DataBinder.Eval(Container.DataItem, " CategoryName " %>
                        
</ td >
                        
< td >
                            
<% # DataBinder.Eval(Container.DataItem, " Description " %>
                        
</ td >
                        
< td >
                            
< asp:ImageButton ID = " show "  ImageUrl = " .iamgesmore.gif "  Text = " 查看 "  CommandName = " show "  Runat = " server "   />
                        
</ td >
                    
</ tr >
                
</ ItemTemplate >
                
< AlternatingItemTemplate >
                    
< tr bgcolor = " #f5f2f7 " >
                        
< td >
                            
< img src = " .iamgesattachtb.gif " >
                            
<% # DataBinder.Eval(Container.DataItem, " CategoryID " %>
                        
</ td >
                        
< td >
                            
<% # DataBinder.Eval(Container.DataItem, " CategoryName " %>
                        
</ td >
                        
< td >
                            
<% # DataBinder.Eval(Container.DataItem, " Description " %>
                        
</ td >
                        
< td >
                            
< asp:ImageButton ID = " Imagebutton1 "  ImageUrl = " .iamgesmore.gif "  Text = " 查看 "  CommandName = " show "  Runat = " server "   />
                        
</ td >
                    
</ tr >
                
</ AlternatingItemTemplate >
                
< FooterTemplate >
                    
</ table >
                
</ FooterTemplate >
            
</ asp:repeater ></ form >
    
</ body >
</ HTML >

 

private   void ? ConnecDB()
{
SqlConnection con
=new SqlConnection("server=localhost;database=Northwind;Trusted_Connection=Yes;");
SqlDataAdapter da
=new SqlDataAdapter("select*from Categories",con);
DataSet ds
=new DataSet();
ad.Fill(ds,
"Categories");
this.Repeater1.DataSource=ds;
this.Repeater1.DataBind();//绑定到Repeater控件
}


下面这段是Repeater模板中的事件

private   void  Repeater1_ItemCommand( object  sender,System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if(e.CommandName=="show")
{
MessageBox.Show(
"you?click"+e.Item.ItemIndex.ToString()+"row","a

show
",MessageBoxButtons.OK,MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
}

else
{
MessageBox.Show(
"you?click?"+e.Item.ItemIndex.ToString()+"
row","altshow",MessageBoxButtons.OK,MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
}

}

(注:1、必须在Repeater模板事件中绑定上面方法到其ItemCommand事件,在事件窗口中可以做到2、在上面的图片URL应是 ImageUrl="./iamges/more.gif",由于BLOG对代码的支持有问题,所以被自动截取掉了)

最终效果如下:Repeater配图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值