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的专栏
相关新闻>>
- 发表评论
-
- 最新评论 进入详细评论页>>
今日头条
更多>>您可能感兴趣的文章
- asp.net DataTable和Dataset序列化成Json格式
- .net架构的最后思考(箴言)
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注
- .NET设计模式:工厂方法模式(Factory Method)[1]
- Web Service学习笔记(4)
- 聊聊.net程序设计——浅谈使用VS2010建模拓展(下
- 使用ASP.NET MVC3+EF+Jquery制作文字直播系统(四
- .NET简谈互操作(四:基础知识之Dispose非托管内存
- JQuery+Asp.net MVC实现用户名重名查询
- MVC3+Entity Framework 实现投票系统(二)



