ww w.china it power.co1qQE1yTAs1vIt8n
ww w.china it power.co1qQE1yTAs1vIt8n
#region 声明 //---------------------------------------------------------------------- // // 修改: 李淼(Nick.Lee) // // dataGrid前台修改数据,不刷新页面ww w.china it power.co1qQE1yTAs1vIt8n
// 时间:2005-04-05ww w.china it power.co1qQE1yTAs1vIt8n
// boyorgril@msn.com // QQ:16503096 //注意:引用请标明修改出处,谢谢 //---------------------------------------------------------------------- #endregionww w.china it power.co1qQE1yTAs1vIt8n
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>ww w.china it power.co1qQE1yTAs1vIt8n
后台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;ww w.china it power.co1qQE1yTAs1vIt8n
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(); }ww w.china it power.co1qQE1yTAs1vIt8n
#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();ww w.china it power.co1qQE1yTAs1vIt8n
} #endregionww w.china it power.co1qQE1yTAs1vIt8n
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);"; } } } } }ww w.china it power.co1qQE1yTAs1vIt8n
脚本文件: /** * <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(); } }ww w.china it power.co1qQE1yTAs1vIt8n
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); } */ }
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?