给你一实例吧 -- 实例之点击获得某一单元格,获得单元格的值
获得的值你要作何操作那是你以后的事了,代码帖上即可 demo_gridviewexp文件名
前台
获得的值你要作何操作那是你以后的事了,代码帖上即可 demo_gridviewexp文件名
前台
-
HTML code
-
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " demo_gridviewexp.aspx.cs " Inherits = " codeexample_demo_gridviewexp " %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html xmlns ="http://www.w3.org/1999/xhtml" > < head id ="Head1" runat ="server" > < title > 无标题页 </ title > </ head > < body > < form id ="form1" runat ="server" > < div > 实例之占击获得某一单元格的值 < asp:GridView ID ="GridView1" runat ="server" OnDataBound ="GridView1_DataBound" > </ asp:GridView > </ div > </ form > < script type ="text/javascript" > var isIe = window.navigator.appName.indexOf( " Netscape " ) == - 1 ? true : false ; function onCellClick(srcCell){ var sCellText = isIe ? srcCell.innerText:srcCell.textContent; alert(sCellText); } function initCellClick(){ var cells = document.getElementById( " <%=GridView1.ClientID %> " ).getElementsByTagName( " td " ); for ( var i = 0 ;i < cells.length; ++ i){ cells[i].onclick = " onCellClick(this) " ; } } // initCellClick(); </ script > </ body > </ html >
后台
-
C# code
-
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class codeexample_demo_gridviewexp : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { GridView1.DataSource = GenerateTable(); GridView1.DataBind(); } private DataTable GenerateTable() { DataTable dt = new DataTable(); dt.Columns.Add( " ID " , typeof ( int )); dt.Columns.Add( " Name " , typeof ( string )); DataRow row; Random rnd = new Random(); for ( int i = 1 ; i != 31 ; ++ i) { row = dt.NewRow(); row[ 0 ] = i; row[ 1 ] = " Product_ " + i; dt.Rows.Add(row); } return dt; } protected void GridView1_DataBound( object sender, EventArgs e) { GridViewRowCollection rows = GridView1.Rows; TableCellCollection cells; for ( int i = 0 ; i != rows.Count; ++ i) { cells = rows[i].Cells; for ( int j = 0 ; j != cells.Count; ++ j) { cells[j].Attributes.Add( " onclick " , " onCellClick(this) " ); } } } }
转自: http://topic.csdn.net/u/20080219/10/d0916682-b30b-4a8d-be92-ce305222db56.html