asp.net2.0 simplified controls vs. simplified data binding

Source: Internet
Author: User
Tags define bind commit eval insert requires xpath
asp.net| Controls | Data templating data-bound controls provide the maximum flexibility to render data in a page. You can recall several templated controls, such as DataList and Repeater controls, in the ASP.net 1.x version. ASP.net 2.0 continues to support these controls, but in this release, the way in which data is bound to controls in the template has been simplified and improved. This section discusses the various ways in which data is bound in a data-bound control template. <%# Container.DataItem ("Name")%>
<%# Container.DataItem ("Department")%>
<%# DataBinder.Eval (
Container.DataItem, "HireDate", "{0:mm dd yyyy}")%>

It's not going to be so much trouble now, the simplest binding syntax is the following:

<%# Eval ("Name")%>
<%# Bind ("Name")%>
<%# Eval ("HireDate", "{0:mm dd yyyy}")%>

Of course, the previous binding syntax is still available in. net2.0.
The syntax for XML data binding is a little different from the one above.
When you use the XmlDataSource control, the syntax for the binding is as follows:

<% Xpathbinder.eval (Container.DataItem, "Employees/employee/name")%>

The easy way is as follows:

<% XPath ("Employees/employee/name")%>

The formatting is the same as the usual binding:

<% XPath ("Employees/employee/hiredate", "{0:mm dd yyyy}")%>

All of these are the syntax for returning a single node, and when you want to get multiple nodes, the syntax is different:

<% Xpathbinder.select (Container.DataItem, "Employees/employee")%>
<% xpathselect ("Employees/employee")%>

Data-binding expressions
ASP.net 2.0 improves data binding in the template, simplifying the data-binding syntax DataBinder.Eval (Container.DataItem, fieldname) of the complete v1.x syntax to Eval (fieldname). As with DataBinder.Eval, the Eval method also accepts an optional formatstring parameter. The abbreviation for the Eval syntax differs from DataBinder.Eval in that Eval automatically parses the field based on the DataItem property of the nearest container object (DataListItem in the example above), and DataBinder.Eval accepts a Parameters. Therefore, Eval is used only in the templates of data-bound controls, not at the page level. Of course, the ASP.net 2.0 page continues to support DataBinder.Eval, so it can be used in situations where the simplified Eval syntax is not supported.
<asp:datalist datasourceid= "ObjectDataSource1" runat= "Server" > <ItemTemplate> <asp:image imageurl= ' & lt;%# eval ("FileName", "images/thumbs/{0}")%> ' runat= ' server '/> <asp:label ' text= ' <%# Eval ("Caption")%> ; ' runat= ' server '/> </ItemTemplate></asp:DataList>
The following example shows the new simplified Eval data binding syntax for binding Image, Label, and HyperLink controls in DataList ItemTemplate.

C # DataBinding in a DataList Template

Data binding can also contain part of the definition of a control's theme so that changes to the applied theme can significantly alter the layout and appearance of the templated control. However, you can only use Eval (or the Bind to be discussed later) in the Theme template. Cannot bind to user code at will. The next example shows the theme applied to the previous example to create a completely different look for a photo page. For more information about topics, refer to the "Apply styles, themes, and Skins" section of this tutorial.

C # DataBinding in a DataList Template (themed)


FormView Control
The DataList control iterates through each data item of the data source, outputting one ItemTemplate for each item. This is useful for rendering a list of items, but typically requires data binding based on a single item of data in a form. Therefore, ASP.net 2.0 introduces the FormView control, which renders a single data item at a time in any form of the template. The main difference between DetailsView and FormView is that DetailsView has built-in table rendering, and FormView requires user-defined templates for rendering. The FormView and DetailsView object models are very similar in other ways. The following example shows a FormView control that is bound to ObjectDataSource. As with the previous DataList example, the FormView ItemTemplate property contains data binding Image, Label, and HyperLink.

C # DataBinding in a FormView Template

Similar to DetailsView, FormView tracks currently rendered items and optionally supports paging of multiple items when the data source returns a list. The following example shows a FormView that has paging enabled.

C # DataBinding in a FormView Template (Paged)


Bidirectional data binding
As with the DetailsView control, FormView supports automatic Update, Insert, and Delete operations through its associated data source control. To define the input UI for an edit or insert operation, you can define EditItemTemplate or insertitemtemplate while defining ItemTemplate. In this template, you will data bind an input control such as a TextBox, CheckBox, or DropDownList to a field that is bound to a data source. However, data binding in these templates uses the bidirectional data binding syntax, which allows FormView to extract the value of the input control from the template for delivery to the data source. These data bindings use the new bind (fieldname) syntax instead of Eval.

Important: A control that uses bind syntax for data binding must have an ID attribute set.


<asp:formview datasourceid= "ObjectDataSource1" datakeynames= "photoid" runat= "Server" > <edititemtemplate > <asp:textbox id= "captiontextbox" text= ' <%# Bind ("Caption")%> ' runat= ' server '/> <asp:button Text = "Update" commandname= "Update" runat= "server"/> <asp:button text= "Cancel" commandname= "Cancel" runat= "Server"/ > </EditItemTemplate> <ItemTemplate> <asp:label text= ' <%# Eval ("Caption")%> ' runat= ' server ' /> <asp:button text= "edit" commandname= "edit" runat= "Server"/> </ItemTemplate></asp:FormView>
When you perform an update or insert operation on a GridView or DetailsView, if the column or field of the control defines Boundfield,gridview or DetailsView to create an input UI in Edit or insert mode so that it automatically extracts These input values are passed back to the data source. Because the template contains arbitrary user-defined UI controls, you need to use bidirectional data-binding syntax so that templated controls such as FormView can know which control values should be extracted from the template for update, insert, or delete operations. In EditItemTemplate, you can still use the Eval syntax for data binding that is not passed back to the data source. Note also that FormView, like DetailsView and GridView, supports the use of the DataKeyNames property to preserve the original values of primary key fields, even if they are not rendered, to be passed back to the update/insert operation.

FormView supports the use of the DefaultMode property to specify the default template to display, but by default, FormView starts in ReadOnly mode and renders ItemTemplate. To enable the UI for converting from ReadOnly mode to edit or Insert mode, you can add a Button control to the template and set its CommandName property to edit or New. You can add a button CommandName set to update or Cancel within EditItemTemplate to commit or abort the update operation. Similarly, you can add a button that CommandName is set to insert or Cancel to commit or abort the insert operation.

The following example shows a FormView that defines ItemTemplate and EditItemTemplate. ItemTemplate contains controls that use the Eval (one-way) binding, and EditItemTemplate contains a TextBox control that uses BIND statements to bind bidirectional. Primary key fields (PhotoID) are roundtrip in view state using the DataKeyNames property. FormView contains command buttons that are used to toggle between their templates.

C # Two-way Databinding in a FormView Edit Template

Templated UIs are also supported by using Templatefield,gridview and DetailsView that are added to the Columns or Fields collection. TemplateField supports the use of ItemTemplate, EditItemTemplate, and InsertItemTemplate (DetailsView only) to specify the field UI in different rendering modes for these controls. As with the FormView example above, bidirectional bindings in EditItemTemplate or InsertItemTemplate allow the GridView or DetailsView to extract values from controls in these templates. A common use of TemplateField is to add validator controls to EditItemTemplate for declarative validation of the GridView or DetailsView operation. The following example shows an example of this method. For more information about the validation controls available in ASP.net, refer to the "Validating Form Input Controls" section of this tutorial.

C # Validation in a GridView Edit Template

Another use of TemplateField is to customize the input control, which is used to enter the GridView or DetailsView column/field values. For example, you can place the DropDownList control in the EditItemTemplate of TemplateField to allow selection from a predefined list of values. The following example shows this method. Note that the DropDownList in this example is data-bound to its own data source control to get the value of the list dynamically.

C # DropDownList in a GridView Edit Template




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.