About ASP. NET data binding

Source: Internet
Author: User

Data Binding overview
<% # %> Syntax:
Asp. NET introduces a new declaration syntax <% # %>. This syntax is used to bind data on the. ASPX page. All data binding expressions must be included in these characters.
simple attributes (used in the customer's syntax ):
<% # custid %>
set (syntax used for order):

Expression (used in the contact syntax ):
<% # (customer. first name + "" + customer. lastname) %>
method result (syntax used for unsettled balances):
<% # getbalance (custid) %>

Page. databind () and control. databind ()After a specific data source is determined and set for the objects on the. ASPX page, you must bind the data to these data sources. You can use the page. databind or control. databind MethodBind data to a data source.
These two methods are used in a similar way. The main difference is that after the page. databind method is called, all data sources are bound to their server controls. No data is presented to the control before the databind method of the Web server control is explicitly called or the page-level page. databind method is called. Generally, you can call page. databind (or databind) from the page_load event ).


Data Binding list control
A list control is a special web server control that can be bound to a set. You can use these controls to display data rows in a custom template format. All list controls expose datasource and datamember attributes, which are used to bind to a collection.
These controls can bind their datasource property to any set that supports the ienumerable, icollection, or ilistsource interface.
Repeater. datasource attributes:Obtains or sets the data source that provides data for the fill list..

Repeater control
The Repeater control is a templated data binding list.. The Repeater control is "invisible"; that is, it does not have any built-in layout or style. Therefore, you must explicitly declare all HTML layout tags, format tags, and style tags in the template of the control.
BelowCodeThe example shows how to useRepeaterThis list control displays data:

 <%  @ Page Language  =  "  C #  "     %>  
<% @ Import namespace = " System. Data " %>
<% @ Import namespace = " System. Data. sqlclient " %>

< Script runat = " Server " >
Void Page_load (Object sender, eventargs E)
{
Sqlconnection CNN = New
Sqlconnection ( " Server = (local); database = pubs; Integrated Security = sspi " );
Sqldataadapter da = New Sqldataadapter ( " Select * from authors " , CNN );
Dataset DS = New Dataset ();
Da. Fill (DS, " Authors " );
Repeater1.datasource = DS. Tables [ " Authors " ];
Repeater1.databind ();
}
</ Script >
< Html >
< Body >
< Form ID = " Webform2 " Method = " Post " Runat = " Server " >
< ASP: repeater ID = " Repeater1 " Runat = " Server " >
< Itemtemplate >
<% # Databinder. eval (container. dataitem, " Au_id " ) %> < BR >
</ Itemtemplate >
</ ASP: Repeater >
</ Form >
</ Body >
</ Html >

Example of databinder. Eval usage
<% # Databinder. eval (container. dataitem, "integervalue", "{0: c}") %>
The format string parameter is optional. If the parameter is ignored, databinder. Eval returns the object type value,

// Display two decimal places
<% # Databinder. eval (container. dataitem, "unitprice", "$ {0: F2}") %>

// {0: g} indicates that true or false is displayed.
<Itemtemplate>
<Asp: image width = "12" Height = "12" border = "0" runat = "server"
Alternatetext = '<% # databinder. eval (container. dataitem, "discontinued", "{0: g}") %>'
Imageurl = '<% # databinder. eval (container. dataitem, "discontinued ","~ /Images/{0: g2.16.gif ") %> '/>
</Itemtemplate>

// Conversion Type
(String) databinder. eval (container, "dataitem. p_ship_time_sbm8"). substring (4, 4)
{0: d} date only displays year, month, and day
{0: yyyy-mm-dd} displays year, month, and day by format
{0: c} currency Style
<% # Container. dataitem ("price", "{0: ¥ #,## 0.00}") %>
<% # Databinder. eval (container. dataitem, "company_ureg_date", "{0: yyyy-m-d}") %>
Specifier Type format output (passed double 1.42) output (passed int-12400)
C currency {0: c} $1.42-$12,400
D decimal {0: d} system. formatexception-12400
E scientific {0: e} 1.420000e + 000-1.2420.e + 004
F fixed point {0: f} 1.42-12400.00
G General {0: g} 1.42-12400
N number with commas for thousands {0: n} 1.42-12,400
R round trippable {0: R} 1.42 system. formatexception
X hexadecimal {0: X4} system. formatexception cf90

{0: d} date only displays year, month, and day
{0: yyyy-mm-dd} displays year, month, and day by format

The style depends on the settings in Web. config.
{0: c} or {0: 00000,000.00} currency style standard British currency Style
<System. Web>
<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8" Culture = "En-us" uiculture = "En-us"/>
</System. Web>
Displayed as listen 3,000.10

{0: c} or string. Format ("{0: c}", price); Chinese currency Style
<System. Web>
<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8" Culture = "ZH-CN" uiculture = "ZH-CN"/>
</System. Web>
Shown as ¥3,000.10

{0: c} or string. Format ("{0: c}", price); American currency Style
<System. Web>
<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8"/>
</System. Web>
Shown as $3,000.10

Reference: http://support.microsoft.com/kb/307860/zh-cn
Thank you!

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.