如何:响应 GridView 控件中的按钮事件
来源:互联网 发布:网络视频发展趋势 编辑:程序博客网 时间:2024/04/29 23:55
在单击 GridView 控件中的按钮时,将引发 RowCommand 事件。GridView 控件具有内置功能,用于进行编辑、删除和分页等操作。还可以添加按钮并使用 RowCommand 事件向控件添加自定义功能。
可以通过下面的方式向 GridView 控件添加自定义功能:
向 GridView 控件添加 ButtonField 控件。
向 GridView 控件中的模板添加 Button、LinkButton 或 ImageButton 控件。
可以使用事件参数的 CommandName 属性来标识按钮的功能。如果使用 ButtonField 或 TemplateField 控件,则还可以使用 CommandArgument 属性来标识当前行。如果使用 ButtonField 控件,则CommandArgument 属性自动设置为行索引。如果使用 TemplateField 控件,则该控件不会自动设置CommandArgument 属性。在这种情况下,如果必须在事件处理程序中确定行索引,则可以使用数据绑定表达式将该按钮的 CommandArgument 属性设置为行索引。
响应 GridView 控件中的按钮事件
将按钮的 CommandName 属性设置为标识其功能的字符串,如“打印”或“复制”。
如果使用 TemplateField 控件并且必须在事件处理程序方法中访问行索引,则将按钮的CommandArgument 属性设置为标识当前行的表达式。
下面的示例演示如何将 TemplateField 列中某个按钮的 CommandArgument 属性设置为当前行索引。在该示例中,该列包含一个显示购物车的 Button 控件。
Visual Basic<asp:TemplateField> <ItemTemplate> <asp:Button ID="AddButton" runat="server" CommandName="AddToCart" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Add to Cart" /> </ItemTemplate> </asp:TemplateField>
C#<asp:TemplateField> <ItemTemplate> <asp:Button ID="AddButton" runat="server" CommandName="AddToCart" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to Cart" /> </ItemTemplate> </asp:TemplateField>
为 GridView 控件的 RowCommand 事件创建一个方法。在该方法中,执行下列操作:
检查事件参数对象的 CommandName 属性来查看传入什么字符串。
如果需要,使用 CommandArgument 属性检索包含该按钮的行的索引。
为用户单击的按钮执行相应的逻辑。
下面的示例演示响应 GridView 控件中的按钮单击的方法。在该示例中,TemplateField 控件中的按钮发送命令“AddToCart”。RowCommand 事件处理程序确定被单击的按钮。如果被单击的是购物车按钮,则代码执行相应的逻辑。
Visual BasicProtected Sub GridView1_RowCommand(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) If (e.CommandName = "AddToCart") Then ' Retrieve the row index stored in the CommandArgument property. Dim index As Integer = Convert.ToInt32(e.CommandArgument) ' Retrieve the row that contains the button ' from the Rows collection. Dim row As GridViewRow = GridView1.Rows(index) ' Add code here to add the item to the shopping cart. End IfEnd Sub
C#protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){ if (e.CommandName == "AddToCart") { // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button // from the Rows collection. GridViewRow row = GridView1.Rows[index]; // Add code here to add the item to the shopping cart. } }
- 如何:响应 GridView 控件中的按钮事件
- 如何:响应 GridView 控件中的按钮事件
- 如何:响应 GridView 控件中的按钮事件
- 响应 GridView 控件中的按钮事件
- GridView控件中的事件
- GridView控件中的事件
- GridView控件中的事件
- 如何给activity中的控件增加事件响应代码
- winform中的datagridview里添加按钮列,如何响应按钮的点击事件
- [VB.NET]如何重载控件中的按钮事件?
- 关于Gridview表格中的按钮事件
- Winform中的按钮要响应回车事件
- Android如何让ListView的子组件按钮响应单击事件并修改该项子控件内容
- C#.NET GridView 中如何获取脚中的控件ID并添加客户端事件
- vc中的 drop down 类型的combo控件 如何响应回车事件
- 如何在list控件中响应右键弹出菜单中的菜单事件
- gridview行按钮事件
- gridview 按钮事件
- Delphi2009/C++Builder2009新特性
- dsadsadas
- c++builder+zeosdbo+postgresql环境的配置
- 堆栈,堆栈,堆和栈的区别(转)
- Tiburon遊記1
- 如何:响应 GridView 控件中的按钮事件
- Tiburon遊記2 DataSnap和JSON
- Tiburon遊記3 動手建立一個DataSnap JSON伺服器吧
- C++随笔分类列表(基础)
- JSP--隐含对象--exception
- C++随笔分类列表(高级)
- 用户与用户组
- Delphi中的消息钩子函数和Windows子类处理 入门篇
- 在maven工程引起 struts2-core发生的错误