实例之点击获得某一单元格,获得单元格的值

本文介绍了一个ASP.NET中GridView控件的使用示例,通过JavaScript实现了点击GridView中的任意单元格时弹窗显示该单元格的文本内容。示例包括了HTML页面布局、JavaScript交互代码及C#后台代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给你一实例吧 -- 实例之点击获得某一单元格,获得单元格的值

获得的值你要作何操作那是你以后的事了,代码帖上即可 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.youkuaiyun.com/u/20080219/10/d0916682-b30b-4a8d-be92-ce305222db56.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值