注)下面的代码是页面的代码,只贴了repeater部分的代码,因为要绑定数据,如()所以在使用之前要先导入名空间
<asp:repeater id="repeater1" runat="server">
<headertemplate>
<table?border="0">
<tr?bgcolor="#999999">
<td>产品类型idtd>
<td>产品类型名称td><td>产品类型描述td>
<td> td>
<tr>
<headertemplate>
<itemtemplate>
<tr?bgcolor=#ffffff>
<td><img src=".iamgesattachtb.gif">
<td>
<td>
<td>
<td>td>
<td><asp:imagebutton id="show"imageurl=".\iamges\more.gif"
commandname="show" runat="server" />td>
<tr>
<itemtemplate>
<alternatingitemtemplate>
<tr bgcolor=#f5f2f7>
<td><img src=".iamgesattachtb.gif">
<td>
<td>td>
<td>td>
<td><asp:imagebutton id="imagebutton1" imageurl=".\iamge\smore.gif"
commandname="show" runat="server"/>
<td>
<tr>
alternatingitemtemplate>
<footertemplate>
<table>
<footertemplate>
asp:repeater>form>
<body>
<html>
后置代码部分:
先导入名空间:using system.data.sqlclient和using system.windows.forms;其余系统默认导入
下面是连接库的代码:
?
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对代码的支持有问题,所以被自动截取掉了)
最终效果如下:
