Operating data 33 in asp.net 2.0: Master/From report based on DataList and Repeater using DropDownList-self-study process

Source: Internet
Author: User

Introduction

In the previous chapter of the master/detail filter using DropDownList, we use the Master/detail table created by the GridView to display some "master" records. Users can view the contents of the from (verbose) based on the master record. Master/ It is a good choice to take information from a table when it renders a one-to-many relationship and a table with more than one column. We've learned how to use the GridView and DetailsView in the front. This chapter and the next two chapters will review these concepts again, But the main learning is to use DataList and Repeater. In this chapter we will learn to use DropDownList to include master Records, while in DataList display from records.

Step one: Add the master/tutorial page

First, add the folders (datalistrepeaterfiltering) and pages that this tutorial will use. When you create a new page, remember to choose Site.master.

Default.aspx
Filterbydropdownlist.aspx
Categorylistmaster.aspx
Productsforcategorydetails.aspx
Categoriesandproducts.aspx


Figure 1: Creating datalistrepeaterfiltering folders and Pages

Then open the Default.aspx page and drag the Sectionleveltutoriallisting.ascx user control in.


Figure 2: Adding Sectionleveltutoriallisting.ascx to the Default.aspx page

We need to add the master/tutorial to the site map. Open Web.sitemap and add the following markup to the displaying Data with the DataList and Repeater node:

<sitemapnode
 title= "Master/detail Reports with the DataList and Repeater"
 description= "the Samples of Reports That use the DataList and Repeater Controls "
 url=" ~/datalistrepeaterfiltering/default.aspx ">
 
 < SiteMapNode
 title= "Filter by Drop-down list"
 description= "filter results using a drop-down list."
 Url= "~/datalistrepeaterfiltering/filterbydropdownlist.aspx"/>
 
 <sitemapnode
 title= "Master/Detail" Across two Pages "
 description=" Master Records on one page, detail records on another. "
 Url= "~/datalistrepeaterfiltering/categorylistmaster.aspx"/>
 
 <sitemapnode
 title= "Maser/Detail on One page "
 description=" Master Records in the left column, details on the right, and both on the
   same Page.
 Url= "~/datalistrepeaterfiltering/categoriesandproducts.aspx"/>
 
</siteMapNode>


Figure 3: The updated site Map

Step two: Show categories in DropDownList

Our master/From table will be listed in the DropDownList categories, The product of the selected item is then displayed with DataList. Open the Filterbydropdownlist.aspx page in the Datalistrepeaterfiltering folder, Drag a DropDownList in. Sets the DropDownList ID to categories. Select the data source on the smart label and create a ObjectDataSource named Categoriesdatasource


Figure 4: Adding a ObjectDataSource named Categoriesdatasource

Use the GetCategories () method of the CATEGORIESBLL class to configure ObjectDataSource. Then the text and value configuration fields for DropDownList (CategoryName and CategoryID, respectively) ).


Figure 5: Configuring the DropDownList text and value

The Categories table is now listed in DropDownList. See Figure 6.


Figure 6: DropDownList after completion

Step Three: Add Products DataList

The following is a list of the product associated with the selected category. Add a DataList, Create a ObjectDataSource named Productsbycategorydatasource. Getproductsbycategoryid with PRODUCTSBLL class (CategoryID) To configure it. Because our report is read-only, select None in the Insert,update and delete tabs.


Figure 7: Selecting the Getproductsbycategoryid (CategoryID) method

Click Next, the wizard prompts us to select source for the CategoryID parameter. Set the parameter source to Control,controlid to categories.


Figure 8: Setting the CategoryID parameter to categories DropDownList

When the above configuration is complete, Visual studio automatically generates a ItemTemplate for DataList to display the name and value of each field. Let's make some improvements, show only the product's Name,category,supplier , quantity and price, and add a

<asp:datalist id= "DataList1" runat= "datakeyfield=" ProductID "datasourceid=" Productsbycategorydatasource "Enableviewstate=" False > <ItemTemplate>  

Look at the page in the browser. On the first visit, the product associated with the Beverager is displayed (Figure 9), but changing the DropDownList does not update the data. This is because the update DataList requires postback. We set the AutoPostBack property of DropDownList to True.


Figure 9: The first visit shows Beverage products


Figure 10: Select a new category (produce), update DataList

Add a "--Choose a Category-" List Item the first time a page is accessed, the Beveages is selected by default, and its product is displayed in DataList. In the main/from the report using the DropDownList filter We added " --Choose a Category--"option (default) to display all of the product. This is convenient when displaying product in the GridView. For DataList, each product has a large screen, so it's choosing Choose a Category-"will not display product below. Select the Items property in the DropDownList attribute and add an entry with text"--Choose a Category-", value 0.


Figure 11: Adding "--Choose a Category--" Item

You can also add the following code directly to the DropDownList Markup Language:

<asp:dropdownlist id= "Categories" runat= "Server" autopostback= "True"
 datasourceid= "Categoriesdatasource" Datatextfield= "CategoryName"
 datavaluefield= "CategoryID" enableviewstate= "False" >
 
 <asp:listitem Value= "0" >--Choose a Category--</asp:listitem>
 
</asp:DropDownList>

In addition, we need to set the DropDownList AppendDataBoundItems to true. Because if False (the default), the manually added list item is overwritten when categories is bound to DropDownList.


Figure 12:set The AppendDataBoundItems property to True

We set the value of "--Choose a Category-" to 0 because there is no categories value of 0 in the system, so there is no product return when this Category is selected. Look at the Web page to confirm this. See Figure 13.


Figure 13: When "--Choose a Category--" is selected, no products are displayed

If you want to display all of the product when you select "--Choose a Category--", set its value to 1. Attentive readers will remember the main/from report using DropDownList filter We have updated the Getproductsbycategoryid (CategoryID) method of the Productsbll class, and if CategoryID is 1 o'clock all the product records will be returned.

Summarize

When displaying hierarchical data, it is helpful to use a master/detail table to present the data. The user can start with the data at the highest level and gradually enter the most detailed data. In this chapter we learn a simple master/ Displays the product under the selected category from the table. We use DropDownList to list the dategory,datalist to display the product. In the next chapter we will learn to separate master/detail records to two pages. In the first page, show all the master records , and there is a link to the "from" message. Dot this link user will see a page showing the details.

I wish you a happy programming!

Introduction of the author

Scott Mitchell, with six asp/asp. NET book, is the founder of 4GuysFromRolla.com, has been applying Microsoft Web technology since 1998. Scott is an independent technical consultant, trainer, writer, recently completed a new book to be published by Sams Press, proficient in asp.net 2.0 within 24 hours. His contact email is mitchell@4guysfromrolla.com, or he can contact him through his blog Http://ScottOnWriting.NET.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.