如何事件添加到asp.net的GridView的按钮 [英] How to add events to buttons of GridView in asp.net
问题描述
我在asp.net一个搜索页面,用户搜索一本书,结果在一个gridview列。我添加了一个按钮,每一个GridView的结果列的权利,我想将事件添加到这些按钮,例如,当用户点击按钮,这本书借出。下面是它的截图:
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:
下面是我的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>
这是此页的SearchResult的.cs文件:
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)
{
}
}
我添加的按钮类似如下:点击ButtonField字段
I added the buttons like the following: Clicked on BUttonField
我的问题是,我怎么能一个事件添加到这些贷款项目按钮?我读这链接 http://msdn.microsoft.com/en-us/library /bb498195.aspx 但它并没有真正告诉事件处理程序是如何加入。我AP preciate任何帮助。谢谢
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
推荐答案
我会做什么,以及我想你的榜样链接是干什么的,是添加RowCommand事件到GridView。
What I would do, and what I think your example link is doing, is adding the RowCommand event to the GridView.
当你点击一个按钮时,RowCommand事件将被解雇,而的CommandName和CommandArgument(这将是标识与单击按钮相关的行/记录的ID)的按钮被传递到事件处理程序
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
而在你的codebehind:
And in your codebehind:
protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
会议将决定该处理程序的名称应为[控件ID] _ [事件名称]所以在我的例子,我的网格的ID只是电网
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屋!