Implementation Code of memory fragments bound to ASP. NET data

Source: Internet
Author: User

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 ";
}
}
}

Related Article

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.