

#region 声明
//----------------------------------------------------------------------
//
// 修改: 李淼(Nick.Lee)
//
// dataGrid前台修改数据,不刷新页面
// 时间:2005-04-05
// [email protected]
// QQ:16503096
//注意:引用请标明修改出处,谢谢
//----------------------------------------------------------------------
#endregion
WebForm3.aspx
1@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm3"
1<html>
2<head>
3<title>WebForm3</title>
4<meta content="text/html;charset=utf-8" http-equiv="content-type"/>
5<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
6<meta content="C#" name="CODE_LANGUAGE"/>
7<meta content="JavaScript" name="vs_defaultClientScript"/>
8<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
9<script language="javascript" src="./GridClick.js"></script>
10</head>
11<body>
12<form id="Form1" method="post" runat="server">
13<asp:datagrid backcolor="White" bordercolor="#3366CC" borderstyle="None" borderwidth="1px" cellpadding="4" font-size="9pt" horizontalalign="Center" id="DataGrid1" runat="server" width="100%">
14<selecteditemstyle backcolor="#009999" font-bold="True" forecolor="#CCFF99"></selecteditemstyle>
15<itemstyle backcolor="White" forecolor="#003399" horizontalalign="Center"></itemstyle>
16<headerstyle backcolor="#003399" font-bold="True" forecolor="#CCCCFF" horizontalalign="Center"></headerstyle>
17<footerstyle backcolor="#99CCCC" forecolor="#003399"></footerstyle>
18<pagerstyle backcolor="#99CCCC" forecolor="#003399" horizontalalign="Left" mode="NumericPages"></pagerstyle>
19</asp:datagrid>
20</form>
21</body>
22</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
{
///
1<summary>
2/// WebForm3 的摘要说明。
3/// </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);
}
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </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
1<e.item.cells.count;i++) *="" **="" ;="" <p="" e.item.cells[i].attributes["onclick"]="editCell(this);" {="" }="" 脚本文件:="">Title: datagrid点击编辑(不刷新页面)
2* <p>Description: 实现datagrid点击编辑(不刷新页面)</p>
3* <p>Copyright: 2005-2005 by mail_ricklee Corporation</p>
4* <p>Company: mail_ricklee Corporation</p>
5* <p>CreateTime: 2005-04-05 11:30</p>
6* <p>ModifyTime: </p>
7* @CreateAuthor 李淼 * @version 1.0
8* @ModifyAuthor * @version 1.0
9*/
10function editCell (cell) {
11if (document.all) {
12cell.innerHTML = '<input id="editCell" onblur="setCell(this.parentElement, this.value)" onchange="setCell(this.parentElement, this.value)" onclick="event.cancelBubble = true;" size="' + cell.innerText.length + '" value="' + cell.innerText + '"/>';
13document.all.editCell.focus();
14document.all.editCell.select();
15}
16else if (document.getElementById) {
17cell.normalize();
18var input = document.createElement('INPUT');
19input.setAttribute('value', cell.firstChild.nodeValue);
20input.setAttribute('size', cell.firstChild.nodeValue.length);
21input.onchange = function (evt) { setCell(this.parentNode, this.value); };
22input.onclick = function (evt) {
23evt.cancelBubble = true;
24if (evt.stopPropagation)
25evt.stopPropagation();
26};
27cell.replaceChild(input, cell.firstChild);
28input.focus();
29input.select();
30}
31}
32
33function setCell (cell, value) {
34if (document.all)
35cell.innerText = value;
36else if (document.getElementById)
37cell.replaceChild(document.createTextNode(value), cell.firstChild);
38/*
39if(confirm('确认修改?')==false)
40{
41location.reload();
42}
43else
44{
45if (document.all)
46cell.innerText = value;
47else if (document.getElementById)
48cell.replaceChild(document.createTextNode(value), cell.firstChild);
49}
50*/
51}</e.item.cells.count;i++)>