如何在asp.net中向GridView的按钮添加事件 [英] How to add events to buttons of GridView in asp.net

查看:33
本文介绍了如何在asp.net中向GridView的按钮添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 asp.net 中有一个搜索页面,用户搜索一本书,结果列在 gridview 中.我在每个 gridview 结果列的右侧添加了一个按钮,并且我想向这些按钮添加一个事件,例如,当用户单击该按钮时,该书被借出.这是它的屏幕截图:

这是我的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchResults.aspx.cs" Inherits="Pages_SearchResults" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title><身体><form id="form1" runat="server"><div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"DataKeyNames="ISBN" DataSourceID="SqlDataSource1"onselectedindexchanged="GridView1_SelectedIndexChanged"><列><asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title"/><asp:BoundField DataField="ISBN" HeaderText="ISBN" ReadOnly="True"SortExpression="ISBN"/><asp:BoundField DataField="作者姓名" HeaderText="作者姓名"SortExpression="作者姓名"/><asp:BoundField DataField="AuthorlName" HeaderText="作者姓氏"SortExpression="作者姓名"/><asp:BoundField DataField="ItemType" HeaderText="Item Type"SortExpression="ItemType"/><asp:BoundField DataField="PublishYear" HeaderText="发布年份"SortExpression="PublishYear"/><asp:ButtonField ButtonType="Button" CommandName="LoanItem" Text="Loan Item"/></列></asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"SelectCommand="SELECT * FROM [Items] WHERE ([Title] LIKE '%' + @Title + '%')"><选择参数><asp:FormParameter FormField="tSearchBox" Name="Title" Type="String"/></选择参数></asp:SqlDataSource></表单>

这里是这个 SearchResults 页面的 .cs 文件:

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;公共部分类 Pages_SearchResults : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){}}

我添加了如下按钮:点击 BUttonField

我的问题是,如何向那些贷款项目"按钮添加事件?我阅读了此链接 http://msdn.microsoft.com/en-us/library/bb498195.aspx 但它并没有真正说明事件处理程序是如何添加的.我很感激任何帮助.谢谢

解决方案

我会做的,以及我认为您的示例链接正在做的,是将 RowCommand 事件添加到 GridView.

当您单击按钮时,将触发 RowCommand 事件,并将按钮的 CommandName 和 CommandArgument(这将是标识与单击的按钮关联的行/记录的 ID)传递给事件处理程序.

要创建处理程序,请参阅下面的评论,或手动创建.在您的网格中:

OnRowCommand="Grid_RowCommand"

在你的代码隐藏中:

protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e){}

约定规定处理程序的名称应该是 [ControlID]_[EventName] 所以在我的例子中,我的网格的 ID 只是 Grid

I have a search page in asp.net, user searches a book and results are listed in a gridview. I added a button to the right of each gridview result column, and i want to add an event to these buttons, for example, when user clicks the button, that book is loaned. Here is a screenshot of it:

Here is my code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchResults.aspx.cs"  Inherits="Pages_SearchResults" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ISBN" DataSourceID="SqlDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="ISBN" HeaderText="ISBN" ReadOnly="True" 
            SortExpression="ISBN" />
        <asp:BoundField DataField="AuthorName" HeaderText="Author Name" 
            SortExpression="AuthorName" />
        <asp:BoundField DataField="AuthorlName" HeaderText="Author Last Name" 
            SortExpression="AuthorlName" />
        <asp:BoundField DataField="ItemType" HeaderText="Item Type" 
            SortExpression="ItemType" />
        <asp:BoundField DataField="PublishYear" HeaderText="Publish Year" 
            SortExpression="PublishYear" />
        <asp:ButtonField ButtonType="Button" CommandName="LoanItem" Text="Loan Item" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Items] WHERE ([Title] LIKE '%' + @Title + '%')">
    <SelectParameters>
        <asp:FormParameter FormField="tSearchBox" Name="Title" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>

And here is the .cs file of this SearchResults page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SearchResults : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

I added the buttons like the following: Clicked on BUttonField

My question is, how can i add an event to those "Loan Item" buttons? I read this link http://msdn.microsoft.com/en-us/library/bb498195.aspx but it does not really tell how the event handler is added. I appreciate any help. Thanks

解决方案

What I would do, and what I think your example link is doing, is adding the RowCommand event to the GridView.

When you click a button, the RowCommand event will be fired, and the CommandName and CommandArgument (which would be an ID that identifies the row/record associated with the button that was clicked) for the button are passed to the event handler.

To create the handler, see my comment below, or do it manually. In your grid:

OnRowCommand="Grid_RowCommand"

And in your codebehind:

protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
{

}

Convention would dictate that the name of the handler should be [ControlID]_[EventName] so in my example, the ID of my grid is simply Grid

这篇关于如何在asp.net中向GridView的按钮添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆