ASP.NET中获取DridView点击的按钮后的索引
<span style="font-family:'Microsoft YaHei';font-size:13px;"><%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
/*
若要确定引发命令事件的记录索引,请使用事件参数的CommandArgument 属性,该事件参数传递到数据绑定控件的命令事件。ButtonField 类自动以适当的索引值填充CommandArgument 属性。
*/
int index = Convert.ToInt32(e.CommandArgument);
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
// Display the selected author.
Message.Text = "You selected " + contact + ".";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ButtonField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ButtonField Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"
AssociatedControlID="CustomersGridView"/>
<!-- Populate the Columns collection declaratively. -->
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="ContactName"
headertext="Contact Name"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnection%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
</span>
摘自 hi_dzj的专栏
相关新闻>>
- 发表评论
-
- 最新评论 进入详细评论页>>
今日头条
更多>>您可能感兴趣的文章
- 谈.Net委托与线程——创建无阻塞的异步调用(一
- 教你如何来恢复一个丢失的数据文件
- 微软ASP.NET站点部署指南(11):部署SQL Server数据
- ASP.NET数据格式的Format--DataFormatString
- asp.net js模拟Button点击事件
- Asp.net MVC源码分析--Model Validation(Client端)实现(2)
- Pro ASP.NET MVC 3 Framework学习笔记之九
- winform下通过webclient使用非流方式上传(post)数据和
- asp.net 六大内置对象(2)
- MVC中一个表单实现多个提交按钮(一个action搞定