Sunday, January 12, 2014

Handling Gridview Doubleclick event

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="DropDownlist_Default3" %>
<!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:TextBox ID="txtSearch" runat="server" />
        <asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="false" OnRowCreated="gvMain_RowCreated">
            <Columns>
            
               <asp:TemplateField HeaderText="Name">
               <ItemTemplate>
               <asp:Label ID="lblName" runat="server"></asp:Label>
                
               </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Dob">
               <ItemTemplate>
              <asp:Label ID="lblDOB" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
               </ItemTemplate>
               </asp:TemplateField>
            </Columns>
        </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;
public partial class DropDownlist_Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee() { ID = 1, Name = "John", DOB = DateTime.Parse("12/11/1971") });
        empList.Add(new Employee() { ID = 2, Name = "Mary", DOB = DateTime.Parse("01/17/1961") });
        empList.Add(new Employee() { ID = 3, Name = "Amber", DOB = DateTime.Parse("12/23/1971") });
        empList.Add(new Employee() { ID = 4, Name = "Kathy", DOB = DateTime.Parse("11/15/1976") });
        empList.Add(new Employee() { ID = 5, Name = "Lena", DOB = DateTime.Parse("05/11/1978") });
        
        gvMain.DataSource = empList;
        gvMain.DataBind();
    }
    protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Employee row = (Employee)e.Row.DataItem;
            Label lblName = (Label)e.Row.FindControl("lblName");
            lblName.Text = row.Name;
            e.Row.Attributes.Add("OnDblClick",
                    "alert(' Double clicked Row ' + " + e.Row.RowIndex + ");");
        }
    }
    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime DOB { get; set; }
    }
}

0 comments:

Post a Comment