<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@ Page Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="Edit" Namespace="DataCon" Assembly="Edit_cs" %>
<html>
<head>
	<script language="c#" runat="server">
	void Page_load(Object sender, EventArgs e){
		if(IsPostBack==false){
			bindNew(true);
			NowEdit.NowRecord=0;//设定目前条数
			Page.DataBind();
		}
	}
	void bindNew(Boolean tag){
		if(tag==true){//以下均是自定义控件的属性
			NowEdit.SQLString="Select * from Customers";
			NowEdit.ServerName="Localhost";//服务器名称
			NowEdit.Uid="sa";//登录id
			NowEdit.Pass="";//密码
			NowEdit.DataKeyField="CusID";
			NowEdit.DBName="stockSQL";
			NowEdit.TBName="customers";
			NowEdit.NowMode="View";
			Page.DataBind();
		}
	}
	void SaveClick(Object sender, EventArgs e){
		Boolean x=NowEdit.Save();
		if(x!=true){
			ErrorSave.Text = "保存错误";
		}
		else{
			ErrorSave.Text = "";
			Save.Visible=false;
			Del.Visible=true;
			EditOrView.Text="编辑";
			NowEdit.NowMode="View";
		}
	}
	void DelClick(Object sender, EventArgs e){
		string d="Delete " + NowEdit.TBName + " Where " + NowEdit.DataKeyField + " ='" + NowEdit.DataKeyValue +"'";
		NowEdit.Delete(d);//删除
		bindNew(true);
	}
	void EditClick(Object sender, EventArgs e){
		ErrorSave.Text = "";
		if(EditOrView.Text=="编辑"){//编辑
			NowEdit.NowMode="Edit";
			EditOrView.Text="取消";
			Save.Visible=true;
			Del.Visible=false;
		}
		else{//检视
			NowEdit.NowMode="View";
			EditOrView.Text="编辑";
			Save.Visible=false;
			Del.Visible=true;
		}
	}
	void ChangeRecord(object sender, CommandEventArgs e){
		if(e.CommandArgument == "Next"){//下条
			if(NowEdit.NowRecord+1<NowEdit.AllRecords){
				NowEdit.NowRecord+=1;
			}
		}
		else if(e.CommandArgument == "First"){//首条
			NowEdit.NowRecord=0;
		}
		else if(e.CommandArgument == "Last"){//末条
			NowEdit.NowRecord=NowEdit.AllRecords-1;
		}
		else if(e.CommandArgument == "Prev"){//上条
			if(NowEdit.NowRecord > 0){
				NowEdit.NowRecord-=1;
			}
			else{
				NowEdit.NowRecord = 0;
			}
		}
		Page.DataBind();
	}
	void GotoRecord(Object sender,EventArgs e){
		if(Convert.ToInt32(ToRecord.Text)<NowEdit.AllRecords){
			NowEdit.NowRecord=Convert.ToInt32(ToRecord.Text)-1;
		}
		else if(Convert.ToInt32(ToRecord.Text)<0){
			NowEdit.NowRecord=0;
		}
		Page.DataBind();
	}
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
查看 <asp:hyperlink id="h1" NavigateUrl="/aspnetdata/shows.aspx?cate=c&file=ch12/12-3/cs/Edit.cs" Text="Edit.cs" runat="server" target="_blank"/><br>
<asp:LinkButton ID="First" Tooltip="First" OnCommand="ChangeRecord" Runat="server" Text="首条" CommandArgument="First" cssclass="Tab_08" />
<asp:LinkButton ID="Prev" Tooltip="Prev" OnCommand="ChangeRecord" Runat="server" Text="上条" CommandArgument="Prev"  cssclass="Tab_08"/>
<asp:LinkButton ID="Next" Tooltip="Next" OnCommand="ChangeRecord" Runat="server" Text="下条" CommandArgument="Next"  cssclass="Tab_08"/>
<asp:LinkButton ID="Last" Tooltip="Last" OnCommand="ChangeRecord" Runat="server" Text="末条" CommandArgument="Last"  cssclass="Tab_08"/>
至第<asp:TextBox Columns="3" AutoPostBack="true" ID="ToRecord" OnTextChanged="GotoRecord" Runat="server" />条
<p>第<asp:Label ID="CurrentRecord" Text='<%# NowEdit.NowRecord+1 %>' Tooltip="CurrentRecord" Runat="server" />条，共<asp:Label ID="AllRecords" Text='<%# NowEdit.AllRecords %>' Tooltip="AllRecords" Runat="server" />条
<asp:linkButton ID="EditOrView" Text="编辑" Runat="server" Onclick="EditClick" />  
<asp:linkButton ID="Save" Text="保存" Visible="false" Runat="server" Onclick="SaveClick" />  
<asp:linkButton ID="Del" Text="删除" Runat="server" Onclick="DelClick" />
<asp:label id="ErrorSave" runat="server" />
<hr>
<Edit:SqlData ID="NowEdit" Runat="server" />
</form>
</body>