1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 4 Unleased - p 66 pptx

10 244 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 548,36 KB

Nội dung

ptg 614 CHAPTER 12 Using the DetailsView and FormView Controls id=”txtTitle” Text=’<%# Bind(“Title”) %>’ Runat=”server” /> <br /><br /> <asp:Label id=”lblDirector” Text=”Movie Director:” AssociatedControlID=”txtDirector” Runat=”server” /> <br /> <asp:TextBox id=”txtDirector” Text=’<%# Bind(“Director”) %>’ Runat=”server” /> <br /><br /> <asp:Label id=”lblBoxOfficeTotals” Text=”Box Office Totals:” AssociatedControlID=”txtBoxOfficeTotals” Runat=”server” /> <br /> <asp:TextBox id=”txtBoxOfficeTotals” Text=’<%# Bind(“BoxOfficeTotals”) %>’ Runat=”server” /> <br /><br /> <asp:LinkButton id=”lnkUpdate” Text=”Update Movie” CommandName=”Update” Runat=”server” /> | <asp:LinkButton id=”lnkCancel” Text=”Cancel Update” CommandName=”Cancel” Runat=”server” /> </EditItemTemplate> </asp:FormView> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,BoxOfficeTotals FROM Movies” From the Library of Wow! eBook ptg 615 Using the FormView Control 12 UpdateCommand=”UPDATE Movies SET Title=@Title, Director=@Director,BoxOfficeTotals=@BoxOfficeTotals WHERE Id=@Id” Runat=”server” /> </div> </form> </body> </html> You should notice several things about the FormView control in Listing 12.22. First, the FormView control includes a DataKeyNames property that contains the name of the primary key from the data source. You need to specify a primary key when editing records. Next, the FormView control’s ItemTemplate includes a LinkButton that looks like this: <asp:LinkButton id=”lnkEdit” Text=”Edit Movie” CommandName=”Edit” Runat=”server” /> This LinkButton includes a CommandName property with the value Edit. Clicking the link switches the FormView control into Edit mode. You could use any other control here that supports the CommandName property such as a Button or ImageButton control. Next, the FormView control includes an EditItemTemplate. This template contains the form for editing the record. Each form field uses a two-way databinding expression. For example, the form field for editing the movie title looks like this: <asp:TextBox id=”txtTitle” Text=’<%# Bind(“Title”) %>’ Runat=”server” /> The Bind(“Title”) method binds the Title column to the Text property of the TextBox control. Finally, the EditItemTemplate includes both a LinkButton for updating the database record and a LinkButton for canceling the update. The LinkButton for updating the record looks like this: <asp:LinkButton id=”lnkUpdate” Text=”Update Movie” CommandName=”Update” Runat=”server” /> From the Library of Wow! eBook ptg 616 CHAPTER 12 Using the DetailsView and FormView Controls This LinkButton includes a CommandName property, which has the value Update. When you click this LinkButton, the SQL statement represented by the SqlDataSource control’s UpdateCommand is executed. NOTE If you want the FormView control to be in Edit mode by default, you can assign the value Edit to the FormView control’s DefaultMode property. Inserting Data with the FormView Control You can use the FormView control to insert new records into a database table. For example, the page in Listing 12.23 enables you to insert a new movie record into the Movies data- base table. LISTING 12.23 ShowFormViewInserting.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”> <style type=”text/css”> html { background-color:silver; } #content { margins:auto; width:600px; padding:10px; background-color:white; font:14px Georgia,Serif; } a { color:blue; } </style> <title>Show FormView Inserting</title> </head> <body> <form id=”form1” runat=”server”> <div id=”content”> From the Library of Wow! eBook ptg 617 Using the FormView Control 12 <asp:FormView id=”frmMovies” DataSourceID=”srcMovies” AllowPaging=”true” Runat=”server”> <ItemTemplate> <h1><%# Eval(“Title”) %></h1> <b>Directed By:</b> <%# Eval(“Director”) %> <br /> <b>In Theaters:</b> <%#Eval(“InTheaters”) %> <hr /> <asp:LinkButton id=”lnkNew” Text=”New Movie” CommandName=”New” Runat=”server” /> </ItemTemplate> <InsertItemTemplate> <asp:Label id=”lblTitle” Text=”Movie Title:” AssociatedControlID=”txtTitle” Runat=”server” /> <br /> <asp:TextBox id=”txtTitle” Text=’<%# Bind(“Title”) %>’ Runat=”server” /> <br /><br /> <asp:Label id=”lblDirector” Text=”Movie Director:” AssociatedControlID=”txtDirector” Runat=”server” /> <br /> <asp:TextBox id=”txtDirector” Text=’<%# Bind(“Director”) %>’ Runat=”server” /> <br /><br /> <asp:CheckBox id=”chkInTheaters” Text=”In Theaters” Checked=’<%# Bind(“InTheaters”) %>’ From the Library of Wow! eBook ptg 618 CHAPTER 12 Using the DetailsView and FormView Controls Runat=”server” /> <br /><br /> <asp:LinkButton id=”lnkInsert” Text=”Insert Movie” CommandName=”Insert” Runat=”server” /> | <asp:LinkButton id=”lnkCancel” Text=”Cancel Insert” CommandName=”Cancel” Runat=”server” /> </InsertItemTemplate> </asp:FormView> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies” InsertCommand=”INSERT Movies (Title,Director,InTheaters) VALUES (@Title,@Director, Runat=”server” /> </div> </form> </body> </html> You should notice several things about the page in Listing 12.23. First, ItemTemplate includes a LinkButton control that looks like this: <asp:LinkButton id=”lnkNew” Text=”New Movie” CommandName=”New” Runat=”server” /> From the Library of Wow! eBook ptg 619 Using the FormView Control 12 When you click this LinkButton control, the FormView switches into Insert mode and displays the contents of the InsertTemplate. The CommandName property has the value New. The FormView control includes InsertItemTemplate that contains the form for inserting a new movie record. Each form field uses a two-way databinding expression. For example, the InTheaters CheckBox looks like this: <asp:CheckBox id=”chkInTheaters” Text=”In Theaters” Checked=’<%# Bind(“InTheaters”) %>’ Runat=”server” /> The Bind(“InTheaters”) method binds the value of the CheckBox control’s Checked prop- erty to the InTheaters database column. The InsertItemTemplate contains a LinkButton for inserting the record and a LinkButton for canceling the insert operation. The LinkButton for inserting a record looks like this: <asp:LinkButton id=”lnkInsert” Text=”Insert Movie” CommandName=”Insert” Runat=”server” /> This LinkButton control includes a CommandName property that has the value Insert. When you click the LinkButton, the SQL command represented by the SqlDataSource control’s InsertCommand is executed. NOTE You c an place the FormView control into Insert mode by default by assigning the value Insert to the control’s DefaultMode property. Deleting Data with the FormView Control You can use the FormView control to delete database records. For example, the page in Listing 12.24 enables you to delete records from the Movies database table (see Figure 12.16). From the Library of Wow! eBook ptg 620 CHAPTER 12 Using the DetailsView and FormView Controls FIGURE 12.16 Deleting a record with the FormView control. LISTING 12.24 ShowFormViewDeleting.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”> <style type=”text/css”> html { background-color:silver; } #content { margins:auto; width:600px; padding:10px; background-color:white; font:14px Georgia,Serif; } a { From the Library of Wow! eBook ptg 621 Using the FormView Control 12 color:blue; } </style> <title>Show FormView Deleting</title> </head> <body> <form id=”form1” runat=”server”> <div id=”content”> <asp:FormView id=”frmMovies” DataSourceID=”srcMovies” DataKeyNames=”Id” AllowPaging=”true” Runat=”server”> <ItemTemplate> <h1><%# Eval(“Title”) %></h1> <b>Directed By:</b> <%# Eval(“Director”) %> <br /> <b>In Theaters:</b> <%#Eval(“InTheaters”) %> <hr /> <asp:LinkButton id=”lnkDelete” Text=”Delete Movie” CommandName=”Delete” OnClientClick=”return confirm(‘Are you sure?’);” Runat=”server” /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies” DeleteCommand=”DELETE Movies WHERE Id=@Id” Runat=”server” /> </div> </form> </body> </html> From the Library of Wow! eBook ptg 622 CHAPTER 12 Using the DetailsView and FormView Controls The FormView control includes a DataKeyNames property, which contains the name of the primary key column from the data source. When deleting records with the FormView control, you need to indicate the primary key column. Furthermore, the ItemTemplate includes a LinkButton for deleting a record. The LinkButton looks like this: <asp:LinkButton id=”lnkDelete” Text=”Delete Movie” CommandName=”Delete” OnClientClick=”return confirm(‘Are you sure?’);” Runat=”server” /> This LinkButton includes a CommandName property that has the value Delete. When you click the LinkButton, the SQL command represented by the SqlDataSource control’s DeleteCommand property is executed. Also, the LinkButton includes an OnClientClick property that calls the JavaScript confirm() method to display a confirmation dialog box. This extra script prevents users from accidentally deleting database records. Summary In this chapter, you learned how to work with individual database records by using the DetailsView and FormView controls. You learned how to use both controls to display, page, edit, insert, and delete database records. You also learned how to format the appear- ance of both controls. From the Library of Wow! eBook ptg CHAPTER 13 Using the Repeater and DataList Controls IN THIS CHAPTER . Using the Repeater Control . Using the DataList Control . Summary Both the Repeater and DataList controls—the subjects of this chapter—enable you to display a set of data items at a time. For example, you can use these controls to display all the rows contained in a database table. The Repeater control is entirely template-driven. You can format the rendered output of the control in any way that you want. For example, you can use the Repeater control to display records in a bulleted list, a set of HTML tables, or even in a comma-delimited list. The DataList control is also template-driven. However, unlike the Repeater control, the default behavior of the DataList control is to render its contents into an HTML table. The DataList control renders each record from its data source into a separate HTML table cell. In this chapter, you learn how to use both of these controls to display database data. You also learn how to use each of the different types of templates that each of the controls supports. Finally, you can see how to handle the different types of events that the controls expose. Using the Repeater Control The Repeater control provides you with the maximum amount of flexibility in rendering a set of database records. You can format the output of the Repeater control in any way that you want. In this section, you learn how to display data with the Repeater control and handle Repeater control events. From the Library of Wow! eBook . CommandName=”Update” Runat=”server” /> | < ;asp: LinkButton id=”lnkCancel” Text=”Cancel Update” CommandName=”Cancel” Runat=”server” /> </EditItemTemplate> < /asp: FormView> < ;asp: SqlDataSource. record into the Movies data- base table. LISTING 12.23 ShowFormViewInserting.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC -/ /W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>. runat=”server”> <style type=”text/css”> html { background-color:silver; } #content { margins:auto; width:600px; padding:10px; background-color:white; font:14px Georgia,Serif; } a {

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