在使用aspx开发中,如果一个页面做纯数据展示,Repeater会比GridView更适合,因为它是轻量级的
下面是最基本的用法:
aspx:
<table>
<asp:Repeater ID="repeater1" runat="server" EnableViewState="false" OnItemCommand="repeater1_ItemCommand"> <HeaderTemplate> <tr><th>Id</th><th>Name</th><th>Pwd</th><th>Type</th><th>删除</th></tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Eval("Id")%></td> <td><%#Eval("Name")%></td> <td><%#Eval("Pwd")%></td> <td><%#Eval("Type")%></td> <td> <asp:Button ID="btnDelete" runat="server" Text="删除" CommandName="Delete" CommandArgument='<%#Eval("Id")%>'/> </td> </tr> </ItemTemplate> </asp:Repeater> </table>
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{ BindList(); }private void BindList()
{ ManagerInfoBLL bll = new ManagerInfoBLL(); repeater1.DataSource = bll.GetManagerInfoList(); repeater1.DataBind(); }protected void repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{ if (e.CommandName == "Delete") { int id = Convert.ToInt32(e.CommandArgument); //拿到前台传递过来的Id } }
1.ItemTemplate其实就是foreach遍历
2.在这里可以禁用ViewState,使页面性能更好