Data Binding expression (I):. Net discovery Tour (I)

Source: Internet
Author: User

As. NET platform software developers, we frequently interact with a variety of data, the data often comes from text, custom types, XML, databases, etc., there are many ways to access this data, the data binding expression is one of the most common and practical methods. I will use two blog posts to explain them as much as possible. . NET platform data binding expression source, usage, underlying principles, and efficiency. In addition, I first published these two blog posts in the blog Park.

I. Summary

The data binding expression must be <% # And %> characters. The format is as follows:

<Tagprefix: tagname property = '<% # data-binding expression %> 'runat = "server"/>

Or:

<% # Data-binding expression %>

ASP. NET supports hierarchical data binding models. Data Binding expressions use the eval and bind methods to bind data to controls and submit changes to the database.

The eval method is a static one-way (read-only) method. Therefore, the eval function is used for one-way (read-only) binding. This method uses the value of a data field as a parameter and returns it as a string.

The bind method supports the read/write function, so the BIND function is used for Bidirectional (updatable) binding. This method retrieves the value of the data-bound control and submits any changes to the database.

The XPath method supports XML data sources.

Ii. Location of the Data Binding expression

1. The data binding expression can be contained in the value side of the attribute name/attribute value pair in the start tag of a server control or common HTML element. For example:

<Asp: textbox id = "textbox1" runat = "server" text = '<% # data binding expression %>'> </ASP: textbox> <br/>

In this case, the binding expression of the data can be a variable or a C # Or VB with a returned value. can also be the value of a property of a control, or C # Or VB.. Net object. Of course, it can also be a string, such as "hello ".

If the data binding expression is eval ("a field in a table in the Database"), You must place textbox1 in the template of a circular display control. Otherwise, the system prompts: data Binding methods such as eval (), XPath (), and bind () can only be used in the context of a data binding control. In fact, you want to place textbox1 in a template of controls such as repeater, datalist, and gridview.

2. The data binding expression is contained anywhere on the page. For example:

<Form ID = "form1" runat = "server">

<Div>

<% # Eval ("data binding expression 1") %>

<% # Eval ("data binding expression 2") %>

</Div>

</Form>

In this case, the binding expression of the data can be a variable or a C # Or VB with a returned value. can also be the value of a property of a control, or C # Or VB.. Net object. Of course, it can also be a string, such as "hello ".

If the data binding expression is eval ("a field in a table in the Database"), you must <% # eval ("data binding expression 1 ") %> <% # eval ("data binding expression 2") %> is placed in a template for controls such as repeater, datalist, and gridview.

3. You can include the data binding expression in JavaScript code to call the C # Or VB. Net Method in JavaScript. For example:

Deafult2.aspx:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default2.aspx. cs" inherits = "default2" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> No title page </title>

<Script language = "JavaScript" type = "text/JavaScript">

Function getstr ()

{

VaR;

A = '';

A = '<% # csharptojavascript () %>' // call the C # Method

Alert ();

}

</SCRIPT>

</Head>

<Body>

<Form ID = "form1" runat = "server">

<Div>

<Input id = "button1" type = "button" value = "javascript calls the C # method! "Onclick =" getstr () "/</div>

</Form>

</Body>

</Html>

Default2.cs:

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class default2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Page. databind (); // You must bind the method with the returned value before javascript can call the C # method!
}
Public String csharptojavascript ()
{
Return "javascript calls the C # method! ";
}
}

3. What types of data binding expressions can be used?

1. It can be a variable.

Example: <asp: Label id = "label1" runat = "server" text = "<% # variable name %>"> </ASP: Label>

2. It can be the property value of the server control.

Example: <asp: Label id = "label1" runat = "server" text = "<% # textbox2.text %>"> </ASP: Label>

3. It can be a collection object such as an array.

For example, bind an array to a list control, such as ListBox or repeater, datalist, or gridview, in this case, you only need to set the attribute datasource = '<% # array name %> '.

4. It can be an expression.

For example, if person is an object and name and city are its two attributes, the data binding expression can be written as follows:

<% # (Person. Name + "" + person. City) %>.

5. It can be a method.

For example, <% # GetUserName () %>. GetUserName () is a defined C # method. Generally, a return value is required.

6. It can be the fields of the data table obtained by using Eval, datebinder. Eval. This is the most common and is no longer an example.

Note: If the data binding expression is used as the attribute value and no double quotation marks appear in the data binding expression, the outermost layer of the <% # data binding expression %> can be either double quotation marks or single quotation marks. If the data binding expression contains double quotation marks, the outermost layer of the <% # data binding expression %> should preferably use single quotation marks.

ThuWhich data binding expressions are available for databases and data binding to data sources such as dataview, able, and dataset?

1, <% # databinder. eval (container. dataitem, "field name") %>

<% # Databinder. eval (container. dataitem, "field name", "{0: c}") %>

There are two other types that are not commonly used:

<% # Databinder. eval (container, "dataitem. Field name") %>

<% # Databinder. eval (container, "dataitem. Field name", {0: c}) %>

Container. dataitem is equivalent to a row record in a table or view in the database, and a row can have many columns.

Use the Three-object operator? Example:

<% # Databinder. eval (container. dataitem, "field name "). Tostring (). Trim (). Length> 16? Databinder. eval (container. dataitem, "field name "). Tostring (). Trim (). Substring (0, 16): databinder. eval (container. dataitem, "field name "). Tostring (). Trim () %>

2, <% # eval ("field name") %>

<% # Eval ("field name", "{0: c}") %>

. NET 2.0 is a new method. It is equivalent to databinder. eval.

Example of binding birthday:

<% # String. Format ("{0: yyyy-mm-dd dddd}", Eval ("stubirth") %>

<% # Databinder. eval (container. dataitem, "stubirth", "{0: yyyy-mm-dd}") %>

Example of using the three-object OPERATOR:

<% # (Eval ("gender ")). Tostring () = "true "? "Male": "female" %>

Gender field type: yes/no (ACCESS), bit (SQL Server)

Example of calling a method:

<% # Getuserphoto (eval ("photopath") %>

Getuserphoto () Definition:

String getuserphoto (Object photopath)

{

If (photopath = dbnull. Value) <% # (datarowview) container. dataitem) ["field name"] %>

Bind to dataview, able, Dataset

{

Return " ";

}

Else

{

Return " ";

}

}

3, <% # (datarowview) container. dataitem) ["field name"] %>

<% # String. Format ("{0: c}", (datarowview) container. dataitem) ["field name"]) %>

Container. dataitem is equivalent to a row record in a table or view in the database, and a row can have many columns.

An example of multiplication after type conversion:

<% # (INT) (datarowview) container. dataitem) ["field name 1"] * (INT) (datarowview) container. dataitem) ["field name 2"] %>

Efficiency of the above three binding methods: the eval method will call the databinder. Eval method during execution, and the databinder. Eval method will use reflection to execute later binding computing during runtime, which will cause a significant reduction in performance. Therefore, the performance may be significantly reduced. Therefore, <% # (datarowview) container. dataitem) ["field name"] %> has the best performance among the three.

 

Address: http://dotnet.chinaitlab.com/DotNetFramework/784463.html

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.