using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace ComplexTest { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Data.SqlClient.SqlConnection sqlConnection1; protected System.Web.UI.WebControls.Button PrevPage; protected System.Web.UI.WebControls.Button NextPage; protected System.Data.SqlClient.SqlCommand sqlSelectCommand2; protected System.Data.SqlClient.SqlCommand sqlInsertCommand2; protected System.Data.SqlClient.SqlCommand sqlUpdateCommand2; protected System.Data.SqlClient.SqlCommand sqlDeleteCommand2; protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; protected ComplexTest.DataSet2 dataSet21; protected System.Web.UI.WebControls.Button FirstPage; protected System.Web.UI.WebControls.Button LastPage; protected System.Web.UI.WebControls.Label lblPageCount; protected System.Web.UI.WebControls.Button GotoPage; protected System.Web.UI.WebControls.TextBox txtPageIndex; protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1; protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if(!IsPostBack) { MyDataBind(); } } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand(); this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand(); this.sqlUpdateCommand2 = new System.Data.SqlClient.SqlCommand(); this.sqlDeleteCommand2 = new System.Data.SqlClient.SqlCommand(); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); this.dataSet21 = new ComplexTest.DataSet2(); ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit(); this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); this.PrevPage.Click += new System.EventHandler(this.Button1_Click); this.NextPage.Click += new System.EventHandler(this.Button2_Click); this.FirstPage.Click += new System.EventHandler(this.btnFirst_Click); this.LastPage.Click += new System.EventHandler(this.btnLast_Click); this.GotoPage.Click += new System.EventHandler(this.GotoPage_Click); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=SOHO;packet size=4096;user id=sa;data source=SOHO;persist security" + " info=False;initial catalog=Northwind"; // // sqlSelectCommand2 // this.sqlSelectCommand2.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region," + " PostalCode, Country, Phone, Fax FROM Customers"; this.sqlSelectCommand2.Connection = this.sqlConnection1; // // sqlInsertCommand2 // this.sqlInsertCommand2.CommandText = @"INSERT INTO Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)"; this.sqlInsertCommand2.Connection = this.sqlConnection1; this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax")); // // sqlUpdateCommand2 // this.sqlUpdateCommand2.CommandText = @"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle, Address = @Address, City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country, Phone = @Phone, Fax = @Fax WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)"; this.sqlUpdateCommand2.Connection = this.sqlConnection1; this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null)); // // sqlDeleteCommand2 // this.sqlDeleteCommand2.CommandText = @"DELETE FROM Customers WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL)"; this.sqlDeleteCommand2.Connection = this.sqlConnection1; this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null)); // // sqlDataAdapter1 // this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand2; this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand2; this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand2; this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"), new System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"), new System.Data.Common.DataColumnMapping("ContactName", "ContactName"), new System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"), new System.Data.Common.DataColumnMapping("Address", "Address"), new System.Data.Common.DataColumnMapping("City", "City"), new System.Data.Common.DataColumnMapping("Region", "Region"), new System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"), new System.Data.Common.DataColumnMapping("Country", "Country"), new System.Data.Common.DataColumnMapping("Phone", "Phone"), new System.Data.Common.DataColumnMapping("Fax", "Fax")})}); this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand2; // // dataSet21 // this.dataSet21.DataSetName = "DataSet2"; this.dataSet21.Locale = new System.Globalization.CultureInfo("zh-CN"); this.Load += new System.EventHandler(this.Page_Load); ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit(); } #endregion private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if((e.Item.ItemType==ListItemType.Item)||(e.Item.ItemType==ListItemType.AlternatingItem)) { string strField=e.Item.Cells[0].Text;//指定要返回的字段,目前是返回第0行,可以改成其他的字段 e.Item.Attributes.Add("onDblClick","SelectOK('"+strField+"')");//双击时也可以返回选中结果 e.Item.Attributes.Add("onClick","RowFocus("+e.Item.ItemIndex+",'"+strField+"',1)");//单击时执行选中行 e.Item.Attributes.Add("id","ROW"+e.Item.ItemIndex);//给表格中的每一行起个名字以方便客户端代码调用 if(e.Item.ItemIndex==0) //设置初始化时的Javascript语句 { CreateJavascript(strField,DataGrid1.PageSize);//初始化客户端代码 } //e.Item.Attributes.Add("onMouseOver","RowFocus("+e.Item.ItemIndex+",'"+e.Item.Cells[0].Text +"',1)"); } } private void CreateJavascript(string ColValue,int PageSize) { //画面初始化Javascript语句,将凸现行设置为首行,并设置页面行数 String scriptString = "<script language=javascript>"; scriptString += "MaxRowCount ="+PageSize+";"; //对客户端代码设置最大的行数 scriptString += "RowFocus(0,'"+ColValue+"',0)";//设置初始化时将光标定位到第一行 scriptString += "</script>"; if(!this.IsStartupScriptRegistered("Startup")) this.RegisterStartupScript("Startup", scriptString); } private void Button1_Click(object sender, System.EventArgs e) { if(DataGrid1.PageCount>0) { if(DataGrid1.CurrentPageIndex>0) { DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-1; MyDataBind(); } } } private void Button2_Click(object sender, System.EventArgs e) { if(DataGrid1.PageCount>0) { if(DataGrid1.CurrentPageIndex<DataGrid1.PageCount-1) { DataGrid1.CurrentPageIndex =DataGrid1.CurrentPageIndex+1; MyDataBind(); } } } private void btnFirst_Click(object sender, System.EventArgs e) { if(DataGrid1.PageCount>0) { DataGrid1.CurrentPageIndex = 0; MyDataBind(); } } private void btnLast_Click(object sender, System.EventArgs e) { if(DataGrid1.PageCount>0) { DataGrid1.CurrentPageIndex =DataGrid1.PageCount-1; MyDataBind(); } } private void MyDataBind() { sqlDataAdapter1.Fill(dataSet21); DataGrid1.DataBind(); if(DataGrid1.PageCount>0) { int PageIndex = DataGrid1.CurrentPageIndex+1; lblPageCount.Text = "共"+DataGrid1.PageCount+"页,当前为第"+PageIndex.ToString()+"页"; txtPageIndex.Text = PageIndex.ToString(); } } private void GotoPage_Click(object sender, System.EventArgs e) { if(DataGrid1.PageCount>0) { if(txtPageIndex.Text.Length>0) { int i=Convert.ToInt16(txtPageIndex.Text)-1; if((i<DataGrid1.PageCount)&&(i>=0)) { DataGrid1.CurrentPageIndex=i; MyDataBind(); } } } } } } |