Asp. NET data binding in general
1, <%= C # code%>//calling code-behind page methods, properties, or fields
This is typically called by a number of properties and methods, pay attention to the invocation of the properties, methods or fields of the scope, must be accessible in the ASPX page can be.
code example (ASPX):<%=property%>
In (CS) are: public string property{get {return ' This is a property ';}}
Properties are used in this way and are similar to the use of methods and fields.
2, <% #数据绑定表达式%>//is used in the list control
How to use one: <%# Eval ("FirstName")%>
Use mode two: <%# DataBinder.Eval (Container.DataItem, "Secondname")%>
Attached below I debug the source code, you can copy the past to see
On the ASPX page:
Copy Code code 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 ">
<title></title>
<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>
On CS page:
Copy Code code 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= "Ming Feng";
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 lit = E.item.findcontrol ("others") as Literal;
if (PE!=null)
Switch (PE. FirstName)
{
Case "Liu":
Lit. Text = "Liu likes playing ball";
Break
Case "Lin":
Lit. Text = "Lin likes playing chess";
Break
Default
Lit. Text = "Chen likes C #";
Break
}
}
Protected String Method ()
{
Return ' This is a method ';
}
}
}