Eval date formatting Method

Source: Internet
Author: User

Address: http://hi.baidu.com/isolated/item/3cdfcbdd3f50c4f692a97455

<td><%# Eval("B_TARGET_DATE", "{0:yyyy-MM}")%></td>

<% # Eval ("finishtime", "{0: yyyy-mm-dd}") %>
This program is often used when binding data: <% # databinder. eval (container. dataitem, "XXXX") %> or <% # databinder. eval (container, "dataitem. XXXX ") %> Microsoft is more efficient, but I am not commonly used. I am used to the previous method.

<% # (Datarowview) container. dataitem) ["XXXX"] %> to use this method, you must first import the namespace system. Data on the foreground page. Otherwise, an error message is generated.

<% @ Import namespace = "system. Data" %>

Databinder. eval () can be customized. Text = '<% # pbnumber (databinder. eval (container. dataitem, "photobookid") %> background code:
Protected string pbnumber (Object pbid)
{

String STR = "[" + convert. tostring (PBC. getinpbkpnum (INT) pbid) + ";

Return STR;
}

Databinder. Eval can also be selected, for example, gender:

<Asp: templatecolumn headertext = "gender">
<Itemtemplate>
<% # Dgformatsex (convert. tostring (databinder. eval (container. dataitem, "XB") %>
</Itemtemplate>
</ASP: templatecolumn>

Define the dgformatsex method in CS
Protected string dgformatsex (string XB)
{
If (XB = "1 ")
Return "male ";
Else
Return "female ";
}

Example of databinder. Eval usage

// 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

This article assumes that you have understood the Data Binding Mechanism of ASP. NET 1.1 (especially the local variable container). Here we mainly analyze the improvements made by ASP 2.0 data binding.

The data binding function eval () of ASP. NET 2.0 simplifies ASP 1.1's mysterious container. dataitem, such as the data binding expression:

<% # (Container. dataitem as datarowview) ["productname"]. tostring () %>
ASP. NET 1.1 is simplified as follows: (Eval is implemented through reflection without specifying the type. This article will not elaborate on this)

<% # Databinder. eval (container. dataitem, "productname"). tostring () %>
ASP. NET 2.0 is simplified to remove the local variable container:

<% # Eval ("productname") %>
So how does page. eval () know that "productname" is the attribute of the data, that is, does container. dataitem really disappear?

Eval () is the templatecontrol method of the parent class of page.

Templatecontrol. eval () can automatically calculate the container, which is obtained from a databindingcontext: Stack stack.

1. Create a dataitem container stack:

Create in control. databind () to ensure that the dataitem iner of the sub-control is always at the top of the stack.

Public class control
{
Protected virtual void databind (bool raiseondatabinding)
{
Bool founddataitem = false;
If (this. isbindingcontainer)
{
Object o = databinder. getdataitem (this, out founddataitem );
If (founddataitem)
Page. pushdataitemcontext (o); <-- press dataitem into the stack
}
Try
{
If (raiseondatabinding)
Ondatabinding (eventargs. Empty );

Databindchildren (); <-- bind a child Control
}
Finally
{
If (founddataitem)
Page. popdataitemcontext (); <-- popdataitemcontext
}
}

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.