用ASP.NET在同一網頁中顯示主從關係表的相關資料

來源:互聯網
上載者:User
摘要:DataSet是資料集在記憶體中的表示方法,資料集可以有主從關係的資料表,在ACCESS中這種關係表現的很直觀,本文討論在同一網頁上直觀地顯示有主從關係的相應資料的程式設計和程式。目錄方法Tables和ImageButtons的ID的命名規則 網頁上Table的欄位長度的估計主要程式事件過程 應用執行個體方法 :對DataSet中的每一張表,如果有子表,將這張表的欄位名行和每一紀錄行在網頁上都顯示為只有一行的Table,並在記錄行Table的第一列置上一個ImageButton,以便程式控制展開或關閉相應的子表的紀錄行所形成的Table或Tables。對於每一紀錄行,子表有相應的記錄行,若子表還有子表,則對子表重複上述過程,即將子表的欄位名行與相應的記錄行在網頁上都顯示為只有一行的Table,並在記錄行Table的第二列置上一個ImageButton,反之將子表的欄位名行與相應的紀錄行在網頁上顯示為一個Table。利用Table的Visible屬性,可以控制子表相應記錄行的顯示。顯然可以使用遞迴調用實現這一過程。Tables和ImageButtons的ID的命名規則:用對ImageButton的點擊,程式控制網頁上的相應的Tables的顯示與隱藏,這些ImageButton.id和Table.id的命名就需要一定的規則。對DataSet中的第一張主表:由欄位行產生的網頁Table的id命名為t0由記錄行產生的網頁Tables的id分別為t1,t2,...tn ,與這些Tables相對應的ImageButton.id命名為 e1,e2,...en, 這裡 n:主表的記錄行數 對DataSet中的子表:子表中的記錄行都是隸屬於主表中的某一記錄行,因此這些記錄行產生的網頁Tables也隸屬於主表某一記錄行所產生網頁的Table,如隸屬於t2的Tables命名為t2-0,t2-1,t2-3...,其中t2-0為子表的欄位名行在網頁上產生的Table.id名,其餘以此類推,若子表沒有子表了,即最後一個子表,則t2-0是包含子表的欄位名行和相應的子表記錄行在網頁上產生的Table.id名。與這些table.id相對應的ImageButton.id分別命名為e2-1,e2-2,...。 網頁上Table的欄位長度的估計: DataSet中的每一張表都會在網頁上產生一些Table,這些Tables的外在形式應該是相同的。即Tables的相同的Cell應有同樣的長度。函數子程式SetCellSize計算DataTable的每一欄位平均字元數,和欄位名的字元數,並取其大者,若欄位的某一紀錄或欄位名都是ASCII字元,則其字元數減半。用此資料便可估計網頁上的Table的相應欄位的顯示長度。 下面是SetCellSize的程式,容易理解。
                  Function SetCellSize(myTable as DataTable)dim myRow as DataRowdim i,j,k,m as integerdim aa() as integerdim myBool as Booleanm=myTable.columns.count-1redim aa(m)for i=0 to m   aa(i)=0nextfor each myRow in myTable.rows                 '計算每一欄位的平均字元長度   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                                        '判斷dataTable中的每一項是否包括漢字            dim str1 as char=mid(mystr,k,1)            if ascw(str1)>255 then                    '有非ASCII字元               myBool=false               exit for            end if         next         if myBool then j=(j/2+0.5)                 '都是ASCII字元,字串長度減半         aa(i)+=j      end if   nextnext myRowk=myTable.rows.countfor i=0 to m   aa(i)=aa(i)/k                                           'DataTable的每一列的平均長度nextfor i=0 to myTable.columns.count-1           '對每一欄位名   dim str2 as string=myTable.columns(i).columnname   j=len(str2)   if j>0 then      myBool=true      for k=1 to j                                         '判斷欄位名中是否包括漢字         dim str1 as char=mid(str2,k,1)         if ascw(str1)>255 then                      '有非ASCII字元            myBool=false            exit for         end if      next      if myBool then j=(j/2+0.5)                    'ASCII字元,字串長度減半      if j>aa(i) then aa(i)=j   end ifnextSetCellSize=aaend Function                   
主要程式:子程式ShowTables設定一些初始值,然後調用子程式ShowChildRows。子程式ShowChildRows的參數說明:Rows:是一個DataRow數組,第一次調用ShowChildRows時,是DataTable的所有的記錄行。以後遞迴調用ShowChildRows時,是與父表某一記錄行相關的子表的一些記錄行。myTable:Rows所屬的DataTable,程式將使用它的Columns,即欄位名行。aa:函數子程式SetCellSize返回的一維整型數組。spaces:整型參數,用於在網頁顯示Tables時,這些Tables左側應設定幾個空儲存格,以顯示Table的隸屬關係。signal:字串參數,ImageButton的id值,用於產生相關的Tables和ImageButtons的id。因為要在網頁中添加Table控制項,所以在網頁中應有一個id為form1的Form控制項。動態地建立一個Table有三個步驟,首先,建立 TableCell 對象,即行中的儲存格,添加儲存格的內容有兩種方法:設定 Text 屬性,或者向 TableCell 的 Control.Controls 集合添加控制項,程式中對某些儲存格添加ImageButton控制項;接下來,建立 TableRow 以表示表中的行,將此前建立的 TableCell 對象添加到 TableRow 的 Cells 集合中。最後,將 TableRow 添加到 Table 控制項的 Rows 集合中。以下是程式:
                  Sub ShowTables(mySet as DataSet)dim spaces as integer=0dim aa() as integerdim i,d as integerdim signal as string=""dim myTable as dataTable=mySet.tables(0)dim rows() as DataRowd=myTable.rows.count-1redim rows(d)for i=0 to myTable.rows.count-1   rows(i)=myTable.rows(i)nextaa=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 integerdim fontsize as integer=10dim myRow as DataRowdim myCol as DataColumndim testTable as Tabledim Cell as TableCelldim Row as TableRowdim myBool as Booleandim myimage as ImageButtondim ChildRows() as DataRowdim ChildTable as DataTabledim myRel as DataRelationdim bb() as integerdim CellStyle as new TableItemStylecellStyle.borderwidth=unit.pixel(1)cellStyle.borderstyle=Borderstyle.solidcellStyle.wrap=falseif myTable.ChildRelations.count=1 then  '有從表   myRel=myTable.ChildRelations(0)   ChildTable =myRel.ChildTable   m=ChildTable.Columns.count-1   redim bb(m)   myBool=True   bb=SetCellSize(ChildTable)end iftestTable=New TabletestTable.borderwidth=unit.pixel(1)testTable.cellspacing=0testtable.cellPadding=0testTable.font.name="宋體"testTable.font.size=fontunit.point(fontsize)testTable.visible=trueif signal<>"" then                     '遞迴調用時,欄位名行形成的Table.id的賦值   leng=len(signal)   testTable.id="t" & mid(signal,2,leng-1) & "-0"   testtable.visible=falseelse   testTable.id="t" & "0"              '初始時,欄位名行形成的Table.id   testTable.visible=trueend ifform1.controls.add(testtable)'********** 以下程式為產生表的欄位名稱行的Table ********************Row=New tableRowRow.borderwidth=unit.pixel(1)m=rows.lengthif 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)   nextend ifif 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 ifm=myTable.Columns.count-1for 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)nexttesttable.Rows.add(row)'************* 以下程式為產生表的各行紀錄的Tables *********************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                 '若有從表,遞迴調用      dim spaces2 as integer      spaces2=spaces+1      ChildRows=myRow.GetChildRows(myRel)      call ShowChildRows(ChildRows,bb,ChildTable,spaces2,myimage.id)   end ifnextEnd Sub                  
事件程序:在“Tables和ImageButtons的ID的命名規則”一節中給出了命名的規則,這裡舉例說明:如果點擊了id為e1的ImageButton,它所對應的網頁上的Table的id為t1,與t1相關的子表上的相應的記錄行在網頁上形成id為t1-1,t1-2,...t1-n的Tables,這裡 n為子表上的相應的記錄行數,而t1-0為子表的欄位名行在網頁上形成的Table。如果e1的ImageUrl為“”close.gif,則將其設定為“”open.gif,並將t1-0,t1-1,...t1-n的Visible屬性設定為True。反之,除了要將e1的ImageUrl改為close.gif和將t1-0,t1-1,...t1-n的Visible屬性改為False外,如果t1-1,t1-2,...t1-n有對應id為e1-1,e1-2,...e1-n的ImageButton的,而這些ImageButtons中的某些ImageUrl為open.gif,即子表的子表的某些記錄行產生的Tables是開啟的,則這些ImageUrl設定為close.gif。文章標題:Using Web Services Enhancements to Send SOAP Messages with Attachments程式如下:
                  Sub ImageButton_Command(Sender as object,e as CommandEventArgs)dim myControl as Controldim str1 as stringdim leng as integerstr1=sender.idleng=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   nextelse   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   nextend ifEnd Sub                  
應用執行個體:以羅斯文資料庫為例,該資料庫有客戶、訂單和訂單明細三個表檔案,客戶和訂單兩張表在欄位名“客戶ID”有一對多的關係,訂單和訂單明細在欄位名“訂單ID”有一對多的關係。下述程式串連資料庫,形成DataSet後,調用子程式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 stringdsdataSet=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 客戶",Conn)   odA.fill(dsDataSet,"Customers")   odA.SelectCommand.CommandText="select * from 訂單"   odA.fill(dsDataSet,"Orders")   odA.SelectCommand.CommandText="select * from 訂單明細"   odA.fill(dsDataSet,"Order_details")dsDataSet.Relations.Add("M",dsDataSet.Tables(0).Columns("客戶ID"),dsDataSet.Tables(1).Columns("客戶ID"))dsDataSet.Relations.Add("N",dsDataSet.Tables(1).Columns("訂單ID"),dsDataSet.Tables(2).Columns("訂單ID"))Cache("mySet")=dsdataSetend ifcall ShowTables(dsDataSet)end sub '子程式ShowTables添加處'子程式SetCellSize添加處'子程式ShowChildRows添加處'子程式ImageButton_Command添加處</script></head><body><form id="form1" runat="server"></form></body></html>                                    
將上述程式copy到記事本中,並將前面的程式copy到相應的子程式添加處,注意:在上述過程中可能會出現文本的斷行錯誤,在加以修改後,命名為.aspx檔案,運行結果如:

作者:曹國宣
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.