阅读内容 

给GridView添加行单击事件

[日期:2007-10-24] 来源:  作者: [字体: ]
    
  功能:
  
  1:添加行单击事件
  2:获取行里面的单元格值
  
  
  主要代码:
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
   Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
   if (btnHiddenPostButton != null) {
   e.Row.Attributes["onclick"] = String.Format("JavaScript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);
   // 额外样式定义
   e.Row.Attributes["onmouseover"] = "JavaScript:this.style.background='red'";
   e.Row.Attributes["onmouseout"] = "JavaScript:this.style.background=''";
   e.Row.Attributes["style"] = "cursor:pointer";
   e.Row.Attributes["title"] = "单击选择当前行";
   }
   // 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引
   //e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
   }
  
   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
  
   GridViewRow row = null;
  
   Label lbl = null;
   switch (e.CommandName) {
   case "HiddenPostButtonCommand": // 模板列
   Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
   row = cmdControl.NamingContainer as GridViewRow; // 当前行
   // 如何访问单元格值
   // string txt = row.Cells[0].Text;
   // 如何获取模板列中的 Label
   lbl = row.FindControl("MyLabelID") as Label;
   // 执行更多的自定义操作
   //
   //
   string txt = lbl.Text.ToString();
   Response.Write(txt);
   Response.Write(String.Format("GridView Version 当前第 {0} 行:", row.RowIndex + 1));
   break;
   // case "Command2":
   // more cases
   //
   }
   }  
阅读:
录入:blue1000

推荐 】 【 打印
相关新闻      
本文评论       全部评论
发表评论
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款


点评: 字数
姓名:
Advertisement
内容查询


Advertisement