Repeater控件使用小结持续更新

Repeater嵌套Repeater绑定数据

 

前台代码

 

 1 <!--注意层级关系不要写错了-->
 2 <asp:Repeater ID="rpGroup" runat="server" 
 3         onitemdatabound="rpGroup_ItemDataBound">
 4         <HeaderTemplate>
 5             <table width="100%" border="0" cellspacing="0" cellpadding="0">
 6         </HeaderTemplate>
 7         <ItemTemplate>
 8             <tr>
 9                 <th>
10 11                     <%#(int)Eval("groupNo")+1%>
12                     批次
13                 </th>
14                 <th>金额:<label id="lblSumMoney" runat="server" ><%#Eval("SaleOrderMoneySum")%></label></th>
15                 <th>单数:<label id="lblSumSaleOrder" runat="server" ><%#Eval("SaleOrderDzSum")%></label></th>
16             </tr>
17             <tr>
18                 <th style="width: 8%">
19                     销售单号
20                 </th>
21                 <th style="width: 13%">
22                     客户名称
23                 </th>
24                 
25             </tr>
26             <asp:Repeater ID="rpSaleOrderDz" runat="server">
27                 <ItemTemplate>
28                     <tr>
29                         <td align="center">
30                             <%#Eval("saleOrderCode")%>
31                         </td>
32                         <td align="center">
33                             <%#Eval("sellerName")%>
34                         </td>
35                         
36                     </tr>
37                 </ItemTemplate>
38             </asp:Repeater>
39         </ItemTemplate>
40         <FooterTemplate>
41             </table>
42         </FooterTemplate>
43     </asp:Repeater>

 

在PageLoad事件中,加载外层Repeter数据

1 protected void Page_Load(object sender, EventArgs e)
2 {
3     var datas = JsonConvert.DeserializeObject<List<ResponseViewSaleOrderDzLstModel>>(strJson);
4                 rpGroup.DataSource = datas;
5                 rpGroup.DataBind();
6 }

 

 

最后一步根据外层绑定的数据找到对应的内层数据

后台代码:这块需要绑定外层Repeater的ItemDataBound事件

 1 //网上资料用到了DataRowView我这里没用到直接绑定了数据集
 2 protected void rpGroup_ItemDataBound(object sender, RepeaterItemEventArgs e)
 3     {
 4         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
 5         {
 6             Repeater rep = e.Item.FindControl("rpSaleOrderDz") as Repeater;//找到里层的repeater对象
 7             //DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项 
 8             //int groupNo = Convert.ToInt32(rowv["qtid"]); //获取组号
 9             rep.DataSource = datas[e.Item.ItemIndex].datas;//datas[groupNo].datas;
10             rep.DataBind();
11         }
12     }

 

 

 

Repeater获取HeaderTemplate、FooterTemplate中的控件

Repeater中FooterTemplate并无FindControl方法,也无法将FooterTemplate转成RepeaterItem,

虽然在Repeater的ItemDataBound事件中,RepeaterItemEventArgs参数用if (e.Item.ItemType == ListItemType.Footer)可判别出这个Footer,但其他时候不行。

可通过如下代码进行文本的赋值:

<label ID="rep2Sum" runat="server" />

((HtmlGenericControl)rep2.Controls[rep2.Controls.Count - 1].FindControl("rep2Sum")).Text = ss.ToString(); 

同理HeaderTemplate:

((HtmlGenericControl)rep2.Controls[0].FindControl("rep2Sum")).Text = ss.ToString(); 

 

Repeater实现行上下移动

前端

<asp:Repeater ID="rpNews" runat="server" OnItemCommand="rpNews_ItemCommand" OnItemDataBound="rpNews_ItemDataBound">
                                    <ItemTemplate>
                                        <tr>
                                            <td><%#Eval("menu_name") %></td>
                                            <td><%#Eval("menu_url") %>
                                                <asp:HiddenField ID="zindex" runat="server" Value=<%# Container.ItemIndex%> />
                                            </td>
                                            <td class="gradeX">
                                                <a  class="btn btn-primary" href="UpDateMenu.aspx?action=update&oid=<%# Eval("OID") %>&menuName=<%# Eval("menu_name") %>">修改</a>
                                                <a ID="btnDelete" href="MenuHandler.ashx?action=delete&oid=<%# Eval("OID") %>" OnClick="return IsConfirmDelete();" class="btn btn-primary">删除</a>
                                                <asp:LinkButton runat="server" ID="linkUp" CommandArgument='<%# Eval("OID") %>' CommandName='<%# Eval("menuorder") %>' OnClick="lbUp_Click"  Text="上移" CssClass="btn btn-primary"></asp:LinkButton> 
                                                <asp:LinkButton runat="server" ID="linkDown" CommandArgument='<%# Eval("OID") %>' Text="下移" CommandName='<%# Eval("menuorder") %>' OnClick="lbDown_Click" CssClass="btn btn-primary"></asp:LinkButton>
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                </asp:Repeater>

后端:

     public int datacount = 0;
        protected void rpNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton linkUp = e.Item.FindControl("linkUp") as LinkButton;
                LinkButton linkDown = e.Item.FindControl("linkDown") as LinkButton;
                if (e.Item.ItemIndex == 0)
                {
                    linkUp.Enabled = false;
                }
                if (e.Item.ItemIndex == datacount - 1)
                {
                    linkDown.Enabled = false;
                }
            }
        }
protected void lbUp_Click(object sender, EventArgs e)
        {
            MenuUCService uc = new MenuUCService();
            LinkButton btn = (LinkButton)sender;
            HiddenField hf = (HiddenField)btn.NamingContainer.FindControl("zindex");
        //先修改自己在修改其他 StringBuilder strSql
= new StringBuilder(); //拿到当前行的上一行的Link数据 LinkButton linkCurrentRow = (LinkButton)rpNews.Items[int.Parse(hf.Value)].FindControl("linkUp"); if (!uc.CheckCurrentRowValueRange(linkCurrentRow)) { return; } LinkButton linkUpRow = (LinkButton)rpNews.Items[int.Parse(hf.Value) - 1].FindControl("linkUp"); //得到的menuorder-oid strSql.Append("update USR_ALLMENU set menuorder='" + linkUpRow.CommandName + "' where OID='" + linkCurrentRow.CommandArgument + "';"); strSql.Append("update USR_ALLMENU set menuorder='" + linkCurrentRow.CommandName + "' where OID='" + linkUpRow.CommandArgument + "';");        //重新绑定数据 }      protected void lbDown_Click(object sender, EventArgs e) { MenuUCService uc = new MenuUCService(); LinkButton btn = (LinkButton)sender; HiddenField hf = (HiddenField)btn.NamingContainer.FindControl("zindex"); LinkButton linkCurrentRow = (LinkButton)rpNews.Items[int.Parse(hf.Value)].FindControl("linkDown"); if (!uc.CheckCurrentRowValueRange(linkCurrentRow)) { return; } LinkButton linkDown = (LinkButton)rpNews.Items[int.Parse(hf.Value) + 1].FindControl("linkDown"); StringBuilder strSql = new StringBuilder(); strSql.Append("update USR_ALLMENU set menuorder='" + linkDown.CommandName + "' where OID='" + linkCurrentRow.CommandArgument + "';"); strSql.Append("update USR_ALLMENU set menuorder='" + linkCurrentRow.CommandName + "' where OID='" + linkDown.CommandArgument + "';");         //重新绑定数据 }

原理:找到相邻行,然后获取ID和排序方式,然后修改

参考自:http://www.cnblogs.com/webapi/p/5718445.html

posted @ 2017-04-05 15:26  大胖儿在努力  阅读(285)  评论(0编辑  收藏  举报