GridView.RowCommand 事件
当单击 GridView控件中的按钮时发生。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
在单击 GridView 控件中的按钮时,将引发 RowCommand 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时执行一个自定义例程。
GridView 控件中的按钮也可调用该控件的某些内置功能。若要执行这些操作之一,请将按钮的 CommandName 属性设置为下表中的某个值。
CommandName 值 |
说明 |
---|---|
“Cancel” |
取消编辑操作并将 GridView 控件返回为只读模式。引发 RowCancelingEdit 事件。 |
“Delete” |
删除当前记录。引发 RowDeleting 和 RowDeleted 事件。 |
“Edit” |
将当前记录置于编辑模式。引发 RowEditing 事件。 |
“Page” |
执行分页操作。将按钮的 CommandArgument 属性设置为“First”、“Last”、“Next”、“Prev”或页码,以指定要执行的分页操作类型。引发 PageIndexChanging 和 PageIndexChanged 事件。 |
“Select” |
选择当前记录。引发 SelectedIndexChanging 和 SelectedIndexChanged 事件。 |
“Sort” |
对 GridView 控件进行排序。引发 Sorting 和 Sorted 事件。 |
“Update” |
更新数据源中的当前记录。引发 RowUpdating 和 RowUpdated 事件。 |
尽管单击上表中所列出的按钮时将引发 RowCommand 事件,但仍建议您使用该表中列出的事件来执行该操作。
将 GridViewCommandEventArgs 对象传递到事件处理方法,以便您可以确定被单击按钮的命令名和命令参数。
注意 |
---|
GridViewCommandEventArgs 类未包含一个用于指示单击按钮所在行的属性。如果需要知道哪个行引发了事件,请使用 CommandArgument 属性将行的索引传给事件处理方法。 |
下面的代码示例演示如何使用 RowCommand 事件在单击某行的“添加”按钮时将客户名称从 GridView 控件添加到 ListBox 控件。
<%@ Page language="C#" %> <script runat="server">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];
// Create a new ListItem object for the customer in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text);
// If the customer is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!CustomersListBox.Items.Contains(item))
{
CustomersListBox.Items.Add(item);
}
}
}
void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
</script>
<html>
<body>
<form runat="server">
<h3>GridView RowCommand Example</h3>
<table width="100%">
<tr>
<td width="50%">
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
onrowcreated="CustomersGridView_RowCreated"
runat="server">
<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="City"
headertext="City"/>
</columns>
</asp:gridview>
</td>
<td valign="top" width="50%">
Customers: <br/>
<asp:listbox id="CustomersListBox"
runat="server"/>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
转载于:https://www.cnblogs.com/12go/archive/2011/08/30/2159805.html
GridView.RowCommand 事件相关推荐
- GridView RowCommand事件中取得當前行
Dim row As GridViewRow = CType(CType(e.CommandSource, Button).NamingContainer, GridViewRow) '取得當前行 D ...
- [GridView]在 RowCommand事件中,自订的Button 如何取出某一列的索引值(RowIndex)
这是我的文章备份,有空请到我的网站走走, http://www.dotblogs.com.tw/mis2000lab/ 才能掌握我提供的第一手信息,谢谢您. http://www.dotblogs.c ...
- Android 开发之 GridView及其事件监听
2019独角兽企业重金招聘Python工程师标准>>> 步骤: //1.准备数据源 //2.新建适配器 //3.GridView加载适配器 //4.GridView配置事件监听器 ` ...
- GridView的RowCommand事件中取得行索引 技巧
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == ...
- 【ASP.NET】演绎GridView基本操作事件
对于gridview学NET的同学再熟悉不过,但是其中功能事件是否能编码熟练实现?前不久看点博文,以及资料,综合自己的一些想法,汇总如下: 数据库设计如下,以便更好理解: 设计: 实现: GridVi ...
- GridView RowCommand (handle with e.CommandArgument)
转自:http://www.pluralsight.com/blogs/fritz/archive/2005/06/24/11975.aspx I've been working on a proje ...
- gridview SelectedIndexChanged事件
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False&q ...
- GridView RowCommand 获取列值
//根据按钮属性,进行操作 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) ...
- ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList
有时候希望在 GridView 模板中使用自动回发的 CheckBox (autopostback=true) ,但是 CheckBox 没有 CommandName 属性,因此也就无法在 GridV ...
- GridView自定义分页样式(上一页,下一页,到第几页)(新手教程)
今天要为网站做一个文章列表,发现GridView的分页样式很难看,于是结合网上的例子,自己做了一个.不是很美观,不过还是很实用的,先看下效果吧,如图(1).演示地址http://www.veryam. ...
最新文章
- linux代码中能出现中文吗_Linux命令很熟悉,你知道它们的英文全称和中文解释吗?...
- Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist的
- 常用的SSH注解标签
- SpringMVC基础配置与简单的SpringMVC的程序
- 协程asyncio_Python 异步模块 asyncio 中的协程与任务
- java学习(7):巩固练习
- bec初级第一课_在您的第一个初级开发人员工作中如何生存和发展
- (53)Xilinx时钟原语-BUG与IBUFG(第11天)
- 嘘!你与谷歌语音助手的对话,可能已经泄露……
- 库克遭一名自称其妻子的陌生女子威胁 苹果紧急申请限制令
- Androidstudio查不出具体哪行报错解决办法
- 华为防火墙管理员角色和级别详解
- 数据库课程设计大作业大盘点【建议在校生收藏】
- Linux 基本命令入门
- 如何使用计算机word,电脑系统教程:电脑Word分栏怎么用
- python量化选股策略 源码_【一点资讯】Python实现行业轮动量化选股【附完整源码】...
- 工作室多wifi软路由工作室Ros软路由使用教程
- 惠普打印机墨盒更换教程_惠普打印机加墨教程:老司机教你
- Excel的VLOOKUP函数及其用法
- Keil uVision5 MDK(ARM)软件的介绍、下载、安装与注册
热门文章
- zookeeper + kafka 集群安装部署教程(linux环境下)
- CCF - 201604-2 - 俄罗斯方块
- JavaScript必须了解的知识点总结【转】
- C# 获取所有网卡信息
- 《无码的青春》第七章	御姐
- 我是如何战胜懒惰的?
- Oracle创建上下文 SYS_CONTEXT
- Java中线程出现Exception in thread Thread-0 java.lang.IllegalMonitorStateException异常 解决方法...
- javascript 笔记--变量
- Jmeter插件监控服务器性能