ASP.NET DEMO 5:如何在GridView的RowCommand事件中获取当前的GridViewRow
2007-07-05 01:12
731 查看
由于事件参数 GridViewCommandEventArgs 并不公开Row属性指示当前行,(DataGridCommandEventArgs 公开 Item 属性以获取当前 DataGridItem,不知 ASP.NET Team 是如何考虑这一设计的),因此需要一点“技巧”来获取此属性。
其实这是一个早就已知的问题,鉴于CSDN里面每每有人疑惑,这里稍微整理下,便于参考。
至少有三种方法可以使用,其中给 CommandArgument 绑定 RowIndex 是最常见的方法,也是 MSDN 提供的,然而实际上只需要充分利用控件层次与事件参数就足够可以回溯出来:sender 与 NamingContainer/BindingContainer,具体见代码。
下载:/Files/Jinglecat/Demo_AccessRowOnGridViewCommand.rar
其实这是一个早就已知的问题,鉴于CSDN里面每每有人疑惑,这里稍微整理下,便于参考。
至少有三种方法可以使用,其中给 CommandArgument 绑定 RowIndex 是最常见的方法,也是 MSDN 提供的,然而实际上只需要充分利用控件层次与事件参数就足够可以回溯出来:sender 与 NamingContainer/BindingContainer,具体见代码。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:BoundField DataField="ProductName" HeaderText="OldProductName" /> <asp:TemplateField HeaderText="NewProductName"> <ItemTemplate> <asp:TextBox ID="txtNewProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button1" CommandName="Command1" CommandArgument='<%# Container.DataItemIndex %>' runat="server" Text="Command1" /> <asp:Button ID="Button2" CommandName="Command2" runat="server" Text="Command2" /> </ItemTemplate> </asp:TemplateField> <asp:ButtonField CommandName="Command3" Text="Command3" /> </Columns> </asp:GridView> </div> </form> </body> </html>
下载:/Files/Jinglecat/Demo_AccessRowOnGridViewCommand.rar
相关文章推荐
- 如何在GridView的RowCommand事件中获取当前的GridViewRow
- 菜鸟学Asp.Net——GridView控件之RowDataBind事件中如何获取已绑定的数据
- 如何在GridView的RowCommand事件中获取当前的GridViewRow
- 如何在GridView的RowCommand事件中获取当前的GridViewRow
- 如何在GridView的RowCommand事件中获取当前的GridViewRow
- asp.net中GridView内部点击事件判断是第几行并获取row数据
- asp.net中获取GridView当前行的索引
- asp.net学习之GridView事件、GridViewRow对象
- ASP.NET使用gridview获取当前行的索引值
- ASP.NET使用gridview获取当前行的索引值
- Asp.net技巧:gridview获取当前行索引的方法
- ASP.NET 2.0 GridView的RowCommand事件中取得行索
- ASP.NET DEMO 10: 如何通过 javascript 访问 GridView/DataGrid 选中 CheckBox 行各列的值
- gridview使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- ASP.NET DEMO 15: 同时支持行单击和双击事件的 GridView/DataGrid
- [ASP.NET]GridView的Row.Cells[ColumnIndex]如何改用ColumnName來使用
- 如何在ASP.NET MVC 中获取当前URL、controller、action
- Asp.net 中 GridView 在 RowDataBound事件中获取 某列值 的方法
- ASP.NET DEMO 15: 同时支持行单击和双击事件的 GridView/DataGrid
- ASP.NET中gridview获取当前行的索引值