Use ASP. NET to display relevant data of the master-slave relationship table on the same webpage

Source: Internet
Author: User

Method:

For each Table in DataSet, if there is a sub-Table, the field name row and each record row of this Table are displayed as tables with only one row on the webpage, set ImageButton on the first column of the record row Table so that the program can control the Table or Tables formed by the record row of the corresponding sub-Table. For each record row, the sub-table has a corresponding record row. If the sub-table has a sub-table, the sub-Table repeats the above process, the field name row and corresponding record row of the sub-Table are displayed as a Table with only one row on the webpage, and an ImageButton is placed on the second column of the Table record row, on the contrary, the field name row of the sub-Table and the corresponding record row are displayed as a Table on the webpage. You can use the Visible attribute of Table to control the display of corresponding record rows in the sub-Table. Obviously, recursive calls can be used to implement this process.

Naming rules for Tables and ImageButtons IDs:
By clicking ImageButton, the program controls the display and hiding of the corresponding Tables on the webpage. The naming of these ImageButton. id and Table. id requires certain rules.
For the first master table in DataSet:
The id of the webpage Table generated by the field row is t0.
The IDS of Tables generated by record rows are t1, t2 ,... tn, The ImageButton corresponding to the Tables. the id is named e1, e2 ,... en, n: number of records in the master table
For sub-tables in DataSet:
The record rows in the sub-Table are subordinate to a record row in the master Table. Therefore, the Web Tables generated by these record rows is also affiliated to the Table on the webpage generated by a record row in the master Table, for example, Tables under t2 is named t2-0, t2-1, t2-3 ..., the t2-0 is the Table generated on the web page for the field name row of the child Table. id, and so on. If the sub-table has no sub-table, that is, the last sub-table, the t2-0 is the Table generated on the web page by the field name row containing the child Table and the corresponding child Table record row. id.
ImageButton. id corresponding to these table. IDs are named e2-1, e2-2,... respectively ,....

Estimation of the length of the Table field on the webpage:
Each Table in DataSet generates some Tables on the webpage. The external form of these Tables should be the same. That is, the same Cell length of Tables should be the same. The SetCellSize function is used to calculate the average number of characters in each field of the able and the number of characters in the field name. If a field's record or field name is an ASCII character, the number of characters is halved. With this data, you can estimate the display length of the corresponding Table fields on the webpage.

The following is the SetCellSize program, which is easy to understand.
Function SetCellSize (myTable as DataTable)
Dim myRow as DataRow
Dim I, j, k, m as integer
Dim aa () as integer
Dim myBool as Boolean

M = myTable. columns. count-1
Redim aa (m)
For I = 0 to m
Aa (I) = 0
Next

For each myRow in myTable. rows calculate the average length of each field
For I = 0 to myTable. columns. count-1
Dim mystr as string
Mystr = myRow (myTable. columns (I). tostring
J = len (mystr)
If j> 0 then
MyBool = true
For k = 1 to J' determine whether each item in the able contains Chinese Characters
Dim str1 as char = mid (mystr, k, 1)
If ascw (str1)> 255 then' has non-ASCII characters
MyBool = false
Exit
End if
Next
If myBool then j = (j/2 + 0.5) 'is an ASCII character, and the string length is halved.
Aa (I) + = j
End if
Next
Next myRow

K = myTable. rows. count
For I = 0 to m
Aa (I) = aa (I)/K' average length of each column of the able
Next

For I = 0 to myTable. columns. count-1 'for each field name
Dim str2 as string = myTable. columns (I). columnname

J = len (str2)
If j> 0 then
MyBool = true
For k = 1 to J' determine whether the field name contains Chinese Characters
Dim str1 as char = mid (str2, k, 1)
If ascw (str1)> 255 then' has non-ASCII characters
MyBool = false
Exit
End if
Next
If myBool then j = (j/2 + 0.5) 'ascii character, the string length is halved
If j> aa (I) then aa (I) = j
End if
Next
SetCellSize = aa
End Function

Main Program:
The subroutine ShowTables sets some initial values and then calls the subroutine ShowChildRows.
Parameters of the subroutine ShowChildRows are described as follows:
Rows: it is a DataRow array. When ShowChildRows is called for the first time, it is all the record Rows of the DataTable. When ShowChildRows is called recursively later, it is some record rows of the child table related to a row in the parent table.
MyTable: The able to which the Rows belongs. The program uses its Columns, that is, the field name row.
Aa: one-dimensional integer array returned by the function subroutine SetCellSize.
Spaces: integer parameter. It is used to display Tables on a webpage. Several empty cells should be set on the left of these Tables to display the Table affiliation.
Signal: string parameter, id value of ImageButton, used to generate related Tables and ImageButtons IDs.
To add a Table control to a webpage, a Form control with the id of form1 should be added to the webpage.
There are three steps to create a Table dynamically. First, create a TableCell object, that is, a cell in the row. There are two ways to add the content of a cell: Set the Text attribute or Control the content to TableCell. add Controls to the Controls set, and add the ImageButton control to some Cells in the program. Next, create TableRow to represent the rows in the table and add the previously created TableCell object to the Cells set of TableRow. Finally, add TableRow to the Table control's Rows collection.
The following is a program:
Sub ShowTables (mySet as DataSet)
Dim spaces as integer = 0
Dim aa () as integer
Dim I, d as integer
Dim signal as string = ""

Dim myTable as dataTable = mySet. tables (0)
Dim rows () as DataRow
D = myTable. rows. count-1
Redim rows (d)

For I = 0 to myTable. rows. count-1
Rows (I) = myTable. rows (I)
Next

Aa = SetCellSize (myTable)
Call ShowChildRows (rows, aa, myTable, spaces, signal)
End Sub

Sub ShowChildRows (rows () as DataRow, aa () as integer, myTable as DataTable, spaces as integer, signal as string)
Dim I, j, k, m, leng as integer
Dim fontsize as integer = 10

Dim myRow as DataRow
Dim myCol as DataColumn

Dim testTable as Table

Dim Cell as TableCell
Dim Row as TableRow
Dim myBool as Boolean
Dim myimage as ImageButton

Dim ChildRows () as DataRow
Dim ChildTable as DataTable

Dim myRel as DataRelation
Dim bb () as integer

Dim CellStyle as new TableItemStyle
CellStyle. borderwidth = unit. pixel (1)
CellStyle. borderstyle = Borderstyle. solid
CellStyle. wrap = false

If myTable. ChildRelations. count = 1 then 'has a slave table
MyRel = myTable. ChildRelations (0)
ChildTable = myRel. ChildTable
M = ChildTable. Columns. count-1
Redim bb (m)
MyBool = True
Bb = SetCellSize (ChildTable)
End if

TestTable = New Table
TestTable. borderwidth = unit. pixel (1)
TestTable. cellspacing = 0
Testtable. cellPadding = 0
TestTable. font. name = ""
TestTable. font. size = fontunit. point (fontsize)
TestTable. visible = true
If signal <> "" then' is used for recursive calling, the value of Table. id formed by the field name row is assigned.
Leng = len (signal)
TestTable. id = "t" & mid (signal, 2, leng-1) & "-0"
Testtable. visible = false
Else
TestTable. id = "t" & "0" 'indicates the Table. id formed by the field name row.
TestTable. visible = true
End if

Form1.controls. add (testtable)

******************* *

Row = New tableRow
Row. borderwidth = unit. pixel (1)
M = rows. length

If spaces> 0 then
For I = 1 to spaces
Cell = new Tablecell
Cell. applyStyle (CellStyle)
Cell. width = unit. pixel (13)
If not myBool then cell. rowspan = m + 1
Row. cells. add (cell)
Next
End if

If myBool then
Cell = new Tablecell
Cell. applystyle (CellStyle)
Cell. backcolor = color. lightgray
Cell. bordercolor = color. black
Cell. width = unit. pixel (13)
Row. cells. add (cell)
End if

M = myTable. Columns. count-1

For I = 0 to m
Cell = new tableCell
Dim str2 as string
Cell. applystyle (cellstyle)
Cell. backcolor = color. lightgray
Cell. bordercolor = color. black
Cell. text = myTable. columns (I). columnName
Cell. HorizontalAlign = HorizontalAlign. Center
K = (fontsize + 6) * aa (I)
Cell. width = unit. pixel (k)
Row. Cells. add (cell)
Next
Testtable. Rows. add (row)

***************** ****

For I = 0 to rows. length-1
If myBool then
TestTable = New table
TestTable. borderwidth = unit. pixel (1)
TestTable. cellspacing = 0
Testtable. cellPadding = 0
TestTable. font. name = ""
TestTable. font. size = fontunit. point (fontsize)
TestTable. visible = true
If signal <> "" then
TestTable. id = "t" & mid (signal, 2, leng-1) & "-" & (I + 1). tostring
Testtable. visible = false
Else
TestTable. id = "t" & (I + 1). tostring
TestTable. visible = true
End if
Form1.controls. add (testtable)
End if

MyRow = rows (I)

Row = New tableRow
If myBool then
If spaces> 0 then
For k = 1 to spaces
Cell = new Tablecell
Cell. applystyle (cellstyle)
Cell. width = unit. pixel (13)
Row. cells. add (cell)
Next
End if

Cell = New tableCell
Cell. width = unit. pixel (13)
Cell. applystyle (CellStyle)

Myimage = New imagebutton
Myimage. imageurl = "close.gif"
If signal <> "" then
Myimage. id = signal & "-" & (I + 1). tostring
Else
Myimage. id = "e" & (I + 1). tostring
End if
AddHandler myimage. Command, AddressOf ImageButton_Command
Myimage. imagealign = ImageAlign. absmiddle
Cell. controls. add (myimage)
Row. cells. add (cell)
End if

M = myTable. columns. count-1

For j = 0 to m
Cell = new tablecell
Cell. ApplyStyle (CellStyle)
Cell. text = myRow (myTable. columns (j). tostring
K = (fontsize + 6) * aa (j)
Cell. width = unit. pixel (k)
Row. cells. add (cell)
Next
Testtable. rows. add (row)


If myBool then' has a slave table, it is recursively called.
Dim spaces2 as integer
Spaces2 = spaces + 1
ChildRows = myRow. GetChildRows (myRel)
Call ShowChildRows (ChildRows, bb, ChildTable, spaces2, myimage. id)
End if
Next
End Sub

Event Process:

The naming rules are provided in the "Tables and ImageButtons ID naming rules" section. Examples are as follows:
If you click ImageButton whose id is e1, the id of the Table on the corresponding webpage is t1, the corresponding record rows in the child table associated with t1 form an id on the web page as t1-1, t1-2 ,... the Tables of the t1-n, where n is the corresponding number of records on the child Table, And the t1-0 is the Table formed on the web page by the field name row of the child Table. If the imageurlof e1is always close.gif, set it to always writable open.gif, and set the Visible attribute of the t1-0, t1-1,... t1-n to True. .

Article Title: Using Web Services Enhancements to Send SOAP Messages with Attachments

The procedure is as follows:
Sub ImageButton_Command (Sender as object, e as CommandEventArgs)
Dim myControl as Control
Dim str1 as string
Dim leng as integer

Str1 = sender. id
Leng = len (str1)
Str1 = "t" & mid (str1, 2, leng-1 )&"-"

If Sender. ImageUrl = "close.gif" then
Sender. ImageUrl = "open.gif"
For each myControl in form1.controls
Dim pos1 as integer = instr (leng + 2, myControl. id ,"-")
If left (myControl. id, leng + 1) = str1 and pos1 = 0 then myControl. visible = true
Next
Else
Sender. ImageUrl = "close.gif"
For each myControl in form1.controls
Dim str0 as string = myControl. id
If left (myControl. id, leng + 1) = str1 then
Dim dTable as table
Dtable = ctype (myControl, table)
If dtable. controls. count> 0 then
Dim rowControl as control
For each rowControl in dtable. controls
Dim drow as tablerow
Drow = ctype (rowControl, tablerow)
If drow. controls. count> 0 then
Dim cellControl as control
For each cellControl in drow. controls
Dim dcell as tablecell
Dcell = ctype (cellControl, TableCell)
If dcell. controls. count> 0 then
Dim imageControl as control
For each imageControl in dcell. controls
Dim dimage as imageButton
Dimage = ctype (imageControl, imageButton)
Dimage. ImageUrl = "close.gif"
Next
End if
Next
End if
Next
End if
MyControl. visible = false
End if
Next
End if
End Sub

Application Instance

Take the arms database as an example. The database has three table files: customer, order, and order details. The customer and order tables have one-to-many relationships in the field name "Customer ID, order and order details have one-to-many relationships in the field name "Order ID. The following program connects to the database to form a DataSet, and then calls the subroutine ShowTables.
<% @ Import Namespace = System. Data. oledb %>
<% @ Import Namespace = System. Data %>
<% @ Import Namespace = System. drawing %>
<Html>
<Head>

<Script language = "VB" runat = "server">
Sub Page_Load (Sender as Object, e as EventArgs)

Dim dsDataSet as DataSet = new DataSet ()
Dim Constr, Conn as string

DsdataSet = cache ("mySet ")
If dsDataSet is Nothing
Constr = server. mappath ("northwind. mdb ")
Conn = "provider = microsoft. jet. oledb.4.0; data source =" & Constr
DsDataSet = new DataSet ()
Dim odA as oledbDataAdapter = new oledbDataAdapter ("select * from customer", Conn)
OdA. fill (dsDataSet, "MERs ")

OdA. SelectCommand. CommandText = "select * from order"
OdA. fill (dsDataSet, "Orders ")

OdA. SelectCommand. CommandText = "select * from Order details"
OdA. fill (dsDataSet, "Order_details ")
DsDataSet. Relations. Add ("M", dsDataSet. Tables (0). Columns ("Customer ID"), dsDataSet. Tables (1). Columns ("Customer ID "))
DsDataSet. Relations. Add ("N", dsDataSet. Tables (1). Columns ("Order ID"), dsDataSet. Tables (2). Columns ("Order ID "))
Cache ("mySet") = dsdataSet
End if
Call ShowTables (dsDataSet)
End sub

'Add the subroutine ShowTables

'Add the subroutine SetCellSize

'Add the subroutine ShowChildRows

'Add the subroutine ImageButton_Command
</Script>

</Head>
<Body>
<Form id = "form1" runat = "server">
</Form>
</Body>
</Html>

Copy the above program to notepad and copy the previous program to the corresponding subroutine addition. Note: the text disconnection error may occur during the above process, after modification, name it. aspx file. The running result is as follows:


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.