asp.net gridview中增加单击单元格事件
实现功能:单击表格中某个单元格(不是第一列、最后一列、最后一行,不为0)根据行第一个单元格内容及列名来查询详细内容,在消息框中查看显示。
在代码中增加
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridViewTzx.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = ; columnIndex < r.Cells.Count; columnIndex++)//(int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString());
}
}
}
base.Render(writer);
}
protected void GridViewTzx_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
// 从第一个单元格内获得LinkButton控件
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// 返回一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, ""); // 给每一个可编辑的单元格增加事件
for (int columnIndex = ; columnIndex < e.Row.Cells.Count-; columnIndex++)//int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++)
{
// 增加列索引作为事件参数
string js = _jsSingle.Insert(_jsSingle.Length - , columnIndex.ToString());
// 给单元格增加onclick事件
e.Row.Cells[columnIndex].Attributes["onclick"] = js;
// 给单元格增加鼠标经过时指针样式
e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
}
}
}
protected void GridViewTzx_RowCommand(object sender, GridViewCommandEventArgs e)
{
string sXianghaos = "";
int _rowIndex = int.Parse(e.CommandArgument.ToString());
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
if (GridViewTzx.Rows[_rowIndex].Cells[].Text != "汇总" && GridViewTzx.Rows[_rowIndex].Cells[_columnIndex].Text != "" && _columnIndex < && _columnIndex > )
{
string sGk = GridViewTzx.Columns[_columnIndex].HeaderText;
string sXx = GridViewTzx.Rows[_rowIndex].Cells[].Text;
string sSubCmd = "id in (select max(id) as id from jzx_active group by xianghao) and XIANGHAO in (select boxno from boxnumber where boxtypeid = (select id from boxtype where type ='"+sXx+"') )";
string sCmd = "";
sCmd = "select xianghao from jzx_active where (QYG like'" + sGk + "%' or MDG like'" + sGk + "%') and " + sSubCmd ;
if (sGk=="租出")
{
sCmd = "SELECT xianghao from jzx_active where ZHUANGTAI='" + sGk + "' and " + sSubCmd;
} MySqlDataReader reader = null; mySqlMod newMySqlMod = new mySqlMod();
newMySqlMod.RunSQL(sCmd, out reader); if (reader.HasRows)
{
while (reader.Read())
{
sXianghaos += reader[].ToString();
sXianghaos += ",";
}
sXianghaos = sXianghaos.TrimEnd(',');
CommData.MessageBoxAsyncPostBack(this, GetType(), sXianghaos);
}
reader.Close(); }
}
参考资料:原文http://www.codeproject.com/Articles/18136/Edit-Individual-GridView-Cells-in-ASP-NET翻译:webabcd
GridView有一个不可见的asp:ButtonField控件,它处于GridView的第一列,名为“SingleClick”。 它用于给GridView的数据行增加单击事件。
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick"
Visible="False" />
</Columns>
在RowDataBound事件内循环为每一数据行的每一单元格增加单击事件。 使用单元格在数据行中的索引作为事件参数,这样在单元格触发了单击事件后我们就可以知道到底是哪个单元格被单击了。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 从第一个单元格内获得LinkButton控件
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// 返回一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, ""); // 给每一个可编辑的单元格增加事件
for (int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++)
{
// 增加列索引作为事件参数
string js = _jsSingle.Insert(_jsSingle.Length - , columnIndex.ToString());
// 给单元格增加onclick事件
e.Row.Cells[columnIndex].Attributes["onclick"] = js;
// 给单元格增加鼠标经过时指针样式
e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
}
}
}
在RowCommand事件内读出命令参数和事件参数。 这会告诉我们被选中的行和列的索引。
int _rowIndex = int.Parse(e.CommandArgument.ToString());
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
为了验证而注册回发和回调数据
在RowDataBound中创建的自定义事件必须要在页中注册。 通过重写Render方法来调用ClientScriptManager.RegisterForEventValidation。 通过GridViewRow.UniqueID返回行的唯一ID,按纽的唯一ID通过在行的唯一ID后附加“$ct100”而生成。
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString());
}
}
} base.Render(writer);
}
这将防止任何“回发或回调参数无效”的错误。
asp.net gridview中增加单击单元格事件的更多相关文章
- Gridview 重建表头/单击单元格弹出对话框/改变单元格背景色
整理工作~ 完整的代码在GitHub上, 路径: 项目背景:追踪某个issue,并且记录每天的状态. 要求:1.点击日期就能更改,并且用颜色标志不同的状态 2.增加按钮可关闭issue 3.布局要求日 ...
- 在DBGrid中,单击单元格选择整行,双击又可编辑单元格
在设计过程中,有时候数据较大量,field 较多的时候,只是点击单元格可能会对某个field的数据误操作(如数据错行),为此才会想到这个问题,解决办法如下:点击单元格就改当前行颜色. 首先DBGRID ...
- easyui datagrid单击单元格选择此列
示例代码实现单击jquery easyui datagrid的单元格时,取消datagrid默认选中高亮此行的样式,改为选中单击的单元格所在的列,高亮此列上的所有单元格.可以配置全局single变量, ...
- C# 获取Excel中的合并单元格
C# 获取Excel中的合并单元格 我们在制作表格时,有时经常需要合并及取消合并一些单元格.在取消合并单元格时需要逐个查找及取消,比较麻烦.这里分享一个简单的方法来识别Excel中的合并单元格,识别这 ...
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...
- excel中,一系列单元格中包含某一个字段的单元格数量?
excel中,一系列单元格中包含某一个字段的单元格数量?这个怎么写公式?如:A列单元格A1-A7的内容分别为 A.AB.BC.AC.CD.AD.EA,怎么数这一列中几个单元格的内容包含A字母? 任意单 ...
- Excel 2010 Alt+; (分号) --- “只选定当前选定区域中的可视单元格”
excel怎样把筛选出来的加上颜色? 1.选中筛选结果数据区域: 2.同时按下Alt+; (分号)键,选中筛选出的数据: 3.鼠标右键,设置单元格格式: 4.在弹出的对话框中,设置字体.背景颜色,即可 ...
- 无法读取Excel中的数据单元格。有数据,但是读出来全是空值
C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...
- ASP.NET Core中实现单体程序的事件发布/订阅
标题:ASP.NET Core中实现单体程序的事件发布/订阅 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/10468058.html 项目源代码: ...
随机推荐
- poj 3575 Crosses and Crosses(SG函数)
Crosses and Crosses Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3063 Accepted: 11 ...
- UVa11090 Going in Cycle!!
UVa11090 Going in Cycle!! 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34650 [思路] ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- TCP连接建立的三次握手过程可以携带数据吗?
前几天实验室的群里扔出了这样一个问题:TCP连接建立的三次握手过程可以携带数据吗?突然发现自己还真不清楚这个问题,平日里用tcpdump或者Wireshark抓包时,从来没留意过第三次握手的ACK包有 ...
- 362. Design Hit Counter
这个傻逼题..我没弄明白 you may assume that calls are being made to the system in chronological order (ie, the ...
- 386. Lexicographical Numbers
用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再... 是吧-- 比N大了BREAK就行. 注意第一个循环是1-9,往后的循环是0-9. pub ...
- HTML5 Security Cheatsheet使用说明
1.URL: https://html5sec.org/ 2.通过点击如图button(也可点击其他:xss firefox)那行的button可以搜索所有button的Cheatsheet,查看都有 ...
- poj1691绘画板
1 7 0 0 2 2 1 0 2 1 6 2 2 0 4 2 1 1 2 4 4 2 1 4 3 6 1 4 0 6 4 1 3 4 6 6 2 #include<stdio.h> #i ...
- google API的.NET库
Goolge发布了一个新的google API .NET库,是一个Portable Class Library,所以无论是.NET,WinTRy,Windows Phone或者Silverlight都 ...
- jquery 实现页面拖拽并保存到cookie
实现的效果就是页面内的图片可拖拽到任意位置,并将所在位置保存.下次打开页面依然可见.本文是作demo用,实际开发中,位置的数据应保存到数据库中. 好了,开始. 1.准备工作. a.jquery(1.7 ...