<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="DBService_vb" %>
<html>
<head>
	<script language="vb" runat="server">
	sub OKClick(sender as object, e as EventArgs)'按下OK按钮
		bindNew(true)
		addnew.Visible=true'显示添加按钮
	end sub
	sub bindNew(tag as Boolean)
		if tag=true then
			if SQLServer.Text="" then
				errMsg.Text="未输入SQL服务器名称"
				return
			else
				Dim DBS as DBService_vb.DBSrc_vb = new DBService_vb.DBSrc_vb()
				Dim DS as DataSet = DBS.GetSQLData("localhost","sa","","StockSQL","Customers")
				Resault.DataSource=DS.Tables("Customers").DefaultView
				Resault.DataBind()
				Session("NowDS")=DS
			end if
		else
			Dim DS as DataSet=Ctype(Session("NowDS"),DataSet)
			Resault.DataSource=DS.Tables("Customers").DefaultView
			Resault.DataBind()
		end if
	end sub
	sub Res_Cancel(sender as object, e as DataGridCommandEventArgs)
		Resault.EditItemIndex = -1'取消
		errMsg.Text=""
		bindNew(false)
	end sub
	sub Res_Edit(sender as object, e as DataGridCommandEventArgs)
		Resault.EditItemIndex = e.Item.ItemIndex'编辑
		bindNew(false)
	end sub
	sub Res_Update(sender as object, e as DataGridCommandEventArgs)
		Dim DBS as DBSrc_vb = new DBSrc_vb()
		Dim NowDS as DataSet = Ctype(Session("NowDS"),DataSet)
		Dim totalCols as integer= e.Item.Cells.Count'取得文本框总数
		try
			dim i as integer=0
			for i=2 to totalCols-1
				Dim CurrentText as TextBox
				CurrentText = Ctype(e.Item.Cells(i).Controls(0),TextBox) '取得文本框
				NowDS.Tables("Customers").Rows(Resault.EditItemIndex)(i-2)=CurrentText.Text
			next
			if DBS.SaveSQLData(NowDS,SQLServer.Text,uid.Text,Password.Text,"StockSQL","Customers")=false then
				errMsg.Text="保存时发生错误"
				errMsg.Visible=true
			else
				errMsg.Visible=false
				Resault.EditItemIndex = -1
				bindNew(true)
			end if
		catch
			errMsg.Text="保存时发生错误"
			errMsg.Visible=true
		end try
	end sub
	sub Res_Delete(sender as object, e as DataGridCommandEventArgs)
		Dim DBS as DBSrc_vb = new DBSrc_vb()
		Dim NowDS as DataSet = Ctype(Session("NowDS"),DataSet)
		NowDS.Tables("Customers").Rows(e.Item.ItemIndex).Delete()
		if DBS.SaveSQLData(NowDS,SQLServer.Text,uid.Text,Password.Text,"StockSQL","Customers")=false then
			errMsg.Text="删除时发生错误"
			errMsg.Visible=true
		else
			errMsg.Visible=false
			Resault.EditItemIndex = -1
			bindNew(true)
		end if
	end sub
	sub AddNewClick(sender as object, e as EventArgs)
		Dim DS as DataSet=Ctype(Session("NowDS"),DataSet)
		Dim Row as DataRow=DS.Tables("Customers").NewRow()
		DS.Tables("Customers").Rows.Add(Row)'执行新增
		bindNew(false)
		Resault.EditItemIndex=Resault.Items.Count-1
		bindNew(false)
	end sub
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<form runat="server">
<Title:TitleShow id="BookT" Runat="server" />
<p>目的：以WebService使用数据库</p>
SQL服服器名称：<ASP:TEXTBOX Text="Localhost" runat="server" id="SQLServer" /><br>
登录ID：<ASP:TEXTBOX Text="sa" runat="server" id="uid" /><br>
密码：<ASP:TEXTBOX Runat="server" TextMode="Password" id="Password" /><ASP:button Text="OK" Runat="server" onclick="OKClick" /><br>
<asp:label id="errMsg" visible="true" runat="server" />
<asp:linkbutton Text="AddNew" Onclick="AddNewClick" runat="server" id="addnew" visible="false"/>
<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"
>
      <Columns>
        <asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="保存" ItemStyle-Wrap="false"/>
	<asp:ButtonColumn CommandName="Delete" Text="删除" ItemStyle-Wrap="false"></asp:ButtonColumn>
      </cOLUMNS>
</ASP:DataGrid>
</form>
</body>