Skip to main content

sp.Net Ajax HoverMenu Control On GridView with Database Manipulations

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

<%@ Register TagPrefix="cc1" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>
<!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>Untitled Page</title>
   <style type="text/css">
       .popupMenu {
           position:absolute;
           visibility:hidden;
           background-color:#F5F7F8;
           opacity:.9;
           filter: alpha(opacity=90);
       } 
       .popupHover {
           background-image:url(img/header-opened.png);
           background-repeat:repeat-x;
           background-position:left top;
           background-color:#F5F7F8;
       }         
   </style>
</head>
<body>
   <form id="form1" runat="server">
       <asp:ScriptManager ID="ScriptManger1" runat="Server">
       </asp:ScriptManager>
       <div>
           <asp:GridView ID="GridView1" DataKeyNames="ISBN" runat="server" AutoGenerateColumns="False"
               Caption="Publisher List List" EmptyDataText="You have deleted all records in customer list"
               OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowCommand="GridView1_RowCommand"
               OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
               <Columns>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:Panel ID="Panel1" runat="server" Width="500px">
                               <asp:Label ID="Label1" runat="server" Text='<%# Eval("ISBN") %>' Visible="False"></asp:Label>
                               <asp:Label ID="Label2" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                               <asp:Label ID="Label3" runat="server" Text='<%# Eval("Publisher") %>'></asp:Label>
                               <asp:Label ID="Label4" runat="server" Text='<%# Eval("Year") %>'></asp:Label>
                           </asp:Panel>
                           <cc1:HoverMenuExtender ID="HoverMenuExtender1" runat="server" HoverCssClass="popupHover"
                               PopupControlID="PopupMenu" TargetControlID="Panel1" PopupPosition="Left">
                           </cc1:HoverMenuExtender>
                           <asp:Panel ID="PopupMenu" runat="server" CssClass="popupMenu">
                               <div style="border: 1px outset white; padding: 2px;">
                                   <asp:LinkButton ID="LinkButton3" runat="server" CommandName="AddNew" Text="Add New"></asp:LinkButton>
                                   <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
                                   <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
                               </div>
                           </asp:Panel>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <asp:Panel ID="Panel1" runat="server" Width="400px">
                               <asp:Label ID="lblISBN" runat="server" Text='<%# Eval("ISBN") %>' Visible="false"></asp:Label>
                               <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>
                               <asp:TextBox ID="txtPub" runat="server" Text='<%# Eval("Publisher") %>'></asp:TextBox>
                               <asp:TextBox ID="txtYear" runat="server" Text='<%# Eval("Year") %>'></asp:TextBox>
                           </asp:Panel>
                           <cc1:HoverMenuExtender ID="HoverMenuExtender2" runat="server" HoverCssClass="popupHover"
                               PopupControlID="PopupMenu" PopupPosition="Right" TargetControlID="Panel1">
                           </cc1:HoverMenuExtender>
                           <asp:Panel ID="PopupMenu" runat="server" CssClass="popupMenu" Width="80">
                               <div style="border: 1px outset white; padding: 2px;">
                                   <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                       Text="Update"></asp:LinkButton>
                                   <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                       Text="Cancel"></asp:LinkButton>
                               </div>
                           </asp:Panel>
                       </EditItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridViewHover : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            BindGridViewView();
        }
    }
    private void BindGridViewView()
    {
        if (Session["strTemp"] != null)
        {

            GridView1.DataSource = Session["strTemp"] as DataTable;
            GridView1.DataBind();

        }
        else
        {
            GridView1.DataSource = GetCustomMadeDataTable();
            GridView1.DataBind();
        }
    }
    public DataTable GetCustomMadeDataTable()
    {
        //Create a new DataTable object
        System.Data.DataTable objDataTable = new System.Data.DataTable();
        //Create three columns with string as their type
        objDataTable.Columns.Add("ISBN", typeof(string));
        objDataTable.Columns.Add("Title", typeof(string));
        objDataTable.Columns.Add("Publisher", typeof(string));
        objDataTable.Columns.Add("Year", typeof(string));
        DataColumn[] dcPk = new DataColumn[1];
        dcPk[0] = objDataTable.Columns["ISBN"];
        objDataTable.PrimaryKey = dcPk;
        objDataTable.Columns["ISBN"].AutoIncrement = true;
        objDataTable.Columns["ISBN"].AutoIncrementSeed = 1;
        //Adding some data in the rows of this DataTable
        DataRow dr;
        for (int i = 1; i <= 5; i++)
        {
            dr = objDataTable.NewRow();
            dr[1] = "Title" + i.ToString();
            dr[2] = "Publisher" + i.ToString();
            dr[3] = "200" + i.ToString();
            objDataTable.Rows.Add(dr);
        }
        for (int i = 6; i <= 8; i++)
        {
            dr = objDataTable.NewRow();
            dr[1] = "Computer" + i.ToString();
            dr[2] = "TMH" + i.ToString();
            dr[3] = "200" + i.ToString();
            objDataTable.Rows.Add(dr);
        }
        Session["strTemp"] = objDataTable;
        return objDataTable;
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridViewView();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ISBN = GridView1.DataKeys[e.RowIndex].Value.ToString();
        DataTable dt = Session["strTemp"] as DataTable;
        DataRow dr;
        dr = dt.NewRow();
        dt.Rows.Find(ISBN).Delete();
        dt.GetChanges();
        Session["strTemp"] = dt;
        BindGridViewView();


    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            InsertBlanRow();
            GridView1.EditIndex = GridView1.Rows.Count;
            BindGridViewView();
        }
        //BindGridViewView();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGridViewView();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ISBN = GridView1.DataKeys[e.RowIndex].Value.ToString();
        DataTable dt = Session["strTemp"] as DataTable;
        DataRow dr;
        dr = dt.NewRow();
        dr = dt.Rows.Find(ISBN);
        GridViewRow row = GridView1.Rows[e.RowIndex];

        if (row != null)
        {

            TextBox txtTitle = row.FindControl("txtTitle") as TextBox;
            TextBox txtPub = row.FindControl("txtPub") as TextBox;
            TextBox txtYear = row.FindControl("txtYear") as TextBox;
            dr["Title"] = txtTitle.Text;
            dr["Publisher"] = txtPub.Text;
            dr["Year"] = txtYear.Text;

            dt.AcceptChanges();
            Session["strTemp"] = dt;

        }
        GridView1.EditIndex = -1;
        BindGridViewView();

    }
    protected void InsertBlanRow()
    {

        DataTable dt = Session["strTemp"] as DataTable;
        DataRow dr;
        dr = dt.NewRow();
        dr["Title"] = "";
        dr["Publisher"] = "";
        dr["Year"] = "";
        dt.Rows.Add(dr);
        Session["strTemp"] = dt;


    }
}

Comments

  1. I've been looking everywhere for a good FULL example of this! THANK YOU!

    ReplyDelete

Post a Comment

Popular posts from this blog