[Asp.Net]Repeater嵌套使用及按钮事件处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/david_520042/article/details/6696908
 前台页面部分:

<asp:Repeater id="rptCategories" runat="server">
<HeaderTemplate>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
    <!--分类名称-->
    <tr><th><%# DataBinder.Eval(Container.DataItem, "TypeName") %></th></tr>
    <!--分类下的产品-->
    <asp:Repeater id="rptProduct" runat="server">
      <ItemTemplate>
        <tr><td><a href='ProductInfo.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "ID") %>'><%# DataBinder.Eval(Container.DataItem, "ProductName") %></a></td></tr>
      </ItemTemplate>
    </asp:Repeater>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>
</asp:Repeater>

后台代码部分(部分代码):

//在绑定分类品名时,绑定分类下的产品
private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    BLL.Products products =new BLL.Products();
    if (e.Item.ItemType == ListItemType.Item ||    e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rptProduct = (Repeater) e.Item.FindControl("rptProduct");
        //找到分类Repeater关联的数据项
        DataRowView rowv = (DataRowView)e.Item.DataItem;
        //提取分类ID
        int CategorieId = Convert.ToInt32(rowv["ID"]);
        //根据分类ID查询该分类下的产品,并绑定产品Repeater
        rptProduct.DataSource = products.GetProductsByCategorieId(CategorieId);
        rptProduct.DataBind();
    }
}

<asp:Repeater runat="server" ID="Repeater1">
             <ItemTemplate>
                           <tr>
                                     <td align="center">
                                            <%# DataBinder.Eval(Container.DataItem, "Ftitle")%>
                                        </td>

                                        <td align="center">
                                            <asp:LinkButton runat="server" ID="btnUpdate" Text="修改" CommandName='<%# DataBinder.Eval(Container.DataItem, "Fid")%>'
                                                OnCommand="btnUpdate_Click" />
                                            <asp:LinkButton runat="server" ID="btnDelete" Text="删除" CommandName='<%# DataBinder.Eval(Container.DataItem, "Fid")%>'
                                                OnCommand="btnDelete_Click" OnClientClick="return confirm('确认删除吗?');" />
                                    </tr>
                                </ItemTemplate>
                                <SeparatorTemplate>
                                    <%--这是分隔线模板--%>
                                    <tr>
                                        <td colspan="4">
                                            <hr style="border-top: 1pt;" />
                                        </td>
                                    </tr>
                                </SeparatorTemplate>
                                <FooterTemplate>
                                </FooterTemplate>
                            </asp:Repeater>

    //修改
    protected void btnUpdate_Click(object sender, CommandEventArgs e)
    {
       int id = Convert.ToInt32(e.CommandName);
       CWebCtl.alert(Page, id.ToString());
    }
    //删除
    protected void btnDelete_Click(object sender, CommandEventArgs e)
    {
        int Id = Convert.ToInt32(e.CommandName);
    
    }

没有更多推荐了,返回首页