Data-binding expressions for Web forms pages

Source: Internet
Author: User
Tags object bind contains eval expression reference visual studio
web| data

Data binding to various control properties in a Web forms page is not achieved by directly binding the property to a data source. Instead, you implement data binding by using a special expression format. Information about the data to bind to is placed in the expression, and the result of the expression is assigned to the control property.

For example, suppose you want to bind a TextBox Web server control to some data. You create a data-binding expression and assign it to the control's Text property so that the value is displayed in the control.

The following example shows the general form of a control declaration in an HTML view. The control's Text property is bound to a data view that contains a single record. Data-binding expressions are delimited by character <%# and%>.

<asp:textbox id= "TextBox1" runat= "server"    text= ' <%# DataView1 (0) ("au_lname")%> ' ></asp:textbox >

Similarly, you can use a data-binding expression to set the ImageUrl property of the Image Web server control. In this case, you are extracting a string from the database that contains the path and file name of the graphic you want to display. An example might resemble the following:

<asp:image id=image1 runat= "Server"    imageurl= ' <%# DataView (0) ("Productphotourl")%> ' >

In Visual Studio, the Properties window provides you with the tools to create data-binding expressions. You can also choose to create your own binding expressions and enter them in the HTML view of the Web Forms Designer.

Advantages of using data-binding expressions

Use data-binding expressions to provide you with flexibility in the following ways:

    • You can use any expression, as long as the expression resolves to a value that the control can use. Most commonly, a data-binding expression resolves to a value that is exported from a data source, but it can also reference the properties of that page or other control, the value that you calculated at run time, or almost any other item.
    • You can assign an expression to any property, that is, you can bind any property to data. For example, you can keep information about user preferences in a database and use data binding to those preferences in implementation properties such as font, color, size, style, and so on. In addition, you can bind more than one control property, which allows you to bind a property to a data source and bind another property to a different source.

Use the DataBinder class for binding

Although you can actually use any expression that resolves to a value for data binding, in most cases you will be bound to some type of data source. The most common scenario is a table in a dataset or Data view that contains a single record of interest to you. To simplify this type of data binding, the ASP.net server control supports a class named DataBinder, which performs some work that extracts data and makes it available to control properties.

You can use the DataBinder class by calling its Eval method, which requires two parameters:

    • A reference to a data container (usually a dataset), a datasheet, or a data view.
    • A reference to a separate value to be exported. This usually refers to a single line (row 0) and the column value in that row.

The following example shows the same data binding that was performed with the text box above, but this time using the DataBinder class.

<asp:textbox id= "TextBox1" runat= "server" text=    ' <%# databinder.eval (DataView1, "[0].au_lname")%> ' > </asp:TextBox>

The previous example of setting the ImageUrl property of the Image control might resemble the following. In the example, a formatting expression is passed in the second argument (optional) of the DataBinder.Eval method, which adds a path as a prefix to the data.

<asp:image id=image1 runat= "Server"    imageurl= ' <%# databinder.eval (Container, "Dataitem.productimage", " http://myserver/myapps/images/{0} ")%> ' >

The advantages of using the DataBinder class are:

    • The syntax is consistent for all bindings and is enforced by the parameters required by the Eval method.
    • The Visual Studio design tool for WEB forms pages supports the DataBinder class.
    • class to perform type conversions automatically. For example, if you bind a text box to a data column that contains integers, the DataBinder class automatically converts integers to strings.
    • You can choose to specify a formatting expression that converts or modifies data.

Resolving data-binding expressions

In order to provide a value that the control can bind to, you must parse the data-binding expression at run time. You can perform this step explicitly during page processing by calling the DataBind method, which is the method of the System.Web.UI.Control class. You can call the method for a separate control, or more effectively, you can call the method for the Page class, which is also exported from the controls class. This method cascaded calls to all child controls, so you can invoke it for all controls on the page once for this method.

The DataBind method is typically invoked in the following situations:

    • The first time the page runs, but after the data source is populated (for example, after you have populated the dataset).
    • After the data source has changed (for example, because records in the data source have been updated).

The following example shows a typical way to invoke the DataBind method during a page initialization event:

' Visual basicprivate Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles mybase.load   Sq Ldataadapter1.fill (DsAuthors1, "authors")   If not (me.ispostback) Then me.databind     () End IfEnd   sub//C # private void Page_Load (object sender, System.EventArgs e) {   SqlDataAdapter1.Fill (dsAuthors1, "authors");   if (!this. IsPostBack)    {this      . DataBind ();   }

Typically, you do not need to call the DataBind method in each round trip (that is, you do not need to check for postbacks in page initialization), because doing so replaces the value in the control. For example, if you use the DataGrid control, the control might contain the changes that you want to work with. When you call DataBind, replace the contents of the grid with values from the data source. If you perform this action during page initialization, you will lose the changes in the grid before you have the opportunity to handle the changes. Instead, you should typically call the DataBind method in an event handler after data processing is performed for the event.




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.