ASP.NET 4 Unleased - p 67 pps

10 199 0
ASP.NET 4 Unleased - p 67 pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

ptg 634 CHAPTER 13 Using the Repeater and DataList Controls LISTING 13.5 EditRepeater.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> // The name of the primary key column string DataKeyName = “Id”; /// <summary> /// Stores the primary keys in ViewState /// </summary> Hashtable Keys { get { if (ViewState[“Keys”] == null) ViewState[“Keys”] = new Hashtable(); return (Hashtable)ViewState[“Keys”]; } } FIGURE 13.4 Editing database records with the Repeater control. From the Library of Wow! eBook ptg 635 Using the Repeater Control 13 /// <summary> /// Build the primary key collection /// </summary> protected void rptMovies_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Keys.Add(e.Item.ItemIndex, DataBinder.Eval(e.Item.DataItem, “Id”)); } } /// <summary> /// Clear the primary keys when Repeater is rebound /// to its data source /// </summary> protected void rptMovies_DataBinding(object sender, EventArgs e) { Keys.Clear(); } /// <summary> /// When you click the Update,Insert, or Delete /// button, this method executes /// </summary> protected void rptMovies_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case “Update”: UpdateMovie(e); break; case “Insert”: InsertMovie(e); break; case “Delete”: DeleteMovie(e); break; } } /// <summary> /// Update a movie record /// </summary> void UpdateMovie(RepeaterCommandEventArgs e) From the Library of Wow! eBook ptg 636 CHAPTER 13 Using the Repeater and DataList Controls { // Get the form fields TextBox txtTitle = (TextBox)e.Item.FindControl(“txtTitle”); TextBox txtDirector = (TextBox)e.Item.FindControl(“txtDirector”); CheckBox chkInTheaters = (CheckBox)e.Item.FindControl(“chkInTheaters”); // Set the DataSource parameters srcMovies.UpdateParameters[“Id”].DefaultValue = Keys[e.Item.ItemIndex].ToString(); srcMovies.UpdateParameters[“Title”].DefaultValue = txtTitle.Text; srcMovies.UpdateParameters[“Director”].DefaultValue = txtDirector.Text; srcMovies.UpdateParameters[“InTheaters”].DefaultValue = chkInTheaters.Checked.ToString(); // Fire the UpdateCommand srcMovies.Update(); } /// <summary> /// Insert a movie record /// </summary> void InsertMovie(RepeaterCommandEventArgs e) { // Get the form fields TextBox txtTitle = (TextBox)e.Item.FindControl(“txtTitle”); TextBox txtDirector = (TextBox)e.Item.FindControl(“txtDirector”); CheckBox chkInTheaters = (CheckBox)e.Item.FindControl(“chkInTheaters”); // Set the DataSource parameters srcMovies.InsertParameters[“Title”].DefaultValue = txtTitle.Text; srcMovies.InsertParameters[“Director”].DefaultValue = txtDirector.Text; srcMovies.InsertParameters[“InTheaters”].DefaultValue = chkInTheaters.Checked.ToString(); // Fire the InsertCommand srcMovies.Insert(); } /// <summary> /// Delete a movie record /// </summary> void DeleteMovie(RepeaterCommandEventArgs e) { // Set the DataSource parameters srcMovies.DeleteParameters[“Id”].DefaultValue = Keys[e.Item.ItemIndex].ToString(); From the Library of Wow! eBook ptg 637 Using the Repeater Control 13 // Fire the DeleteCommand srcMovies.Delete(); } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <style type=”text/css”> html { background-color:silver; } .content { width:600px; height:400px; padding:10px; border:solid 1px black; background-color:white; } .movies td { text-align:center; } a { color:blue; } </style> <title>Edit Repeater</title> </head> <body> <form id=”form1” runat=”server”> <div class=”content”> <asp:Repeater id=”rptMovies” DataSourceID=”srcMovies” Runat=”server” OnItemCommand=”rptMovies_ItemCommand” OnItemDataBound=”rptMovies_ItemDataBound” OnDataBinding=”rptMovies_DataBinding”> <HeaderTemplate> <table class=”movies”> <tr> <th>Title</th> <th>Director</th> From the Library of Wow! eBook ptg 638 CHAPTER 13 Using the Repeater and DataList Controls <th>In Theaters</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:TextBox id=”txtTitle” Text=’<%#Eval(“Title”)%>’ Runat=”server” /> </td> <td> <asp:TextBox id=”txtDirector” Text=’<%#Eval(“Director”)%>’ Runat=”server” /> </td> <td> <asp:CheckBox id=”chkInTheaters” Checked=’<%#Eval(“InTheaters”)%>’ Runat=”server” /> </td> <td> <asp:LinkButton id=”lnkUpdate” CommandName=”Update” Text=”Update” Runat=”server” /> &nbsp;|&nbsp; <asp:LinkButton id=”lnkDelete” CommandName=”Delete” Text=”Delete” OnClientClick=”return confirm(‘Are you sure?’);” Runat=”server” /> </td> </tr> </ItemTemplate> <FooterTemplate> <tr> <td> <asp:TextBox id=”txtTitle” Runat=”server” /> </td> From the Library of Wow! eBook ptg 639 Using the Repeater Control 13 <td> <asp:TextBox id=”txtDirector” Runat=”server” /> </td> <td> <asp:CheckBox id=”chkInTheaters” Runat=”server” /> </td> <td> <asp:LinkButton id=”lnkInsert” CommandName=”Insert” Text=”Insert” Runat=”server” /> </td> </tr> </table> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies” UpdateCommand=”UPDATE Movies SET Title=@Title, Director=@Director,InTheaters=@InTheaters WHERE Id=@Id” InsertCommand=”INSERT Movies (Title,Director,InTheaters) VALUES (@Title,@Director, DeleteCommand=”DELETE Movies WHERE Id=@Id” Runat=”server”> <UpdateParameters> <asp:Parameter Name=”Id” /> <asp:Parameter Name=”Title” /> <asp:Parameter Name=”Director” /> <asp:Parameter Name=”InTheaters” /> </UpdateParameters> <InsertParameters> <asp:Parameter Name=”Title” /> <asp:Parameter Name=”Director” /> <asp:Parameter Name=”InTheaters” /> </InsertParameters> <DeleteParameters> From the Library of Wow! eBook ptg 640 CHAPTER 13 Using the Repeater and DataList Controls <asp:Parameter Name=”Id” /> </DeleteParameters> </asp:SqlDataSource> </div> </form> </body> </html> In Listing 13.5, the ItemDataBound event handler builds a collection of primary keys from the data source. The collection of primary keys is stored in ViewState so that they will be available after a postback to the server. The DataBinding event handler clears the primary key collection when the Repeater is rebound to its data source (after a record is updated or deleted). If you don’t clear the collection, you get duplicates of the primary keys and an exception is raised. The ItemCommand event handler takes care of processing the button click events. When you click an Insert, Update, or Delete button, the event bubbles up and raises the ItemCommmand event. The ItemCommand event handler grabs the values from the form fields and calls the Insert(), Update(), or Delete() methods of the SqlDataSource control. Using the DataList Control The DataList control, like the Repeater control, is template driven. Unlike the Repeater control, by default, the DataList renders an HTML table. Because the DataList uses a particular layout to render its content, you are provided with more formatting options when using the DataList control. In this section, you learn how to use the DataList control to display data. You also learn how to render database records in both single-column and multicolumn HTML tables. We also explore how you can edit data with the DataList control. Displaying Data with the DataList Control To display data with the DataList control, you must supply the control with an ItemTemplate. The contents of the ItemTemplate are rendered for each data item from the data source. For example, the page in Listing 13.6 uses a DataList to display the contents of the Movies database table. The ItemTemplate displays the values of the Title, Director, and BoxOfficeTotals columns (see Figure 13.5). From the Library of Wow! eBook ptg 641 Using the DataList Control 13 LISTING 13.6 ShowDataList.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show DataList</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:DataList id=”dlstMovies” DataSourceID=”srcMovies” Runat=”server”> <ItemTemplate> <h1><%#Eval(“Title”)%></h1> Directed by: <%#Eval(“Director”) %> <br /> FIGURE 13.5 Displaying database records with the DataList control. From the Library of Wow! eBook ptg 642 CHAPTER 13 Using the Repeater and DataList Controls Box Office Totals: <%#Eval(“BoxOfficeTotals”,”{0:c}”) %> </ItemTemplate> </asp:DataList> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Title,Director,BoxOfficeTotals FROM Movies” Runat=”server” /> </div> </form> </body> </html> The DataList in Listing 13.6 renders an HTML table. Each data item is rendered into a separate table cell (<td> tag). The rendered output of the DataList control in Listing 13.6 looks like this: <table id=”dlstMovies” cellspacing=”0” border=”0” style=”border-collapse:collapse;”> <tr> <td> <h1>Titanic</h1> Directed by: James Cameron <br /> Box Office Totals: $600,000,000.00 </td> </tr> <tr> <td> <h1>Star Wars</h1> Directed by: George Lucas <br /> Box Office Totals: $500,000,000.00 </td> </tr> </table> From the Library of Wow! eBook ptg 643 Using the DataList Control 13 The default behavior of the DataList control is to render an HTML table. However, you can override this default behavior and display the contents of each data item in a separate HTML <span> tag. This approach is illustrated in Listing 13.7. LISTING 13.7 ShowFlowDataList.aspx <%@ Page Language=”C#” %> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show Flow DataList</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:DataList id=”dlstMovies” DataSourceID=”srcMovies” RepeatLayout=”Flow” Runat=”server”> <ItemTemplate> <%#Eval(“Title”)%> </ItemTemplate> </asp:DataList> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Title FROM Movies” Runat=”server” /> </div> </form> </body> </html> The DataList control in Listing 13.7 includes a RepeatLayout property that has the value Flow. Each movie title is rendered in a <span> tag followed by a line-break tag (<br>). The RepeatLayout property accepts one of the following two values: . Table—Data Items are rendered in HTML table cells. . Flow—Data Items are rendered in HTML <span> tags. From the Library of Wow! eBook . <UpdateParameters> < ;asp: Parameter Name=”Id” /> < ;asp: Parameter Name=”Title” /> < ;asp: Parameter Name=”Director” /> < ;asp: Parameter Name=”InTheaters” /> </UpdateParameters>. ptg 6 34 CHAPTER 13 Using the Repeater and DataList Controls LISTING 13.5 EditRepeater.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC -/ /W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script. <InsertParameters> < ;asp: Parameter Name=”Title” /> < ;asp: Parameter Name=”Director” /> < ;asp: Parameter Name=”InTheaters” /> </InsertParameters> <DeleteParameters>

Ngày đăng: 06/07/2014, 18:20

Tài liệu cùng người dùng

Tài liệu liên quan