How can I have an onclick event in the GridView for any Column?

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

<!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>
        <asp:GridView ID="GridView1" DataKeyNames="id" runat="server" OnRowDataBound="GridView1_RowDataBound">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
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 Blog : System.Web.UI.Page
{
    Employee obj = new Employee();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            GridView1.DataSource = ConvetListToDataSet();
            GridView1.DataBind();

        }

    }
    //This is Helper function that convert list to DataSet
    public DataTable ConvetListToDataSet()
    {
        DataSet dsResults = new DataSet();
        DataTable dtEmp = dsResults.Tables.Add();
        dtEmp.Columns.Add("ID", typeof(int));
        dtEmp.Columns.Add("Name", typeof(string));
        dtEmp.Columns.Add("Sales", typeof(int));
        List<Employee> employees = obj.GeyEmployee();
        foreach (Employee emp in employees)
        {
            dtEmp.Rows.Add(new object[] { emp.EmpID, emp.Name, emp.Sales });
        }
        return dtEmp;

    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable dv = (DataTable)GridView1.DataSource;
        string strID = "";
        DataColumnCollection dc = dv.Columns;

        foreach (DataColumn dcCol in dv.Columns)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                strID = GridView1.DataKeys[e.Row.RowIndex].ToString();
                e.Row.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add("style", "cursor:hand");
                e.Row.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add("onclick", "javascript:window.open('details.aspx?id=" + strID + "'," + "'MyPage','height=300,width=300')");
            }
        }
    }
}
public class Employee
{
    public Employee()
    {

    }
    int m_id;
    public int EmpID
    {

        get { return m_id; }
        set
        {
            m_id = value;
        }
    }

    string m_name;

    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }

    int m_sales;

    public int Sales
    {
        get { return m_sales; }
        set { m_sales = value; }
    }

    public Employee(int id, string name, int sales)
    {
        m_id = id;
        m_name = name;
        m_sales = sales;
    }
    public List<Employee> GeyEmployee()
    {

        List<Employee> employees = new List<Employee>();
        employees.Add(new Employee(1, "XYZ", 1234));
        employees.Add(new Employee(2, "ABC", 4567));
        employees.Add(new Employee(3, "XXX", 2222));


        return employees;

    }

}
Reactions

Post a Comment

0 Comments

Close Menu