asp.net use for loop to implement DataList display function _ Practical skills

Source: Internet
Author: User
server controls, while convenient to use, also sacrifice performance, and sometimes seem accuse. Hope that advanced friends more advice.
Copy Code code as follows:

<summary>
Engineering performance--with a for loop instead of the DataList multiple-column display, get 2 rows of four columns of the table, requires a memory table of 8 rows of data
</summary>
private void Gcyjshow ()
{
StringBuilder sb = new StringBuilder ();
PRODUCTBLL PB = new Productbll ();
DataTable dt = PB. Getproductbymoreconditions ("Project Performance", "Project Performance", "Enable");
int count = dt. rows.count;//Number of rows

Sb. Append ("<table style=" display:inline; Vertical-align:middle ' cellpadding= ' 5 ' align= ' center ' > ')//two rows of four-column tables
Sb. Append ("<tr>");
for (int i = 0; i < count; i++)
{
DataRow dr = dt. Rows[i];
string Strid = dr["ProdId"]. ToString ();
String strurl = dr["Picture"]. ToString ();
String strName = dr["ProdName"]. ToString ();
if (Dr!= null)
{
Sb. Append ("<td>");
Sb. Append ("<table border= ' 0 ' cellspacing= ' 0 ' cellpadding= ' 0 ' width= ' 100% ' >");
Sb. Append ("<tr>");
Sb. Append ("<td bgcolor= ' #ffffff ' >");


if (!string. IsNullOrEmpty (Strid) &&!string. IsNullOrEmpty (strName) &&!string. IsNullOrEmpty (strURL))
{
Sb. Append ("<a title= '/" + StrName + "");
Sb. Append ("href= ' gcyjshow.aspx?strid=" + Strid + "' >");
Sb. Append ("Sb. Append (strName + "'");
Sb. Append ("src= '/photo/");
Sb. Append (strURL + "' >");
Sb. Append ("</a>");
}
Sb. Append ("</td>");
Sb. Append ("</tr>");
Sb. Append ("<tr>");
Sb. Append ("<td>");
Sb. Append ("<div style=" padding-left:15px; Background:url (images/index_r1_c9.jpg) no-repeat ' > ');
if (!string. IsNullOrEmpty (StrName))
{
if (Strname.length > 7)
Sb. Append (strname.substring (0, 7) + "...");
Else
Sb. Append (StrName);
}
Sb. Append ("</div>");
Sb. Append ("</td>");
Sb. Append ("</tr>");
Sb. Append ("</table>");
Sb. Append ("</td>");
}
We output the 4 rows of data in the memory table as a row of 4 columns
If the number of rows in the memory table is count<=4, which is less than the row column we want to display, it automatically terminates the loop and does not need to process TR
When the data equals four columns (that is, one row) and is larger than a row-wrap
if (i = = 3 && count > 4)
{
Sb. Append ("</tr><tr>");
}
if (i = = 7)////When the loop reaches 8 times, a table of two rows four columns is completed, and the loop is terminated at this time
{
Break
}
}

Sb. Append ("</tr>");
Sb. Append ("</table>");
Lblgcyj.text = sb. ToString ()////label label to output
}
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.