ASP. NET2.0 implements data binding in the template

Source: Internet
Author: User

<Several templated controls (such as DataList and Repeater controls) in v1.x ). ASP. NET>

Data Binding expression

ASP. NET 2.0 improves the Data Binding operation in the template. It simplifies 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 formatted string parameter. Shortened Eval syntax and DataBinder. the difference between Eval is that Eval will automatically parse fields based on the DataItem attribute of the most recent container object (such as DataListItem), while DataBinder. eval needs to use parameters to specify the container. For this reason, Eval can only be used in the template of the data-bound control, but cannot be used in the Page layer. Of course, the ASP. NET 2.0 page still supports DataBinder. Eval. You can use it in environments that do not support simplified Eval syntax.

The following example demonstrates how to bind a new simplified Eval Data Binding syntax to the Image, Label, and HyperLink controls in the DataList data item template (ItemTemplate.

Reference content is as follows:
<Asp: DataList ID = "DataList1" RepeatColumns = "5" Width = "600" runat = "server" performanceid = "objectperformance1">
<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 = "objectperformance1" runat = "server" TypeName = "DataComponentTableAdapters. PhotosTableAdapter" SelectMethod = "GetPhotosForAlbum">


Data Binding can also be part of the theme definition of the control, so that we can freely change the layout and appearance of the templated control by changing the theme. However, the Theme template can only use Eval (or Bind discussed later ). Binding to any user code is forbidden.

FormView Control

The DataList control iterates on data items from the data source and outputs ItemTemplate (data item template) for each data item ). This is useful for displaying the data item list, but you usually want to bind a single data item in a form. To achieve this, ASP. NET 2.0 introduces the FormView control, which can display a data item each time in any template. The main difference between DetailsView and FormView is that DetailsView has a built-in table display mode, while FormView needs to use a custom display template. In other aspects, the FormView and DetailsView object models are very similar. The following example shows a FormView control bound to ObjectDataSource. The ItemTemplate attribute of the FormView contains the Image, Label, and HyperLink controls bound to the data, which is similar to the preceding DataList example.

Reference content is as follows:
<Asp: FormView ID = "FormView1" runat = "server" performanceid = "objectperformance1">
<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>


Similar to DetailsView, FormView also tracks the currently displayed data items. However, when the data source returns to the list, we can select to support paging operations for multiple data items. The following example shows a FormView with the paging function.

Reference content is as follows:
<Asp: FormView ID = "FormView1" Runat = "server" performanceid = "sqlperformance1"
HeaderText = "Books for Author" AllowPaging = "True">
<ItemTemplate>
<Asp: Image ID = "Image1" ImageUrl = ''<% # Eval (" title_id ","~ /Images 02.16.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>


Bidirectional data binding

FormView supports automatic update, insert, and delete operations (similar to DetailsView) through related data source controls ). To define the UI for editing or inserting, you must define the EditItemTemplate or InsertItemTemplate in addition to the ItemTemplate. In this template, You can bind the input control (such as text box, check box, or drop-down list) to the fields of the data source. Data Binding in these templates uses the "two-way" Data Binding syntax, allowing FormView to extract values from the input control of the template and pass them to the data source. These data binding operations replace Eval with the new Bind (fieldname) syntax.

Note: The ID attribute must be set for the data binding control using the Bind syntax.

When the GridView or DetailsView performs an update or insert operation (Columns or Fields of these controls define BoundFields and bind Fields), The GridView or DetailsView is responsible for setting up the input UI in edit or insert mode, therefore, it can automatically extract these values and pass them to the data source. Because the template contains any custom UI controls, the bidirectional data binding syntax is necessary to ensure that the templated controls (such as FormView) are updated, inserted, or deleted, know the control values that should be extracted from the template. You can still use the Eval statement in EditItemTemplate to bind data and pass values to the data source. Please note that FormView and DetailsView support the DataKeyNames attribute like GridView. It stores the original values passed to the primary key Dictionary of the update/delete operation, even if these values are not displayed.
  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next Page

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.