2011-03-03 52 views
0

我目前正在通過從XML文檔檢索數字和名稱並動態構建表來構建表。目前,表格已經生成,您可以選中一個框來選擇是否要隨機化與名稱對應的數字。我需要能夠檢查另一個框以確定您是否想要升序或降序。我不知道如何對錶格對象進行排序。我是否必須首先將它讀入數組,然後將它們重新讀入表中?如何將表格分類爲升序或降序? VB.NET

XML文檔示例:

- <roster> 
- <student> 
    <order>01</order> 
    <name>Sabrina Spark</name> 
    </student> 
- <student> 
    <order>02</order> 
    <name>Ryan Weble</name> 
    </student> 
- <student> 
    <order>03</order> 
    <name>Fredric Scott</name> 
    </student> 
- <student> 
    <order>04</order> 
    <name>Cory Snyder</name> 
    </student> 

代碼:
標記:

<%@頁面語言= 「VB」 AutoEventWireup = 「假」 代碼隱藏= 「Default.aspx.vb」 繼承= 「itc320_presentation_order._Default」 %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
    <link href="StyleSheet1.css" rel="Stylesheet" type = "text/css" media = "screen" /> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <asp:CheckBox ID="Descending" runat="server" Text="Descending" /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:CheckBox ID="ExistingOrder" runat="server" Text="Existing Order" /> 

     <br /> 
     <asp:Xml ID="Xml1" runat="server"></asp:Xml> 
     <br /> 
     <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
      DataFile="~/App_Data/roster.xml" TransformFile="~/App_Data/roster.xsl"> 
     </asp:XmlDataSource> 
     <br /> 
     <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> 
     <br /> 
     <br /> 
     <asp:Button ID="cmdRandomize" runat="server" Height="36px" Text="Generate Table" 
      Width="127px" /> 
     <br /> 

    </div> 
    </form> 
</body> 
</html> 

代碼背後:

Imports System.Xml 
Imports System.IO 
Partial Public Class _Default 
    Inherits System.Web.UI.Page 
    Dim rand As New Random() 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     If Not IsPostBack Then 

     End If 
    End Sub 
    Protected Sub cmdRandomize_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdRandomize.Click 

     Dim intArl(15) As Integer 

     Dim ds As DataSet 
     Dim dr As DataRow 

     Dim tbl As New Table 

     Dim strPath As String 

     'Placehold control will hold the dynamically created table 
     PlaceHolder1.Controls.Clear() 

     'build path to data file 
     strPath = Server.MapPath("/App_Data") & "\roster.xml" 


     ds = New DataSet() 

     'load dataset object with data from xml source 
     ds.ReadXml(strPath) 

     Dim dtRoster As DataTable = ds.Tables(0) 

     'Generate table of random order ints 
     If ExistingOrder.Checked = False Then 
      genUnique(intArl, dtRoster.Rows.Count - 1) 
     Else 
      intArl = ViewState("intArl") 
     End If 


     Dim intSeq As Integer = 0 
     For Each dr In dtRoster.Rows 
      Dim tr As New TableRow 
      For intColCnt = 0 To 1 
       Dim tc As New TableCell() 
       Dim txtBox As New TextBox() 
       If intColCnt = 0 Then 
        txtBox.Text = intSeq 
       Else 
        txtBox.Text = dr(intColCnt) 
       End If 
       'Add the control to the TableCell 
       tc.Controls.Add(txtBox) 
       'Add the TableCell to the TableRow 
       tr.Cells.Add(tc) 
      Next 
      If intSeq Mod 2 Then 
       tr.CssClass = "odd" 
      Else 
       tr.CssClass = "even" 
      End If 



      tbl.Rows.Add(tr) 
      intSeq += 1 

     Next 
     PlaceHolder1.Controls.Add(tbl) 


     ViewState("intArl") = intArl 
    End Sub 
    Private Sub genUnique(ByRef intArl() As Integer, ByVal intCnt As Integer) 
     Dim intGen As Integer 
     Dim intClassCnt As Integer 
     For intClassCnt = 0 To intCnt 
regen: 
      'intGen = Int(Rnd() * 20) + 1 
      intGen = rand.Next(1, intCnt + 2) 
      If Not alreadyExists(intArl, intGen, intClassCnt) Then 
       intArl(intClassCnt) = intGen 
       Else : GoTo regen 
       End If 
     Next 
    End Sub 
    Private Function alreadyExists(ByRef intArl() As Integer, ByVal num As Integer, ByVal curr As Integer) As Boolean 
     Dim intCnt As Integer 

     'Scan the entries in the array for the existance of the newly 
     'generated number. Return True if found to already exist in the array 

     For intCnt = 0 To curr - 1 
      If num = intArl(intCnt) Then 
       Return True 
      End If 
     Next 
     Return False 
    End Function 

End Class 

回答

0

既然你是編程生成不能被分類爲這樣的表。你可以很容易地sort your data table,然後重新生成你的表格。另一種選擇是使用DataGridview中的一個可用數據控件,例如處理生成任務的工作,並且在某些情況下允許您在設計器中添加排序。

+0

如果我在鏈接中實現它,那麼我會傳遞給函數什麼?我不知道如何將其更改爲升序/降序而不是按字母順序。 – amazinghorse24 2011-03-03 07:02:32

+0

htmllinkbutton有一個你可以在服務器端處理的onlick。如果你有兩個按鈕,你可以有兩個處理程序,除非我不明白,否則你不需要通過任何東西。 – rerun 2011-03-03 07:07:49

+0

'私人函數AlphabeticSort(ByVal dtTable作爲DataTable,ByVal sortOrder作爲整數)作爲DataTable' 功能標題是什麼,但不適用於我的代碼。我無法適應我需要做的事情。 – amazinghorse24 2011-03-03 07:21:16