Monday, 18 July 2016

How to add click event to rows in Gridview in asp.net

Add Row Click event to GridView Rows in ASP.Net

Add Row Click event to GridView Rows in ASP.Net
Description:

In this example we explain that how to add Row Click event in GridView in asp.net using C#.or how to add row click event to Gridview Rows in asp.net.

Sometime we have requirement like when user click on Gridview Row then it will display its related information on popup.

Here we demonstrate how to pass row index in the row click of the gridview and perform some operation.we used gridviews onSelectedIndexChanged event to perform the row click functionality of the gridview.


Below is the code for Add Row Click event to Gridview Rows in asp.net

GridviewRowClickEventDemo.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewRowClickEventDemo.aspx.cs"
    EnableEventValidation="false" Inherits="GridviewRowClickEventDemo" %>

<!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>Add Row Click event to GridView Rows in ASP.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="ged_Employee" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="Blue"
        runat="server" AutoGenerateColumns="False" OnRowDataBound="ged_Employee_OnRowDataBound"
        OnSelectedIndexChanged="ged_Employee_OnSelectedIndexChanged" CellPadding="4"
        ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Emp Name" ItemStyle-Width="150">
                <ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="Department" HeaderText="Department" ItemStyle-Width="150">
                <ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="Salary" HeaderText="Salary" ItemStyle-Width="150">
                <ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True"></HeaderStyle>
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>
    </form>
</body>
</html>

GridviewRowClickEventDemo.aspx.cs:

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

public partial class GridviewRowClickEventDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name"), new DataColumn("Department"), new DataColumn("Salary") });
            dt.Rows.Add("kirit", ".NET", 20000);
            dt.Rows.Add("Pintu", "CRM", 30000);
            dt.Rows.Add("John", "PHP", 25000);
            dt.Rows.Add("Rahul", "MVC", 10000);
            ged_Employee.DataSource = dt;
            ged_Employee.DataBind();
        }

    }
    protected void ged_Employee_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(ged_Employee, "Select$" + e.Row.RowIndex);
            e.Row.Attributes["style"] = "cursor:pointer";
        }
    }
    protected void ged_Employee_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string name = ged_Employee.SelectedRow.Cells[0].Text;
        string Department = ged_Employee.SelectedRow.Cells[1].Text;
        string Salary = ged_Employee.SelectedRow.Cells[2].Text;
        string message = "Name: " + name + "\\nDepartment: " + Department + "\\nSalary: " + Salary;
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('"+message+"');", true);
    }
}


0 comments:

Post a Comment