ASP. NET unleashed-using the datalist and DataGrid controls (1)

Source: Internet
Author: User

Overview ofDatalistAndDataGridControls

TheDatalistAndDataGridControls share items of the same features. before getting into the details of how each of these controls work, you will learn about some of the common properties of the controls. in the following sewing, you will learn how both controls support event bubbling, templates, andDatakeysCollection.

Understanding event bubbling

Three of the standard controls encoded with the ASP. NET Framework support event bubbling:Repeater,Datalist, AndDataGridControls. these controls enable you to capture events in their child controls. when an event is raised in a child control, the event "bubbles up" to the ining control, and the containing control can then execute a subroutine to handle the event.

As you saw in the preceding chapter, for example, you can display a listLinkbuttonControls inRepeaterControl. When you clickLinkbuttonControl, it raisesClickEvent. Instead of writing code to handleClickEvent for eachLinkbuttonIndividually, You can associate one subroutine with anyClickEvent that is raised inRepeaterControl.

TheRepeater,Datalist, AndDataGridControls enable you to associate a subroutine with an event bubbled up from a child control by specifying a value forOnitemcommandProperty. For example, in the case ofRepeaterControl, you wowould associate a subroutine withItemcommandEvent like this:

 
<asp:Repeater  OnItemCommand="Repeater_ItemCommand"  Runat="Server"><ItemTemplate>  <li>  <asp:LinkButton   Text='<%# Container.DataItem( "CategoryName" ) %>'   Runat="Server" /></ItemTemplate></asp:Repeater>

When any ofLinkbuttonControls rendered byRepeaterControl are clicked andClickEvent occurs,Repeater_itemcommandSubroutine executes to handle the event.

TheDatalistAndDataGridControls enable you to go one step further. Both of these controls enable you to make distinctions between the events raised by their child controls.

TheDatalistAndDataGridControls Support the following five events:

  • Itemcommand

  • Updatecommand

  • Editcommand

  • Deletecommand

  • Cancelcommand

When you create the child controls inDatalistOrDataGrid, You can indicate the type of event to raise in the containing control by usingCommandnameProperty. For example, if you want to raiseUpdatecommandEvent, you wocould specifyCommandnameProperty like this:

 
<asp:LinkButton  Text="Update Field!"  CommandName="update"  Runat="Server" />

Three ASP. NET controls haveCommandnameProperty:Linkbutton,Button, AndImagebutton. All three controls also haveCommandargumentProperty that enables you to specify an optional argument that is passed when the control is clicked. So, you can bubble up an optional argument by using an event like this:

 
<asp:LinkButton  Text="Update Field!"  CommandName="update"  CommandArgument="12"  Runat="Server" />

Using templates

Templates were introduced in the preceding chapter.RepeaterControl supports several templates that enable you to format the output of the control. For example, you can useItemtemplateTo Format howRepeaterControl renders each item that it displays.

TheDatalistAndDataGridControls also support templates.DatalistControl supports all the same templates asRepeaterControl, in addition to two others:

  • Headertemplate-Controls how the header ofDatalistIs formatted

  • Itemtemplate-Controls the formatting of each item displayed byDatalist

  • Alternatingitemtemplate-Controls how alternate items are formatted

  • Separatortemplate-Displays a separator between each item displayed byDatalist

  • Footertemplate-Controls how the footer ofDatalistIs formatted

  • Selecteditemtemplate-Controls how a selected item is formatted

  • Edititemtemplate-Controls how an item selected for editing is formatted

When you select an item inDatalist,SelecteditemTemplate is displayed. When you select an item for editing inDatalist,EdititemTemplate is displayed.

TheDataGridControl supports the following four templates when you display a column usingTemplatecolumn:

  • Headertemplate-Controls how the header ofDataGridIs formatted

  • Itemtemplate-Controls the formatting of items displayed byDataGrid

  • Footertemplate-Controls how the footer ofDataGridIs formatted

  • Edititemtemplate-Controls how an item selected for editing is formatted

TheEdititemtemplateIs displayed when you select an item for editing inDataGrid.

When working withDatalistAndDataGridControls, you often run into situations in which you need to retrieve a participant control contained in a template. You can find a participant control in a template by usingFindcontrol ()Method.

You can useFindcontrol ()Method, for example, withControlsCollection ofDatalistitemTo find a participant control inDatalistTemplate. You can useFindcontrol ()METHOD WITHControlsCollection ofDatagriditemTo find a participant control inDataGridTemplate. I will discuss some concrete examples of how to useFindcontrol ()Method later in this chapter.

Using DatakeysCollection

BothDatalistAndDataGridControls have a special collection namedDatakeys. This collection represents the primary key field associated with each item in the control.

Imagine, for example, that you have a database table named products that you want to display and edit withDataGridControl. Imagine, furthermore, that the products table has an identity column namedProductid.

You can associate the value of the identity column with each item inDataGridBy assigning the name of the identity column toDatakeyfieldProperty ofDataGridLike this:

 
<asp:DataGrid  DataKeyField="ProductID"  Runat="Server" />

The advantage of associating the identity column with each item inDataGridIs that you can retrieve the value of this column when you edit a field inDataGrid. If you update a field inDataGridAnd want to update the corresponding field in the underlying database table, you can useDatakeysCollection to uniquely identify the correct row in the database table to update.

You'll come into your SS several situations in whichDatakeysCollection will prove valuable later in this chapter.

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.