Copy codeThe Code is 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
{
// Step
// 1. There must be a general method to spell strings like private string MakeString ()
// 2. There is a way to obtain data in this total method, such as private DataTable FetchData ()
Protected void Page_Load (object sender, EventArgs e)
{
Response. Write (MakeString ());
}
/* This is the method for assembling strings */
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 ()
{
// This is assembly, and you can also obtain it by 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;
}
}