ASP. NET data binding
1. <% = C # code %> // call the code to hide the method, attribute, or field of the page.
Generally, there are many calling attributes and Methods. Note that the scope of the called attributes, methods, or fields must be accessible on the ASPX page.
Sample Code (ASPX): <% = Property %>
In (CS) is: public string Property {get {return "This is a Property ";}}
The attribute is used in this way, and the method is similar to the field.
2. <% # data binding expression %> // used in the List Control
Method 1: <% # Eval ("FirstName") %>
Method 2: <% # DataBinder. Eval (Container. DataItem, "SecondName") %>
The following is the source code for debugging. You can copy it and check it out.
On the ASPX page:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "DataBindEx. _ Default" %>
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Collections. Generic" %>
<! 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> </title>
</Head>
<Body>
<Form runat = "server">
<Div>
<% = Property %>
<Br/>
<Asp: TextBox ID = "TextBox1" Text = "This is TextBox of serverClient" runat = "server"> </asp: TextBox>
<Br/>
<% = Method () %>
<Br/>
<Br/>
<Asp: Label ID = "Label1" runat = "server"> <% = TextBox1.Text %> </asp: Label>
<Br/>
<% = (Property + "" + Method () %>
</Div>
<Div>
<Asp: Repeater ID = "Repeater1" runat = "server" OnItemDataBound = "RptAllOnItemDataBound">
<HeaderTemplate>
This is Header <br/>
</HeaderTemplate>
<ItemTemplate>
FirstName: <% # Eval ("FirstName") %>
SecondName: <% # DataBinder. Eval (Container. DataItem, "SecondName") %>
FullName: <% # (Container. DataItem as DataBindEx. Person). FullName %>
<Asp: Literal ID = "Others" runat = "server"> </asp: Literal>
<Br/>
</ItemTemplate>
<FooterTemplate>
This is footer <br/>
</FooterTemplate>
</Asp: Repeater>
</Div>
</Form>
</Body>
</Html>
On the CS page:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. MobileControls;
Namespace DataBindEx
{
Public class Person
{
Public string FirstName
{
Get;
Set;
}
Public string SecondName
{
Get;
Set;
}
Public string FullName
{
Get
{
Return FirstName + SecondName;
}
}
}
Public partial class _ Default: System. Web. UI. Page
{
Public string Property
{
Get
{
Return "This is a Property ";
}
}
Protected void Page_Load (object sender, EventArgs e)
{
String str = TextBox1.Text;
Person per = new Person ();
Per. FirstName = "Liu ";
Per. SecondName = "Mingfeng ";
Person per1 = new Person ();
Per1.FirstName = "Lin ";
Per1.SecondName = "Wang ";
Person per2 = new Person ();
Per2.FirstName = "Chen ";
Per2.SecondName = "Ren Feng ";
List <Person> list = new List <Person> ();
List. Add (per );
List. Add (per1 );
List. Add (per2 );
Repeater1.DataSource = list;
Repeater1.DataBind ();
}
Protected void RptAllOnItemDataBound (object sender, RepeaterItemEventArgs e)
{
Person pe = (Person) e. Item. DataItem;
Literal items = e. Item. FindControl ("Others") as Literal;
If (pe! = Null)
Switch (pe. FirstName)
{
Case "Liu ":
Pipeline. Text = "Liu likes playing ";
Break;
Case "Lin ":
Text = "Lin likes playing chess ";
Break;
Default:
Gradient. Text = "Chen likes c #";
Break;
}
}
Protected string Method ()
{
Return "This is a Method ";
}
}
}