ASP.net 2.0 implementing the data Binding series in the template

Source: Internet
Author: User
Tags bind eval join

Templated Data-bound controls provide the fundamental flexibility to display data on the page. You may remember several templated controls (such as the DataList and Repeater controls) in ASP.net v1.x. asp.net 2.0 still supports these controls, but the syntax for binding data in the template has been simplified and improved. This article discusses a variety of ways to bind data in a data-bound control template.

Data-binding expressions

ASP.net 2.0 improves data-binding operations in the template, simplifying the data-binding syntax DataBinder.Eval (Container.DataItem, fieldname) in v1.x to Eval (fieldname). The Eval method, like DataBinder.Eval, can accept an optional format string parameter. The difference between the shortened eval syntax and the DataBinder.Eval is that Eval automatically parses the fields based on the DataItem properties of the nearest container object (such as DataListItem), and DataBinder.Eval needs to use parameters to specify the container. For this reason, eval can only be used in a data-bound control's template, not the page layer. Of course, DataBinder.Eval is still supported on the ASP.net 2.0 page, and you can use it in an environment that does not support simplified Eval syntax.

The following example shows how to use the new simplified eval data binding syntax to bind to the image, label, and Hyperlink controls in the DataList data item template (ItemTemplate).

 <asp:DataList ID="DataList1" RepeatColumns="5" Width="600" runat="server" DataSourceID="ObjectDataSource1">
  <ItemTemplate>
   <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("PhotoID", "PhotoFormViewPlain.aspx?ID={0}") %>'>
   <asp:Image ID="Image1" Runat="server" ImageUrl='<%# Eval("FileName", "images/thumbs/{0}") %>' /></asp:HyperLink>
   <asp:Label ID="CaptionLabel" runat="server" Text='<%# Eval("Caption") %>' />
  </ItemTemplate>
 </asp:DataList><br />
 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="DataComponentTableAdapters.PhotosTableAdapter" SelectMethod="GetPhotosForAlbum">

Data binding can also be part of the subject definition of a control (theme definition) so that we can change the layout and appearance of the templated control arbitrarily by changing the theme. However, the theme (subject) template can only use eval (or the bind discussed later). Binding to arbitrary user code is prohibited.

FormView control

The

DataList control iterates through the data items from the data source and outputs ItemTemplate (data item templates) for each data item. This is useful for displaying a list of data items, but typically you want to implement the binding operation of a single data item in a form. To do this, ASP.net 2.0 introduces the FormView control, which can display one data item at a time in any template. The main difference between DetailsView and FormView is that DetailsView has built-in tabular display, and FormView needs to use user-defined display templates. In other respects the FormView and DetailsView object models are very similar. The following example shows a FormView control that is bound to ObjectDataSource. The FormView ItemTemplate property contains image, label, and hyperlink controls for data binding, similar to the previous DataList example.

<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1">
  <ItemTemplate>
   <asp:Label ID="CaptionLabel" runat="server" Text='<%# Eval("Caption") %>' Font-Size="32pt" /><br />
   <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("FileName", "images/{0}") %>' />
   <asp:HyperLink ID="HyperLink1" Text="Back to Album" NavigateUrl='<%# Eval("AlbumID", "PhotosDataList.aspx?ID={0}") %>' runat="server" />
  </ItemTemplate>
 </asp:FormView>
 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="DataComponentTableAdapters.PhotosTableAdapter" SelectMethod="GetPhoto">
  <SelectParameters>
 <asp:QueryStringParameter Name="PhotoID" DefaultValue="9" QueryStringField="ID" />
 </SelectParameters>
 </asp:ObjectDataSource>

FormView is similar to DetailsView and keeps track of the data items that are currently displayed, but when the data source returns to the list, we can also choose to support paging operations for multiple data items. The following example shows a FormView with paging functionality.

<asp:formview id= "FormView1" runat= "Server" datasourceid= "SqlDataSource1"
headertext= "Books for Author" allowpaging= "True" >
<ItemTemplate>
<asp:image id= "Image1" Imageurl= ' <%# Eval ("title_id", "~/images/{0}.gif")%> ' runat= ' server '/>
<asp:label id= "Label1" font-size= "1.2em" font-bold= "true" text= ' <%# Eval ("title")%> ' runat= ' server '/>
<asp:label id= "Label2" text= ' <%# Eval ("price", "{0:c}")%> ' runat= ' server '/>
</ItemTemplate>
</asp:FormView>
<asp:sqldatasource id= "SqlDataSource1" runat= "Server" selectcommand= "select dbo.authors.au_id, dbo.titles.title_id, Dbo.titles.title, Dbo.titles.type, Dbo.titles.price, dbo.titles.notes from dbo.authors INNER JOIN dbo.titleauthor on dbo . authors.au_id = dbo.titleauthor.au_id INNER JOIN dbo.titles on dbo.titleauthor.title_id = dbo.titles.title_id WHERE (dbo . authors.au_id = @au_id) "
connectionstring= "<%$ connectionstrings:pubs%>"
<SelectParameters>
<asp:querystringparameter name= "au_id" defaultvalue= "213-46-8915" querystringfield= "id"/>
</SelectParameters>
</asp:SqlDataSource>
Related Article

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.