The data binding expression must be <% # And %> characters. The format is as follows:
<tagprefix:tagname property='<%#data-binding expression%>' runat="server">
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 updatability.) Bind. This method retrieves the value of the data-bound control and submits any changes to the database.
The data binding expression can appear at the following position 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 runat = "server" Text = '<% # data binding expression %>'> </asp: TextBox>
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 included in any position on the page..
For example:
<Div> <% # data binding expression %> </div>
It also complies with the "1" note clause.
If the data binding expression is Eval ("a field in a table in the Database"), you must set <% # Eval (
Data Binding expression 1 ") %> <% # Eval (" data binding expression 2 ") %> placed like Repeater, DataList, GridVie
In the template of such a control.
3. You can include data binding expressions in Javascript code to call C # OrVB. NET method.
For example:
Deafult2.aspx:
<script language="javascript" type="text/javascript"> function GetStr() { var a; a = ''; a = '<%#CSharpToJavaScript() %>>'; alert(a); }</script>
Default2.aspx. cs:
public string CSharpToJavaScript(){ ...}
Data Binding expressions can all be of the following type:
1. It can be a variable.
Example: <asp: LabelID = "Label1" runat = "server" Text = "<% # variable name %>"> </asp: Label>
2. It can be the property value of the server control.
Example: <asp: LabelID = "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 using Eval, DateBinder. Eval.
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.
Data Binding expressions related to databases and bound to data sources such as DataView, able, and DataSet include:
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.
The last parameter is in the same Format as String. Format. For example, c indicates currency, p indicates percentage, and d indicates short day.
The format is displayed. f indicates the floating point reality, f3 indicates the last three decimal places, and so on.
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). 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.
The last parameter is in the same Format as String. Format. For example, c indicates currency, p indicates percent, and d indicates currency.
The date format is displayed. f indicates the floating point reality, f3 indicates the last three decimal places, and so on.
<%#string.Format("{0:yyyy-MM-dd dddd}",Eval("date")) %><%#DataBinder.Eval(Container.DataItem,"date","{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 using method call:
<%# GetUserPhoto(Eval("PhotoPath")) %>
GetUserPhoto () Definition:
private string GetUserPhoto(object photoPath){ if (photoPath==DBNull.Value) { return "