<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ADO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="DBService" %>
<html>
<head>
	<script language="VB" runat="server">
	Sub Page_load(sender As Object, e As EventArgs) 
		if ispostback=False then
		End if
	End Sub
	Sub selectTable(sender As Object, e As EventArgs)
		Dim DBS as DBSRC = New DBSRC
		Dim DS As DataSet = DBS.GetACCData(AllTables.SelectedItem.Value)
		Resault.DataSource=DS.Tables(AllTables.SelectedItem.Value).DefaultView
		Resault.DataBind
		Session("NowDSUse") = DS
		TotalRec.Text = "共" & DS.Tables(AllTables.SelectedItem.Value).Rows.Count.Tostring & "条"
		errorMsg.Visible=False
	End sub
	Sub Res_Cancel(Sender As Object, E As DataGridCommandEventArgs)
		Resault.EditItemIndex = -1 '取消
		errorMsg.Text=""
		bindNew()
	End Sub
	Sub Res_Edit(Sender As Object, E As DataGridCommandEventArgs)
		Resault.EditItemIndex = CInt(E.Item.ItemIndex)'编辑
		bindNew()
	End Sub
	Sub Res_Update(Sender As Object, E As DataGridCommandEventArgs)
		Dim DBS as DBSRC = New DBSRC
		Dim NowDS As DataSet = CType(Session("NowDSUse"), DataSet)
		Dim totalCols As Integer = E.Item.Cells.Count'取得文本框总数
		Try
			For I=2 To totalCols-1'逐一设定各字段
				Dim CurrentText As TextBox
				CurrentText = E.Item.Cells(I).Controls(0)'取得文字方块
				ColValue = CurrentText.Text
				NowDS.Tables(AllTables.SelectedItem.Value).Rows(Resault.EditItemIndex)(I-2)=ColValue
			Next
			If DBS.SaveACCData(NowDS,AllTables.SelectedItem.Value)=False Then
				errorMsg.Text="保存时发生错误1"
				errorMsg.Visible=True
			Else
				Resault.EditItemIndex = -1
				errorMsg.Visible=False
				Dim DS As DataSet = DBS.GetACCData(AllTables.SelectedItem.Value)
				Resault.DataSource=DS.Tables(AllTables.SelectedItem.Value).DefaultView
				Resault.DataBind
				Session("NowDSUse") = DS
			End if
		Catch Ex As Exception
			errorMsg.Text="保存时发生错误"
			errorMsg.Visible=True
		End Try
	End Sub
	Sub BindNew()
		Resault.DataSource=Session("NowDSUse").Tables(AllTables.SelectedItem.Value).DefaultView
		Resault.DataBind
	End Sub
	Sub Res_Delete(Sender As Object, E As DataGridCommandEventArgs)
		Dim DBS as DBSRC = New DBSRC
		Session("NowDelete")=Session("NowDelete")+1
		Dim NowSel as String = AllTables.SelectedItem.Value
		Dim NowDS As DataSet = CType(Session("NowDSUse"), DataSet)
		NowDS.Tables(NowSel).Rows(E.Item.ItemIndex).Delete
 		If DBS.SaveACCData(NowDS,NowSel)=False Then
			errorMsg.Text="保存时发生错误"
		Else
			Dim DS As DataSet = DBS.GetACCData(NowSel)
			Resault.DataSource=DS.Tables(NowSel).DefaultView
			Resault.DataBind
			Session("NowDSUse") = DS
			TotalRec.Text = "共" & DS.Tables(NowSel).Rows.Count.Tostring & "条"
		End if
	End sub
	</script>
</head>
<body background="../../images/winback.gif">
<!--#INCLUDE FILE="../../fun.inc"--> 
<p>本例文件：UseDBS.aspx(SQL) 目的：以WebService使用数据库</p>  
<form runat="server">
选择数据表：<asp:dropdownlist tooltip="AllTables" id="AllTables" Autopostback="True" OnSelectedIndexChanged="selectTable" Runat="server" >
            <asp:ListItem>Customers</asp:ListItem>
            <asp:ListItem>Employees</asp:ListItem>
            <asp:ListItem>Orders</asp:ListItem>
            <asp:ListItem>Supplier</asp:ListItem>
            <asp:ListItem>Product</asp:ListItem>
        </asp:DropDownList>
<asp:label ID="errorMsg" Runat="server" /><ASP:Label ID="TotalRec" Runat="server" /><br>

<ASP:DataGrid id="Resault" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="GreenYellow"
AlternatingItemStyle-BackColor="LightYellow"
AutoGenerateColumns="True"
      OnEditCommand="Res_Edit"
      OnCancelCommand="Res_Cancel"
      OnUpdateCommand="Res_Update"
OnDeleteCommand="Res_Delete"
>
      <property name="Columns">
        <asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="保存" ItemStyle-Wrap="false"/>
	<asp:ButtonColumn CommandName="Delete" Text="删除" ItemStyle-Wrap="false"></asp:ButtonColumn>
      </property>
</ASP:DataGrid>
</form>
</body>