ASP.NET 4 Unleased - p 37 potx

10 247 0
ASP.NET 4 Unleased - p 37 potx

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

Thông tin tài liệu

ptg 334 CHAPTER 7 Creating Custom Controls with User Controls /// <summary> /// Execute the step’s NextStep() method /// and move to the next step /// </summary> protected void btnNext_Click(object sender, EventArgs e) { bool success = ((IWizardStep)_currentStep).NextStep(); if (success) { if (StepIndex < _wizardSteps.Count - 1) { StepIndex++; LoadWizardStep(); } } } /// <summary> /// Move to the previous step /// </summary> protected void btnPrevious_Click(object sender, EventArgs e) { if (StepIndex > 0) { StepIndex ; LoadWizardStep(); } } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <style type=”text/css”> html { font:14px Georgia,Serif; } fieldset { display:block; width:600px; padding:20px; margin:10px; } </style> <title>Wizard</title> From the Library of Wow! eBook ptg 335 Dynamically Loading User Controls 7 </head> <body> <form id=”form1” runat=”server”> <div> <asp:Label id=”lblStepNumber” Runat=”server” /> <fieldset> <legend><asp:Literal ID=”ltlStep” runat=”server” /></legend> <asp:PlaceHolder id=”plhWizardStep” Runat=”server” /> </fieldset> <asp:Button id=”btnPrevious” Text=”&lt; Previous” CausesValidation=”false” OnClick=”btnPrevious_Click” Runat=”server” /> <asp:Button id=”btnNext” Text=”Next &gt;” OnClick=”btnNext_Click” Runat=”server” /> </div> </form> </body> </html> The list of wizard steps is created in the Page_Load() method. The path to each wizard step User control is added to a collection of wizard steps. The StepIndex property represents the index of the wizard step to display. The value of this property is stored in ViewState so that the value is available across multiple page requests. The current wizard step is loaded by the LoadWizardStep() method. This method uses the StepIndex to grab the path to the current wizard step. Next, it uses the Page.LoadControl() method to actually load the wizard step User control. After the LoadWizardStep() method loads the current wizard step, it calls the control’s LoadStep() method and initializes the control. From the Library of Wow! eBook ptg 336 CHAPTER 7 Creating Custom Controls with User Controls The page also contains a Previous and Next button. When you click the Previous button, the btnPrevious_Click() method is called and the StepIndex is reduced by one. When you click the Next button, the btnNext_Click() method is called. The btnNext_Click() method first calls the current wizard step’s NextStep() method. If this method returns the value True, one is added to the StepIndex property, and the next wizard step is loaded. Otherwise, if the NextStep() method returns false, the next wizard step is not loaded. Summary In this chapter, you learned how to build custom controls by creating User controls. The first section covered the basics of User controls. You learned how to create a User control and register it both in a page and in a Web configuration file. You learned how to add custom properties and events to a User control. The next topic was caching and User controls. You learned how to cache the rendered content of a User control in server memory. You also learned how to share the same cached content across multiple pages. You also explored the topic of AJAX and User controls. You learned how to update content in a User control without posting the page that contains the User control back to the web server. Finally, you learned how to add User controls dynamically to a page. You learned how to use the <%@ Reference %> directive to cast a User control to a particular type. You also saw a series of User controls loaded dynamically to create a multipage wizard. From the Library of Wow! eBook ptg CHAPTER 8 Overview of Data Access IN THIS CHAPTER . Using DataBound Controls . Using DataSource Controls . Using Programmatic DataBinding . Understanding Templates and DataBinding Expressions . Overview of SQL Server 2008 Express . Sample Database-Driven Web Application . Summary Any web application worth writing involves data access. In this chapter, you learn how to take advantage of the rich set of controls included in ASP.NET 4 Framework for working with data. You learn how to take advantage of the DataBound controls to display data in your ASP.NET pages. You also learn how to take advantage of the DataSource controls to represent different sources of data such as databases, XML files, and business objects. Next, you are provided with an overview of Microsoft SQL Server 2008 Express, which is the royalty-free database included with Visual Web Developer. You learn how to connect to this database and use it for all your data access needs. Finally, at the end of this chapter, we build a database- driven application, which illustrates how you can use many of the Data controls discussed in this chapter. We build an Employee Directory application. Using DataBound Controls You use DataBound controls to generate your application’s user interface for working with data. The DataBound controls display and edit database data, XML data, or just about any other type of data you can imagine. There are three main types of DataBound controls: list controls, tabular DataBound controls, and hierarchical DataBound. From the Library of Wow! eBook ptg 338 CHAPTER 8 Overview of Data Access Working with List Controls List controls display simple option lists. The ASP.NET 4 Framework includes the following five list controls: . BulletedList— Displays a bulleted list of items. Each item can be displayed as text, a link button, or a hyperlink. . CheckBoxList—Displays a list of check boxes. Multiple check boxes in the list can be selected. . DropDownList—Displays a drop-down list. Only one item in the drop-down list can be selected. . ListBox—Displays a list box. You can configure this control so that only one item in the list can be selected or multiple items can be selected. . RadioButtonList—Displays a list of radio buttons. Only one radio button can be selected. All five controls inherit from the same base ListControl class. This means that all these controls share a core set of properties and methods. In Chapter 10, “Using List Controls,” you can find detailed instructions on how to use each of the list controls. The examples in this chapter rely on local databases. You can simply copy the database files (the .mdf files) from code examples found on the book’s website. We’ll discuss the SQL Server 2008 Express database engine that allows you to utilize these files later in the chapter. The page in Listing 8.1 illustrates how to use all five list controls to display the same set of database records (see Figure 8.1). From the Library of Wow! eBook ptg 339 Using DataBound Controls FIGURE 8.1 Using list controls. LISTING 8.1 ShowListControls.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”> .floater { float:left; border:solid 1px black; padding:5px; margin:5px; } </style> <title>Show List Controls</title> </head> <body> <form id=”form1” runat=”server”> <div class=”floater”> <h3>BulletedList</h3> 8 From the Library of Wow! eBook ptg 340 CHAPTER 8 Overview of Data Access <asp:BulletedList id=”BulletedList1” DataSourceId=”srcMovies” DataTextField=”Title” Runat=”server” /> </div> <div class=”floater”> <h3>CheckBoxList</h3> <asp:CheckBoxList id=”CheckBoxList1” DataSourceId=”srcMovies” DataTextField=”Title” Runat=”server” /> </div> <div class=”floater”> <h3>DropDownList</h3> <asp:DropDownList id=”DropDownList1” DataSourceId=”srcMovies” DataTextField=”Title” Runat=”server” /> </div> <div class=”floater”> <h3>ListBox</h3> <asp:ListBox id=”ListBox1” DataSourceId=”srcMovies” DataTextField=”Title” Runat=”server” /> </div> <div class=”floater”> <h3>RadioButtonList</h3> <asp:RadioButtonList id=”RadioButtonList1” DataSourceId=”srcMovies” DataTextField=”Title” Runat=”server” /> </div> <asp:SqlDataSource id=”srcMovies” ConnectionString=”Data Source=.\SQLExpress; From the Library of Wow! eBook ptg 341 Using DataBound Controls AttachDbFilename=|DataDirectory|MyDatabase.mdf; Integrated Security=True;User Instance=True” SelectCommand=”SELECT Title FROM Movies” Runat=”server” /> </form> </body> </html> In Listing 8.1, each list control is bound to a SqlDataSource control which represents the contents of the Movies database table. For example, the BulletedList control is bound to the DataSource control like this: <asp:BulletedList id=”BulletedList1” DataSourceID=”srcMovies” DataTextField=”Title” Runat=”server” /> <asp:SqlDataSource id=”srcMovies” ConnectionString=”Data Source=.\SQLExpress; AttachDbFilename=|DataDirectory|MyDatabase.mdf; Integrated Security=True;User Instance=True” SelectCommand=”SELECT Title FROM Movies” Runat=”server” /> The BulletedList control includes a DataSourceID attribute, which points to the ID of the SqlDataSource control. The DataSourceID attribute associates a DataBound control with a DataSource control. Working with Tabular DataBound Controls The tabular DataBound controls are the main set of controls that you use when working with database data. These controls enable you to display and, in some cases, modify data retrieved from a database or other type of data source. Six tabular DataBound controls can be divided into two types: those that display multiple data items at a time and those that display a single data item at a time. First, you can use any of the following controls to display a set of data items: . GridView—Displays a set of data items in an HTML table. For example, you can use the GridView control to display all the records contained in the Movies database table. This control enables you to display, sort, page, select, and edit data. . DataList—Displays a set of data items in an HTML table. Unlike the GridView control, more than one data item can display in a single row. 8 From the Library of Wow! eBook ptg 342 CHAPTER 8 Overview of Data Access . Repeater—Displays a set of data items using a template. Unlike the GridView and DataList controls, a Repeater control does not automatically render an HTML table. . ListView—Displays a set of data items using a template. Unlike the Repeater con- trol, the ListView control supports sorting, paging, and editing database data. You can use either of the following two controls to display a single data item at a time: . DetailsView—Displays a single data item in an HTML table. For example, you can use the DetailsView control to display a single record from the Movies database table. This control enables you to display, page, edit, and add data. . FormView—Uses a template to display a single data item. Unlike the DetailsView, a FormView enables you to use to layout a form by using templates. NOTE What happened to the DataGrid? The DataGrid was included in ASP.NET 1.x Framework, but it no longer appears in the Toolbox in Visual Web Developer. The DataGrid is offi- cially deprecated. You should use the GridView control instead because the GridView is more powerful. For backward-compatibility reasons, the DataGrid is included in ASP.NET 4 Framework so that you can still use it in your pages. The page in Listing 8.2 illustrates how you can use each of the tabular DataBound controls (see Figure 8.2). FIGURE 8.2 Using tabular DataBound controls. From the Library of Wow! eBook ptg 343 Using DataBound Controls LISTING 8.2 ShowTabularDataBound.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”> .floater { float:left; border:solid 1px black; padding:5px; margin:5px; } </style> <title>Show Tabular Databound Controls</title> </head> <body> <form id=”form1” runat=”server”> <div class=”floater”> <h3>GridView</h3> <asp:GridView id=”GridView1” DataSourceId=”srcMovies” Runat=”server” /> </div> <div class=”floater”> <h3>DataList</h3> <asp:DataList id=”DataList1” DataSourceId=”srcMovies” RepeatColumns=”2” Runat=”server”> <ItemTemplate> <%#Eval(“Title”)%> <i>directed by</i> <%#Eval(“Director”)%> </ItemTemplate> </asp:DataList> </div> 8 From the Library of Wow! eBook . <legend>< ;asp: Literal ID=”ltlStep” runat=”server” /></legend> < ;asp: PlaceHolder id=”plhWizardStep” Runat=”server” /> </fieldset> < ;asp: Button id=”btnPrevious” Text=”&lt; Previous”. ((IWizardStep)_currentStep).NextStep(); if (success) { if (StepIndex < _wizardSteps.Count - 1) { StepIndex++; LoadWizardStep(); } } } /// <summary> /// Move to the previous step ///. steps is created in the Page_Load() method. The path to each wizard step User control is added to a collection of wizard steps. The StepIndex property represents the index of the wizard step

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

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

  • Đang cập nhật ...

Tài liệu liên quan