实现功能:单击表格中某个单元格(不是第一列、最后一列、最后一行,不为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);
}

这将防止任何“回发或回调参数无效”的错误。

最新文章

  1. android
  2. [译]Node.js Best Practices
  3. QTP 10 安装及破解
  4. web服务器 应用 服务器
  5. Sina App Engine(SAE)入门教程(9)- SaeMail(邮件)使用
  6. 微信中web页面实现和公众号中查看图片一样的效果
  7. “_In_opt_z_”: 未声明的标识符
  8. 自己动手系列——实现一个简单的LinkedList
  9. 【CSS/JS学习】如何实现单行/多行文本溢出的省略(...)--老司机绕过坑道的正确姿势
  10. SQLServer之附加数据库
  11. mybatis 查询单个对象,结果集类型一定要明确
  12. Docker Mysql数据库主从同步配置方法
  13. 基于Docker+Prometheus+Grafana监控SpringBoot健康信息
  14. Google Reader 快关了!!
  15. 【react】关于react框架使用的一些细节要点的思考
  16. Eclipse 插件安装报错问题(已解决)
  17. 从零开始编写自己的JavaScript框架(一)
  18. localhost与127.0.0.1及本机ip的区别
  19. MySQL 插入emoji 表情
  20. 任务查询系统(cqoi2015,bzoj3932)(主席树)

热门文章

  1. java 运行项目不放到tomcat下的webapps文件夹下放到自己建的文件夹中的处理办法
  2. IDEA新建SpringMVC项目报错解决办法
  3. js冲突怎么解决
  4. Android中的FrameLayout帧布局
  5. 经常使用虚拟现实仿真软件总汇(zz)
  6. mybatis0207 resultType、resultMap、延迟加载使用场景总结
  7. mapreduce实战:统计美国各个气象站30年来的平均气温项目分析
  8. Java中获得程序当前路径的4中方法
  9. 关于String的hashCode
  10. Rouh set 入门知识1(基础定义篇)