ASP. net2.0 Data Binding

Source: Internet
Author: User

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

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.