4

我在 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="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>

这是此 SearchResults 页面的 .cs 文件:

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)
    {

    }
}

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

在此处输入图像描述

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

4

3 回答 3

5

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

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

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

OnRowCommand="Grid_RowCommand"

在您的代码隐藏中:

protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
{

}

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

于 2013-05-24T13:22:49.273 回答
3

如果您通过向导添加按钮,则GridView1_SelectedIndexChanged每次单击行按钮时都会触发。因此,您需要做的就是找出选定的行并采取相应的行动。

GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

请注意,如果您没有另行指定,则可能有多个选定的行。

于 2013-05-24T13:12:12.803 回答
3

改变

  <asp:ButtonField ButtonType="Button" CommandName="LoanItem" Text="Loan Item" />

     <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="Buttonid" runat="server" CommandName="LoanItem" Text="Loan Item" OnClick="Button_click_event"></asp:Button>    
        </ItemTemplate>
 </asp:TemplateField>

在后面的代码中

Private void Button_click_event(Object sender,EventArgs e)
{
// Click Event of Button
}
于 2013-05-24T13:12:46.170 回答