Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add events to buttons of GridView in asp.net

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:

enter image description here

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

enter image description here

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

like image 478
yrazlik Avatar asked Dec 21 '22 05:12

yrazlik


1 Answers

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

like image 154
Jason P Avatar answered Jan 12 '23 06:01

Jason P