<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@Page Debug=True %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<html>
<head>
	<script language="VB" runat="server">
	dim Source as SqlConnection
	dim NowSort as string
	dim d as string ="SELECT Staff.*, MonthPay.* FROM MonthPay INNER JOIN Staff ON MonthPay.SID = Staff.SID Order by Staff.SID"
	sub Page_load(sender as object,e as EventArgs)
		Source = Ctype(Session("NowEmpSQL"),SqlConnection)
		if IsPostBack=false then
			dim HT as Hashtable =new Hashtable()
	        	HT.Add("StaffID", "SID")
        		HT.Add("Name", "cname")
        		HT.Add("PID", "pid")
        		HT.Add("YearMonth", "YearMonth")
        		HT.Add("Pay", "Pay")
			AllField.DataSource = HT
			AllField.DataValueField="Value"
			AllField.DataTextField="Key"
			AllField.DataBind()
			NowSort=""
			bindNew(true)
		end if
	end sub
	sub bindNew(tag as Boolean)'结合至DataGrid
		if tag=true then
			Dim DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
			NComm.Fill(DS, "staffPay")
			Session("NowDS")=DS '声明为Session
			staffPay.DataSource=DS.Tables("staffPay").DefaultView
			staffPay.DataBind()
		else
			Dim DS as DataSet =Ctype(Session("NowDS"),DataSet)
			Dim dv as DataView =DS.Tables("staffPay").DefaultView
			dv.Sort=NowSort
			staffPay.DataSource=dv
			staffPay.DataBind()
		end if
	end sub
	sub SortOK_Click(sender as object,e as EventArgs)
		dim DS as DataSet =Ctype(Session("NowDS"),DataSet)
		Dim dv as DataView =DS.Tables("staffPay").DefaultView
		dim i as integer=0
		for i=0 to AllField.Items.Count-1 '取得每一选项
			if AllField.Items(i).Selected=true then '判断是否选取
				NowSort +=  "," & AllField.Items(i).Value & " " & SortType.SelectedItem.Value
			end if
		next
		if NowSort="" then '若未选取则不处理
			return
		else
			NowSort=NowSort.Remove(0,1)
			dv.Sort = NowSort '执行排序
			staffPay.DataSource=dv
			staffPay.DataBind()
		end if
	end sub
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server" >
<table><tr><td valign="top" width="70">
指定欲排序的字段(可复选)：
</td><td><asp:ListBox id=AllField Width="100px" rows="4" runat="server" selectionmode="Multiple" /></td>
<td valign="top">
　排序方式：<asp:DropDownlist ID="SortType" Runat="Server" >
            <asp:ListItem value="asc" selected>递增</asp:ListItem>
            <asp:ListItem value="desc">递减</asp:ListItem>
</asp:DropDownList>
<asp:Button id="SortOK" Runat="Server" Onclick="SortOK_Click" Text="执行排序" />
</td>
</tr></table>
      <ASP:DataGrid id="staffPay" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="9pt"
        HeaderStyle-BackColor="LightYellow"
	HeaderStyle-HorizontalAlign="Center"
	DataKeyField="SID"
	AutoGenerateColumns="FALSE"
	SelectedItemStyle-BackColor="#FFFFC0"
>
      <Columns>
	<asp:BoundColumn HeaderText="StaffID" DataField="SID" ItemStyle-Wrap="false"  />
	<asp:BoundColumn HeaderText="Name" DataField="cname" />
	<asp:BoundColumn HeaderText="PID" DataField="pid" />
	<asp:BoundColumn HeaderText="YearMonth" DataField="YearMonth" />
	<asp:TemplateColumn HeaderText="Pay" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Pay","{0:c}") %> id="Pay"/>
		</ItemTemplate>
	</asp:TemplateColumn>
	</Columns>
</ASP:DataGrid>
</form>