Asp.net dynamically generates a table on the page

Source: Internet
Author: User

The project is still being debugged. To sum up, it is always uncomfortable. After all, it is not complete yet. So now I want to record some troublesome knowledge points used in this system in sequence, so that others can check it out (this is just a waste of hard work to find information from the Internet ...)

First, define a table control on the page and name it autotable. Then add the following code to an event (I click an event on the button, and you can load the event on the page ..)

I'll explain a few concepts later, or you will faint.

A table consists of N rows, that is, tablerow. Each row is composed of N units. It can also be called a cell, that is, tablecell.

Therefore, the basic algorithm used to dynamically generate tables is to constantly add cells to each object. Note that each addition is a new cell and a new tablecell, otherwise, the final result is that no matter how many times you loop, you only add a cell (tablecell ). Then, add tablerow with tablecell added to autotable. This is the same as tablecell, and a new one is required for each addition. Finally, a dynamic table addition result is displayed.

 

[Code = C #]

 

// Generate a table <br/> tablerow Trow; // define a data table row <br/> tablecell Tcell; // define a data table <br/> label lbltitle; // define the title label <br/> label lblchapter; // define the chapter label <br/> textbox txtvalue; // define the text box used to enter the score <br/> numrows = 3; <br/> numcells = 3; <br/> // you can specify several rows and columns. <br/> for (INT irow = 0; irow <= numrows; irow ++) <br/>{< br/> Trow = new tablerow (); // a new row is generated. <br/> for (INT jcol = 0; jcol <= numcells; jcol ++) <br/>{< br/> // you can add Table header, table header is difficulty level <br/> If (irow = 0) <br/>{< br/> // execute the following code when the first line is used <br/> If (jcol = 0) <br/>{< br/> // when it is the first column of the First row <br/> Tcell = new tablecell (); // new generates a cell <br/> lbltitle = new label (); // This is a lable used by myself. For more information, see <br/> lbltitle. TEXT = "Difficulty coefficient:"; // assign a value to the label <br/> lbltitle. width = 80; // set the width <br/> Tcell. controls. add (lbltitle); // Add the label to the cell <br/> Trow. cells. add (Tcell); // Add the cell to tablerow <br/>}< br/> else <BR/>{< br/> // if it is not the first column: <br/> Tcell = new tablecell (); <br/> lbltitle = new label (); <br/> lbltitle. TEXT = jcol. tostring (). trim (); <br/> Tcell. horizontalalign = horizontalalign. center; <br/> Tcell. controls. add (lbltitle); <br/> Trow. cells. add (Tcell); <br/>}< br/> else <br/>{< br/> // If the column is the first, add the row title. <Br/> If (jcol = 0) <br/>{< br/> Tcell = new tablecell (); <br/> lblchapter = new label (); <br/> lblchapter. TEXT = strchapter [irow-1]. tostring () + ":"; <br/> lblchapter. width = 80; <br/> Tcell. controls. add (lblchapter); <br/> Tcell. horizontalalign = horizontalalign. center; <br/> Trow. cells. add (Tcell ); <br/>}< br/> else <br/>{< br/> // Add content if it is not the first column of the First row <br/> Tcell = new tablecell (); <br/> txtvalue = New Textbox (); <br/> txtvalue. id = "TXT" + irow. tostring () + jcol. tostring (); // set the ID for the control, because I will use this control later. <Br/> txtvalue. TEXT = "0"; <br/> txtvalue. TEXT = txtvalue. text. trim (); <br/> Tcell. controls. add (txtvalue); <br/> Trow. cells. add (Tcell); <br/>}< br/> autotable. rows. add (Trow); // Add tablerow <br/>}< br/>

[/Code]

 

 

Figure:

 


Of course, it is also troublesome to get the data in the table. After some learning, Ajax is used, and updatepanel is used in vs2008.

But you still have another good way to tell me, thank you first!

 

New Blog

 

 

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.