<%@ 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_cs" %>
<html>
<head>
	<script language="c#" runat="server">
	void OKClick(object sender, EventArgs e){//按下OK按钮
		bindNew(true);
		addnew.Visible=true;//显示添加按钮
	}
	void bindNew(Boolean tag){
		if(tag==true){
			if(SQLServer.Text==""){
				errMsg.Text="未输入SQL服务器名称";
				return;
			}
			else{
				DBService_cs.DBSrc DBS = new DBService_cs.DBSrc();
				DataSet DS = DBS.GetSQLData("localhost","sa","","StockSQL","Customers");
				Resault.DataSource=DS.Tables["Customers"].DefaultView;
				Resault.DataBind();
				Session["NowDS"]=DS;
			}
		}
		else{
			DataSet DS=(DataSet)Session["NowDS"];
			Resault.DataSource=DS.Tables["Customers"].DefaultView;
			Resault.DataBind();
		}
	}
	void Res_Cancel(Object Sender, DataGridCommandEventArgs e){
		Resault.EditItemIndex = -1;//取消
		errMsg.Text="";
		bindNew(false);
	}
	void Res_Edit(Object Sender, DataGridCommandEventArgs e){
		Resault.EditItemIndex = e.Item.ItemIndex;//编辑
		bindNew(false);
	}
	void Res_Update(Object Sender, DataGridCommandEventArgs e){
		DBSrc DBS = new DBSrc();
		DataSet NowDS = (DataSet)Session["NowDS"];
		int totalCols = e.Item.Cells.Count;//取得文本框总数
		try{
			for(int i=2;i<=totalCols-1;i++){
				TextBox CurrentText;
				CurrentText = (TextBox)e.Item.Cells[i].Controls[0];//取得文本框
				NowDS.Tables["Customers"].Rows[Resault.EditItemIndex][i-2]=CurrentText.Text;
			}
			if(DBS.SaveSQLData(NowDS,SQLServer.Text,uid.Text,Password.Text,"StockSQL","Customers")==false){
				errMsg.Text="保存时发生错误";
				errMsg.Visible=true;
			}
			else{
				errMsg.Visible=false;
				Resault.EditItemIndex = -1;
				bindNew(true);
			}
		}
		catch{
			errMsg.Text="保存时发生错误";
			errMsg.Visible=true;
		}
	}
	void Res_Delete(Object Sender, DataGridCommandEventArgs e){
		DBSrc DBS = new DBSrc();
		DataSet NowDS = (DataSet)Session["NowDS"];
		NowDS.Tables["Customers"].Rows[e.Item.ItemIndex].Delete();
		if(DBS.SaveSQLData(NowDS,SQLServer.Text,uid.Text,Password.Text,"StockSQL","Customers")==false){
			errMsg.Text="删除时发生错误";
			errMsg.Visible=true;
		}
		else{
			errMsg.Visible=false;
			Resault.EditItemIndex = -1;
			bindNew(true);
		}
	}
	void AddNewClick(object sender, EventArgs e){
		DataSet DS=(DataSet)Session["NowDS"];
		DataRow Row=DS.Tables["Customers"].NewRow();
		DS.Tables["Customers"].Rows.Add(Row);//执行添加
		bindNew(false);
		Resault.EditItemIndex=Resault.Items.Count-1;
		bindNew(false);
	}
	</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>