temDataBound="dataSetCasting_ItemDataBound" id="dataSetCasting" Runat="server">
2: <HeaderTemplate>
3: <ul>
4: </HeaderTemplate>
5: <ItemTemplate>
6: <li><%# ((DataRowView)Container.DataItem)["Name"]%>
7: <ul>
8: <asp:Repeater ID="orders" Runat="server">
9: <ItemTemplate>
10: <li><%# ((DataRowView)Container.DataItem)["Amount"]%></li>
11: </ItemTemplate>
12: </asp:Repeater>
13: </ul>
14: </li>
15: </ItemTemplate>
16: <FooterTemplate>
17: </ul>
18: </FooterTemplate>
19: </asp:Repeater>Notice that our inner repeater doesn''''t have a DataSource property [8], however our outer repeater does specify an OnItemDataBound function [1], let''''s look at it:
1: protected void dataSetCasting_ItemDataBound(object s, RepeaterItemEventArgs e) {
2: if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
3: Repeater rpt = (Repeater)e.Item.FindControl("orders");
4: if (rpt != null){
5: rpt.DataSource = ((DataRowView)e.Item.DataItem).CreateChildView("CustomerOrders");
6: rpt.DataBind();
7: }
8: }
9: }Basically the same thing is happening as we saw before, except this is happening out of the UI.
Handling EventsThe last thing to discuss is how to handle events raised by controls inside your repeater/datalist/datagrid. Events raised from controls inside your repeater bubble up to the repeater and are exposed via the OnItemCommand event. LinkButtons and Buttons have a CommandArgument and CommandName property which lets the OnItemCommand handler figure out which button was clicked, for example:
1: <asp:Repeater OnItemCommand="eventRepeater_ItemCommand" id="eventRepeater" Runat="server">
2: <ItemTemplate>
3: <%# DataBinder.Eval(Container.DataItem, "Name")%>
4: <asp:LinkButton ID="delete"
5: Runat="server"
6: CommandName="Delete"
7: CommandArgument=''''<%# DataBinder.Eval(Container.DataItem, "CustomerId") %>''''>
8: Delete Customer
9: </asp:LinkButton>
10: -
11: <asp:LinkButton ID="addOrder"
12: Runat="server"
13: CommandName="Add"
14: CommandArgument=''''<%# DataBinder.Eval(Container.DataItem, "CustomerId") %>''''>
15: Add Order
16: </asp:LinkButton>
17: <br />
18: </ItemTemplate>
19: </asp:Repeater> In the above code, two linkbuttons can raise events, either deleting the customer [4-9] or adding an order [11-16]. Also note that the ItemCommand is hooked up [1]:
1: protected void eventRepeater_ItemCommand(object s, RepeaterCommandEventArgs e) {
2: int customerId = Convert.ToInt32(e.CommandArgument);
3: switch (e.CommandName.ToUpper()){
4: case "DELETE":
5: CustomerUtility.DeleteCustomer(customerId);
6: BindEventRepeater(false);
7: break;
8: case "Add":
9: //doesn''''t actually do antyhing right now.
10: break;
11: }
12: }Depending on what the commandName is [3] we know different actions were requested. Its important to note that if you change the underlaying datasource (like deleting a row) and want that to be visible to the user, you need to rebind your repeater/datalist/datagrid. Also note that if you are caching your data, like I am here, you''''ll need to invalidate the cache so that the new data source (with the delete/added/updated rows) is used.
DownloadThis sample web application simply contains a number of pages which do various things with repeaters. It should provide a playground for trying different things and simply messing around with databinding:
- C# Application
- VB.Net Application
上一页 [1] [2] [3] |