asp.net Dynamic Generation Table _ Practical skills

Source: Internet
Author: User
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data;
Using System.Text;

public partial class Default2:System.Web.UI.Page
{
Steps

1. There's a general way to spell a string like a private string makestring ()
2. In this general approach, there's a way to get the data, like a private DataTable fetchdata ()
protected void Page_Load (object sender, EventArgs e)
{
Response.Write (Makestring ());
}

* * This is the method of assembling the string.
private String makestring ()
{
DataTable dtdata = Fetchdata ();
StringBuilder strcontent = new StringBuilder ();
String strformate = "<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr > ";
Strcontent.append ("<table>");
foreach (DataRow row in dtdata.rows)
{
Strcontent.appendformat (Strformate, row[0). ToString (), row[1]. ToString (), row[2]. ToString ());
}
Strcontent.append ("</table>");
return strcontent.tostring ();
}

Private DataTable Fetchdata ()
{
It's assembled, and you can get it yourself.
DataTable dtdata = new DataTable ();
DTDATA.COLUMNS.ADD ("C1");
DTDATA.COLUMNS.ADD ("C2");
DTDATA.COLUMNS.ADD ("C3");
DTDATA.ROWS.ADD (new string[] {"1-1", "1-2", "1-3"});
DTDATA.ROWS.ADD (new string[] {"2-1", "2-2", "2-3"});
DTDATA.ROWS.ADD (new string[] {"3-1", "3-2", "3-3"});
DTDATA.ROWS.ADD (new string[] {"4-1", "4-2", "4-3"});
DTDATA.ROWS.ADD (new string[] {"5-1", "5-2", "5-3"});

return dtdata;
}
}

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.