Recently, the project was developed in dnn. Several data-bound applications were published:
<Tr runat = "server" visible = '<% # eval ("Description"). tostring () <> "" %>'>
<TD style = "width: 240px; border-bottom: # efefef 2px dashed; font-weight: bolder;" valign = "TOP">
Description </TD>
<TD style = "width: 800px; border-bottom: # efefef 2px dashed">
<Asp: Label runat = "server" id = "summary_bolder" style = "font-weight: bolder" visible = '<% # eval ("Description "). tostring () <> "" %> '> Summary: <br/> </ASP: Label>
<Asp: Label runat = "server" id = "label_nbsp1" visible = '<% # eval ("Description "). tostring () <> "" %> '> & nbsp; </ASP: Label> <asp: label id = "descriptionlabel" runat = "server" text = '<% # eval ("Description ") & "<br/>" %> 'visible = '<% # eval ("Description "). tostring () <> "" %> '> </ASP: Label>
<Asp: Label runat = "server" id = "extendeddescription_bolder" style = "font-weight: bolder" visible = '<% # eval ("extended_description "). tostring () <> "" %> '> extended Description: <br/> </ASP: Label>
<Asp: Label runat = "server" id = "label_nbsp2" visible = '<% # eval ("extended_description "). tostring () <> "" %> '> & nbsp; </ASP: Label> <asp: label id = "extended_description" runat = "server" text = '<% # eval ("extended_description") %> 'visible =' <% # eval ("extended_description "). tostring () <> "" %> '> </ASP: Label>
</TD>
</Tr>
Explanations:
1. The visible attribute of TR in the first row controls that if a field (in this example: Description) in the database is empty, a row of table is not displayed! Note: syntax-related data binding at the front-end, VB. net and cs.net are different. For example, if the page uses C #, It is case sensitive and the method needs to be "()". The tostring function of VB can be "()". The other visible controls below are similar!
2. Note that two carriage returns are added to the binding of descriptionlabel. Only one-way binding (eval) is supported, but not two-way binding (BIND) is supported )!
3. Also: Let's look at Scott's binding. If your binding logic is complicated, you can write the background logic:
<Asp: templatefield headertext = "days on the job">
<Itemtemplate>
<% # Displaydaysonjob (northwind. employeesrow) (system. Data. datarowview) container. dataitem). Row) %>
</Itemtemplate>
<Itemstyle horizontalalign = "center"/>
</ASP: templatefield>
The displaydaysonjob method in the background is:
Protected string displaydaysonjob (northwind. employeesrow employee)
{
// Make sure that hireddate is not empty ...... If it is null, "unknown" is returned"
If (employee. ishiredatenull ())
Return "unknown ";
Else
{
// Returns the number of days between the current date and hiredate.
Timespan Ts = datetime. Now. Subtract (employee. hiredate );
Return ts. Days. tostring ("#, #0 ");
}
}
In this way, we can write data at will!
This tutorial isScott Mitchell's ASP. NET 2.0 data tutorial 12: Using templatefield in the gridview Control