Data Binding expression

Source: Internet
Author: User
Data Binding expression

I haven't updated it in a year. I thought it was good before.ArticleI thought it would be better to understand it. Today I saw an article about data binding, and I thought it was good, so I posted it. it is also a sign of your re-efforts :)

Let's start from a little bit. Let the blog Park give a witness.
Start with the original article :)

Let's talk about the data binding expression in. Net today.
Data Binding expression
It must be between <% # And %> characters. The format is as follows:

< Tagprefix: tagname Property = '<% # Data-binding expression %>' Runat = "Server"   />

Or:

<% # Data-binding expression %>

ASP. NET supports the hierarchical data binding model. The data binding expression is used.EvalAndBindMethod to bind data to the control and submit the changes back to the database.
EvalThe method is static one-way (read-only) method, soEvalThe 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.
BindMethod supports read/write, soBindFunctions are 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.

Where can data binding expressions appear on the page?

1. You can include the data binding expression on 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 />

Note: The top-bound 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 >

It also complies with the "1" note clause.
If the data binding expression is eval ("a field in a table in the Database "), <% #Eval("Data binding expression 1") %> <% #Eval("Data Binding expression 2") %> In the template of controls such as repeater, datalist, and gridview.

3. Data Binding expressions can be included 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 =   '' ;
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 (); // If a method has a return value, you must bind it to implement JavaScript to call the C # method!
}
Public   String Csharptojavascript ()
{
Return   " Javascript calls the C # method! " ;
}
}

What types of data binding expressions can all be?
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, which generally requires a return value.
6. It can be the fields of the data table obtained by using Eval, datebind. 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.

What are common database-related data binding expressions?

1, <% # databind. eval (container. dataitem, "field name") %>
<% # Databind. 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 in the database, and a row can have many columns.
The last parameter and 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 databind. eval.
The last parameter and

String Getuserphoto ( Object Photopath)
{
If (Photopath = Dbnull. Value) <% # (datarowview) container. dataitem) ["field name"] %>

{
Return   " " ;
}
Else
{
Return   " "   + Photopath. tostring () +   " '> " ;
}
}

3, <% # (datarowview) container. dataitem) ["field name"] %>
<% # string. format ("{0: c}", (datarowview) container. dataitem) ["field name"]) %>
Use
type conversion example: <% # (INT) (datarowview) container. dataitem) ["field name 1"] * (INT) (datarowview) container. dataitem) ["field name 2"] %> means that two fields are multiplied.
efficiency of the preceding three binding methods: databinder is called when the eval method is executed. eval method, databinder. when the eval method is run, the reflection is used to perform subsequent binding calculations, which will significantly degrade the performance. Therefore, the performance may be significantly reduced. Therefore, <% # (datarowview) container. dataitem) ["field name"] %> has the best performance among the three.

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.