A brief talk on C # eval () function

Source: Internet
Author: User

<%# Container.dataitemindex + 1%>//implement auto-numbering
<%# DataBinder.Eval (Container.DataItem, "[n]")%>
commonly used methods (three of the best performance)
<%# DataBinder.Eval (Container.DataItem, "ColumnName")%>
<%# DataBinder.Eval (Container.DataItem, "ColumnName", null)%>
<%# DataBinder.Eval (Container, "Dataitem.columnname", null)%>
Other usage
<%# ((DataRowView) container.dataitem) ["ColumnName"]%>
<%# ((DataRowView) Container.DataItem). row["ColumnName"]%>
<%# ((DataRowView) container.dataitem) ["Adtitle"]%>
<%# ((DataRowView) Container.DataItem) [n]%>
<%# ((DbDataRecord) Container.DataItem) [0]%>
<%# ((custom type) Container.DataItem). property. ToString ()%>//If the property is a string type, do not use ToString ()
DataBinder.Eval Usage Examples
<%# DataBinder.Eval (Container.DataItem, "IntegerValue", "{0:c}")%>
Formatting string parameters is optional. If the parameter is omitted, DataBinder.Eval returns the value of the object type.

Show two decimal places
<%# DataBinder.Eval (Container.DataItem, "UnitPrice", "${0:f2}")%>

{0:g} represents display True or false
<ItemTemplate>
<asp:image width= "height=" border= "0" runat= "Server"
Alternatetext= ' <%# DataBinder.Eval (Container.DataItem, "discontinued", "{0:g}")%> '
Imageurl= ' <%# DataBinder.Eval (Container.DataItem, "discontinued", "~/images/{0:g}.gif")%> '/>
</ItemTemplate>

Conversion type
(string) DataBinder.Eval (Container, "DATAITEM.P_SHIP_TIME_SBM8")). Substring (bis)
{0:D} date only displays month date
{0:YYYY-MM-DD} displays month date by format
{0:C} currency style
<% #Container. DataItem ("Price", "{0:¥#,# #0. xx}")%>
<%# 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.240000e+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 month date
{0:YYYY-MM-DD} displays month date by format

Style depends on settings in Web. config
{0:c} or {0:£0,000.00} currency style standard UK currency style
<system.web>
<globalization requestencoding= "Utf-8" responseencoding= "Utf-8" culture= "en-us" uiculture= "en-US"/>
</system.web>
Display as £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>
Display 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>
Display as $3,000.10

1,

<asp:hyperlink runat= "Server" id= "hlusername" text= ' <% #DataBinder. Eval (Container.DataItem, "UserName")%> ' Navigateurl= ' <%# string. Format ("userview.aspx?uid={0}", Eval ("UserId"))%> '/>

2, you can invoke intrinsic functions and custom functions in CS:

(1):

(2):

In CS:

Protected string Test (string s)

{return s.toupper ();}

In ASPX:

3, not placed in the control, directly call the function

<%= DateTime.Now.ToLongDateString ()%>

Formatted Date:

<%# Eval ("Finishtime", "{0:YYYY-MM-DD}")%>
This procedure is often used when binding data: <%# DataBinder.Eval (Container.DataItem, "xxxx")%> or <%# DataBinder.Eval (Container, " Dataitem.xxxx ")%> Microsoft This method is more efficient, but I'm not used to it, I'm used to the last one.

<%# ((DataRowView) container.dataitem) ["xxxx"]%> Use this method to first import the namespace System.Data in the foreground page, otherwise an error message will be generated.

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

DataBinder.Eval () can be text= the method, "<%# Pbnumber (DataBinder.Eval (Container.DataItem," Photobookid "))%> Backstage Code:
Protected string Pbnumber (object pbid)
{

string str = "[" + Convert.ToString (pbc.getinpbkpnum ((int) pbid)) + "] Zhang";

return str;
}

DataBinder.Eval can also be judged by choice, as in the case of sex:

<asp:templatecolumn headertext= "Gender" >
<ItemTemplate>
<%# Dgformatsex (convert.tostring (DataBinder.Eval (Container.DataItem, "XB"))%>
</ItemTemplate>
</asp:TemplateColumn>

Dgformatsex method defined in CS
Protected string Dgformatsex (String xb)
{
if (XB = = "1")
return "male";
Else
return "female";
}

DataBinder.Eval usage Examples

Show two decimal places
<%# DataBinder.Eval (Container.DataItem, "UnitPrice", "${0:f2}")%>

{0:g} represents display True or false
<ItemTemplate>
<asp:image width= "height=" border= "0" runat= "Server"
Alternatetext= ' <%# DataBinder.Eval (Container.DataItem, "discontinued", "{0:g}")%> '
Imageurl= ' <%# DataBinder.Eval (Container.DataItem, "discontinued", "~/images/{0:g}.gif")%> '/>
</ItemTemplate>

Conversion type
(string) DataBinder.Eval (Container, "DATAITEM.P_SHIP_TIME_SBM8")). Substring (bis)

{0:D} date only displays month date
{0:YYYY-MM-DD} displays month date by format
{0:C} currency style

This article assumes that you already understand the mechanism of ASP. NET 1.1 data binding (especially the local variable of container), where the main analysis of ASP 2.0 data bindings is customized for those improvements.

The data-binding function of ASP. 2.0 () simplifies the ASP 1.1 cryptic Container.DataItem, such as data-binding expressions:

<%# (Container.DataItem as DataRowView) ["ProductName"]. ToString ()%>

ASP. NET 1.1 simplified to: (removed type designation, eval implemented by reflection, this article no longer elaborated)

<%# DataBinder.Eval (Container.DataItem, "ProductName"). ToString ()%>

The ASP. NET 2.0 is also simplified to remove the container local variables:

<%# Eval ("ProductName")%>

So, how does page.eval () know that "ProductName" is the attribute of that data, that Container.DataItem really disappears?

Eval () is a method of the parent class of page TemplateControl

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

1. Build the DataItem Container stack:

In Control.DataBind (), this ensures that the DataItem container of the child 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); <--pressing the DataItem into the stack
}
Try
{
if (raiseondatabinding)
OnDataBinding (Eventargs.empty);

Databindchildren (); <--Binding child controls
}
Finally
{
if (foundDataItem)
Page.popdataitemcontext (); <--the DataItem pops up the stack
}
}
}

2. Get DataItem Container

public class Page
{
public Object Getdataitem ()
{
...
return This._databindingcontext.peek (); <--reads the DataItem Container at the top of the stack, which is the DataItem Container that is being bound
}
}

3. Templatecontrol.eval ()

public class TemplateControl
{
Protected string Eval (string expression, string format)
{
Return DataBinder.Eval (Page.getdataitem (), expression, format);
}
}

   Conclusion:

From the above see Page.eval () in the calculation or reference Container.DataItem, but this DataItem through the DataItem container stack automatically calculated. I think Page.eval () seems to be simplifying the problem, actually making it more mysterious.

A brief talk on C # eval () function

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.