Customized table display method

zhaozj2021-02-16  60

When designing a website, we often need to display data in a database on a page. In the traditional ASP, you must create an HTML table that loops in a recordset (RECORDSET) to get each record and create a tab for each record. In each record, you must loop to get each field and create a tag for it. You have to manually handle many details, such as creating columns, setting size of each field and setting colors. Also, if you just want to display certain fields of the record set, you must use the IF statement to see each field to see if you want to show it. Asp.net not only makes all of these work simple, but it also allows you to fully control the form of display.

First, drag and drop a DataGrid to a web form. Then write some data access code to get data from a database. Using the following code we can bind the data in the DataTable named MDTUsers to DGDUsers this DataGrid:

DGDUSERS.DataSource = MDTUSERS

DGDUSERS.DATABIND ()

figure 1.

Use a DataGrid to display the data

We set DataGrid's DataSource to DataTable, and then call the Database to bind the data. Such ASP.NET automatically creates a form for all data in DataTable. It arranges each column and displays the field name in the DataTable as a column header.

You can also modify the code to make DataGrid displays (see Figure 1) in the way you need (see Figure 1). For example, you can only display certain fields of DataTable on a web form, or you can use a different column header instead of the field name used by DataTable. .NET allows you to fully control the display of DataGrid.

Select the HTML Tab button at the bottom of the page to display the HTML code of the page. Find the tab. You need to add some code between the and tag. For example, the following code changes the background color of each row in alternate:

BackColor = "CORAL">

Add this row code to set the background color of the title:

Now, suppose you don't want to display all the fields in the DataTable. First, you need to add an AutoGenerateColumns property to the tab and set it to false:

Runat = "server" autogeneratecolumns = "false">

By default, AutoGenerateColumns is set to TRUE, which is telling .Net, let it generate all columns. If you want to control which columns are generated, set this property to false. Then, you need to add a code block to tell .Net you want to show. For example, the following code only displays the Last Name, First Name, and Email address fields:

Headertext = "Last Name">

Headeertext = "first name">

"Headertext =" email ">

You use a tag for each column you want to display. The DataField property is the field name in the DataTable. Headertext This property allows you to control the title name used for the column.

Your DataGrid's complete HTML code should look like this:

Runat = "server" autogeneratecolumns = "false">

BackColor = "CORAL">

BackColor = "crimson">

Headertext = "Last

Name>

Headertext = "first

Name>

Headertext = "email">

ASP.NET simplifies many common website design work. By using a few lines of code you can display the data of the entire table, and it can also display data as you want.

转载请注明原文地址:https://www.9cbs.com/read-23843.html

New Post(0)