Data binding: <% #Eval ("Partyname")%> '

Source: Internet
Author: User
1.asp:hyperlink id= "Link" runat= "server" target= "_blank" text= ' <% #Eval ("Partyname")%> '

Navigateurl= ' <%# "details.aspx?id=" + Eval ("id") + ""%> ' ></asp:HyperLink> this as long as the implementation of hyperlinks, navigateurl= ' <%# "details.aspx?id=" + Eval ("id") + "%>" > is the address that jumps to the new page.
Eval ("") and Bind ("") are two one-way bindings, a two-way binding, bind is a two-way binding, but the data source support DataBinder.Eval (Container.DataItem, fieldname) is simplified to Eval ( FieldName). The Eval method, like DataBinder.Eval, can accept an optional format string parameter. The difference between the shortened eval syntax and the DataBinder.Eval is that Eval automatically parses the fields based on the DataItem properties of the nearest container object (such as DataListItem), and DataBinder.Eval needs to use parameters to specify the container. For this reason, eval can only be used in a data-bound control's template, not the page layer. Of course, DataBinder.Eval is still supported on the ASP.net 2.0 page, and you can use it in an environment that does not support simplified Eval syntax.
the data-binding expression must be contained between the <% #和%> characters. The format is as follows: <tagprefix:tagname property= ' <%# data-binding expression%> ' runat= ' server '/>
Or as follows:
<%# data-binding Expression%>
asp.net supports a hierarchical data-binding model that uses the Eval and bind methods to bind data to a control and submits the changes back to the database. The Eval method is a static one-way (read-only) method, so the Eval function is used for one-way (read-only) binding, which takes the value of a data field as a parameter and returns it as a string. The Bind method supports read/write functionality, so the BIND function is used for bidirectional (updatable) bindings. This method retrieves the value of a data-bound control and submits any changes back to the database. A data-bound binding expression is contained anywhere in the page. For example:
<form id= "Form1" runat= "Server" >
<div>
<% #Eval ("data-binding Expression 1")%>
<% #Eval ("data-binding Expression 2")%>
</div>
</form>
If the data-binding expression at this point is Eval (a field in a table in the database) and so on, you must put the <% #Eval (data Binding expression 1)%> <% #Eval ("Data Binding Expression 2")%> like Repeater, Datalist,gridview in a template for such a control.

You can include data-binding expressions in JavaScript code to implement methods that call C # or vb.net 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" >
<title> Untitled Page </title>
<script language = "javascript" type= "Text/javascript" >
function Getstr ()
{
var A;
A = ';
A= ' <% #CSharpToJavascript ()%> '//Call method for C #
alert (a);
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<input id= "Button1" type= button "value=" JavaScript calls the C # Method! "onclick=" Getstr () "

/</div>
</form>
</body>
<ml>
Default2.cs: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)
{
The Page.DataBind ()//method has the return value to bind first in order to implement the method that JavaScript calls C #!
}
public string Csharptojavascript ()
{
Return "JavaScript calls the C # Method!";
}
}
What types of data-binding expressions can be?
1, can be a variable
For example: <asp:label id= "Label1" runat= "Server" text= "<% #变量名%>" ></asp:Label>
2, can be the property value of the server control
For example: <asp:label id= "Label1" runat= "Server" text= "<% #TextBox2. Text%>" ></asp:Label>
3, can be an array and other collection objects
For example, to bind an array to a list control, such as a ListBox, or a control such as Repeater,datalist,gridview, you only need to datasource= the property to the ' <%# array name%> '.
4, can be an expression
For example: The person is an object, name and city are its 2 properties, then the data-binding expression can be written like this:
<%# (Person.name + "" + person.city)%>.
5, can be a method
For example: <% #GetUserName ()%>. GetUserName () is a defined C # method that generally requires a return value.
6, can be obtained by using Eval,datebind.eval data table fields, this is the most common, no longer examples.
Note: If a data-binding expression is the value of a property, as long as there are no double quotes in the data-binding expression, the outermost layer of the <% #数据绑定达式%> can be in double quotes or single quotes. If double quotes appear in a data-binding expression, it is best to use single quotes for the outermost layer of the <% #数据绑定表达式%>.

<% #Eval (field name)%>
<% #Eval ("Field name", "{0:c}")%>
. NET 2.0 A new method that appears. and Databind.eval () equivalence.
The last parameter is the same as the String.Format form. For example, c for currency, p for percent semicolon, D for short date format display, F for floating-point reality, F3 for three digits after the decimal point, one analogy.
Examples of using the three-mesh operator:
<%# (Eval ("Sex")). ToString () = = "True"? Male ":" Female "%>
Gender field type is: Yes/No (Access), bit (SQL Server)
Examples of using method calls:
<%# Getuserphoto (Eval ("Photopath"))%>
Definition of Getuserphoto ():
String Getuserphoto (Object Photopath) {
if (Photopath = = DBNull.Value) <%# ((DataRowView) container.dataitem) [Field name]%>
{
Return "}
Else
{
Return "}
}
<%# ((DataRowView) container.dataitem) [Field name]%>
<%# string. Format ("{0:c}", ((DataRowView) container.dataitem) [Field name])%>
The uses the String.Format method to set the display style for the field. For example, c for currency, p for percent semicolon, D for short date format display, F for floating-point reality, F3 for three decimal points, one analogy.
  Type Conversion example: <%# (int) ((DataRowView) container.dataitem) [Field name 1]* (int)  

((DataRowView) Container.DataItem) ["Field Name 2"]%> mean 2 words San multiply.
  Efficiency of the three binding methods above: The Eval method execution invokes the DataBinder.Eval method, and the DataBinder.Eval method performs a late-bound calculation using reflection at

Runtime, resulting in a noticeable decrease in performance. So it can cause a noticeable drop in performance. So the performance of the <%

# ((DataRowView) container.dataitem) [Field name]%> is best in all 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.