

#region 声明 //---------------------------------------------------------------------- // // 修改: 李淼(Nick.Lee) // // dataGrid前台修改数据,不刷新页面 // 时间:2005-04-05 // [email protected] // QQ:16503096 //注意:引用请标明修改出处,谢谢 //---------------------------------------------------------------------- #endregion WebForm3.aspx <%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm3" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm3</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language="javascript" src="./GridClick.js"></script> </HEAD> <body> <form id="Form1" method="post" runat="server"> <asp:DataGrid id="DataGrid1" runat="server" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" Font-Size="9pt" Width="100%" HorizontalAlign="Center"> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle> <ItemStyle HorizontalAlign="Center" ForeColor="#003399" BackColor="White"></ItemStyle> <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle> <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle> <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle> </asp:DataGrid> </form> </body> </HTML> 后台cs文件 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 WebApplication1 { /// <summary> /// WebForm3 的摘要说明。 /// </summary> public class WebForm3 : System.Web.UI.Page { protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; protected System.Data.SqlClient.SqlCommand sqlSelectCommand1; protected System.Data.SqlClient.SqlConnection sqlConnection1; protected WebApplication1.DataSet1 dataSet11; protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 this.sqlDataAdapter1.Fill(this.dataSet11); this.DataGrid1.DataSource=this.dataSet11.Tables[0]; this.DataBind(); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.dataSet11 = new WebApplication1.DataSet1(); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit(); // // sqlDataAdapter1 // this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "Categories", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"), new System.Data.Common.DataColumnMapping("CategoryName", "CategoryName"), new System.Data.Common.DataColumnMapping("Description", "Description"), new System.Data.Common.DataColumnMapping("Picture", "Picture")})}); // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT TOP 10 CategoryID, CategoryName, Description, Picture FROM Categories"; this.sqlSelectCommand1.Connection = this.sqlConnection1; // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=\"STAR-NICK\";packet size=4096;user id=sa;data source=\"(local)\";pers" + "ist security info=False;initial catalog=Northwind;pwd=sa"; // // dataSet11 // this.dataSet11.DataSetName = "DataSet1"; this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN"); this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); this.Load += new System.EventHandler(this.Page_Load); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); } #endregion private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType==ListItemType.AlternatingItem|e.Item.ItemType==ListItemType.Item) { for(int i=0;i<e.Item.Cells.Count;i++) { e.Item.Cells[i].Attributes["onClick"]="editCell(this);"; } } } } } 脚本文件: /** * <p>Title: datagrid点击编辑(不刷新页面)</p> * <p>Description: 实现datagrid点击编辑(不刷新页面)</p> * <p>Copyright: 2005-2005 by mail_ricklee Corporation</p> * <p>Company: mail_ricklee Corporation</p> * <p>CreateTime: 2005-04-05 11:30</p> * <p>ModifyTime: </p> * @CreateAuthor 李淼 * @version 1.0 * @ModifyAuthor * @version 1.0 */ function editCell (cell) { if (document.all) { cell.innerHTML = '<INPUT ID="editCell" ONCLICK="event.cancelBubble = true;" ONCHANGE="setCell(this.parentElement, this.value)" ONBLUR="setCell(this.parentElement, this.value)" VALUE="' + cell.innerText + '" SIZE="' + cell.innerText.length + '">'; document.all.editCell.focus(); document.all.editCell.select(); } else if (document.getElementById) { cell.normalize(); var input = document.createElement('INPUT'); input.setAttribute('value', cell.firstChild.nodeValue); input.setAttribute('size', cell.firstChild.nodeValue.length); input.onchange = function (evt) { setCell(this.parentNode, this.value); }; input.onclick = function (evt) { evt.cancelBubble = true; if (evt.stopPropagation) evt.stopPropagation(); }; cell.replaceChild(input, cell.firstChild); input.focus(); input.select(); } } function setCell (cell, value) { if (document.all) cell.innerText = value; else if (document.getElementById) cell.replaceChild(document.createTextNode(value), cell.firstChild); /* if(confirm('确认修改?')==false) { location.reload(); } else { if (document.all) cell.innerText = value; else if (document.getElementById) cell.replaceChild(document.createTextNode(value), cell.firstChild); } */ } 
|