首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>NET专区>ASP.NET>实例讲解GridView/DataGri同时支持行单击和双击事件
实例讲解GridView/DataGri同时支持行单击和双击事件
来源: 发布时间:2008-08-28 发布人: 浏览: 人次   字体: [ ]  

本文实例讲解如何实现既有单击又有双击的GridView控件
功能介绍:
单击行弹出当前行详细页面 双击行进入编辑状态(GridView/DataGrid内置 Edit) 说明: 单击事件(onclick)使用了 setTimeout 延迟,根据实际需要修改延迟时间 常见处理行方式会选择在
RowDataBound/ItemDataBound 中处理,这里我选择 Page.Render 中处理,至少基于以下考虑RowDataBound 仅仅在调用 DataBind 之后才会触发,回发通过 ViewState 创建空件不触发 假如需要更多的处理,你需要分开部分逻辑到 RowCreated 等事件中 并且我们希望使用ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法 进行
安全脚本的注册,而后者需要在页的 Render 阶段中才能处理 关于“DataGrid中采取的辅助按钮支持回发”见ASP.NET DEMO8: 为 GridView 每行添加服务器事件 PS:未实现 Edit 对应的 Update/Cancel ,根据需要自行添加即可。

可直接运行源码(单页 .aspx),在后面有下载:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<%--http://community.csdn.net/Expert/TopicView3.asp?id=5767096--%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack) {
    LoadGridViewProductData();
    LoadDataGridProductData();
  }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  /**//*
    当然可以在这里进行客户端脚本绑定,
    但是,我选择在重载页的 Render 方法中处理,因为
    1. RowDataBound 仅仅在调用 DataBind 之后才会触发,回发通过 ViewState 创建空件不触发假如需要更多的处理,你需要分开部分逻辑到 RowCreated 等事件中
    2. 并且我们希望使用ClientScript.GetPostBackEventReference 和ClientScript.RegisterForEventValidation 方法 进行安全脚本的注册,而后者需要在页的 Render 阶段中才能处理    
  */
}  

protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
  // 隐藏辅助按钮列
  int cellIndex = 0;
  e.Item.Cells[cellIndex].Attributes["style"] = "display:none";
}  

void LoadGridViewProductData()
{
  DataTable dt = CreateSampleProductData();

  GridView1.DataSource = dt;
  GridView1.DataBind();  
}

void LoadDataGridProductData()
{
  DataTable dt = CreateSampleProductData();

  DataGrid1.DataSource = dt;
  DataGrid1.DataBind();
}

sample data#region sample data

static DataTable CreateSampleProductData()
{
  DataTable tbl = new DataTable("Products");

  tbl.Columns.Add("ProductID", typeof(int));
  tbl.Columns.Add("ProductName", typeof(string));    
  tbl.Columns.Add("UnitPrice", typeof(decimal));
  tbl.Columns.Add("CategoryID", typeof(int));

  tbl.Rows.Add(1, "Chai", 18, 1);
  tbl.Rows.Add(2, "Chang", 19, 1);
  tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
  tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2);
  tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2);
  tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
  tbl.Rows.Add(48, "Chocolade", 12.75, 3);
  tbl.Rows.Add(49, "Maxilaku", 20, 3);    

  return tbl;
}
本文实例代码下载:点击此次下载

#endregion  
 
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
  GridView1.EditIndex = e.NewEditIndex;
  LoadGridViewProductData();
}

protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
  DataGrid1.EditItemIndex = e.Item.ItemIndex;
  LoadDataGridProductData();
}

protected override void Render(HtmlTextWriter writer)
{
  // GridView
  foreach (GridViewRow row in GridView1.Rows) {
    if (row.RowState == DataControlRowState.Edit) { // 编辑状态
      row.Attributes.Remove("onclick");
      row.Attributes.Remove("ondblclick");
      row.Attributes.Remove("style");
      row.Attributes["title"] = "编辑行";
      continue;
    }
    if (row.RowType == DataControlRowType.DataRow) {
      //单击事件,为了响应双击事件,延迟 1 s,根据需要可能需要增加延迟
      row.Attributes["onclick"] = String.Format("javascript:setTimeout(\"window.open('DummyProductDetail.aspx?productid={0}')\
", 1000*1);event.cancelBubble=true;", GridView1.DataKeys[row.RowIndex].Value.ToString());
      // 获取ASP.NET内置回发脚本函数,返回 __doPostBack(<<EventTarget>>,
<<EventArgument>>)
      // 可直接硬编码写入脚本,不推荐
      row.Attributes["ondblclick"] =
ClientScript.GetPostBackEventReference(GridView1, "Edit$" + row.RowIndex.ToString(), true);
      //
      row.Attributes["style"] = "cursor:pointer";
      row.Attributes["title"] = "单击打开详细页面、双击进入编辑";
    }
  }


共2页: 上一页 1 [2] 下一页
相 关 文 章   发布商链接
·Asp.net实现加密和解密的运算代码实...
·Asp.net+AJAX实现的省市县无刷新级联...
·实例讲解asp.net一次预览并上传多张...
·asp.net中页面Div层内部的控件在垂直...
·解决B/S系统开发中Activex控件需要激...
·asp.net如何自定义配置文件的探讨
·如何在asp.net刷新页面时不提示 "重...
·如何在asp.net中使用javascript获取s...
·asp.net动态生成txt文件并提供用户下...
·如何给FCKeditor编辑器进行精简操作
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·如何实现在DataList控件中的D...
·实例讲解asp.net生成高质量缩
·asp.net适用于IE或FireFox的...
·举例详解C#代码与javaScript...
·asp.net无刷新上传文件
·在Ie中保存图片时出现"800700...
·asp.net文件上传大小限制的控
·asp.net动态生成txt文本文件...
·asp.net实现将Excel文件导入...
·实现多列,带图片的DropDownLi...
·javascript实现TreeView 控件
·ASP.NET配置Word的操作权限
·让FileUpload控件在IE和FireF...
·ASP.NET实现Office文档的分类
·asp.net文件下载显示进度条的
热 门 文 章
·asp.net(C#)上传下载及文件管...
·图解asp.net如何用excel做报...
·asp.net无刷新上传文件
·Asp.Net防止刷新重复提交数据...
·asp.net实现将Excel文件导入...
·asp.net中Web.Config配置文件...
·ASP.NET取得物理路径和虚拟路...
·asp.net(c#)生成验证码代码,...
·asp.net页面回传与js调用服务...
·asp.net(C#版)实现登录验证码...
·DataGrid中DropDownList触发S...
·ASP.net 实现批量数据更新或...
·asp.net中DataBinder.Eval的...
·asp.net可输入的下拉框复合控...
·asp.net中常见的几种日历控件...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .