Friday, 16 March 2012

udate,delete,sorting and ItemDataBound in datalist

design the page :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!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">
    <asp:ScriptManager  ID="sm" runat="server"></asp:ScriptManager>
    <div>
    <center>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
       <table align="center">
    <tr><td>&nbsp;</td><td><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
    <tr><td>EID</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
    <tr><td>NAME</td><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td></tr>
     <tr><td>SALARY</td><td><asp:TextBox ID="tb2" runat="server"></asp:TextBox></td></tr>
     <tr><td>&nbsp;</td><td>
         <asp:Button ID="btn" runat="server" Text="Save" Width="80"
             onclick="btn_Click" />&nbsp;<asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></td></tr>
     <tr><td>&nbsp;</td><td>
     <asp:DataList ID="dl" runat="server" DataKeyField="eid"
             oncancelcommand="dl_CancelCommand" ondeletecommand="dl_DeleteCommand"
             oneditcommand="dl_EditCommand1" onupdatecommand="dl_UpdateCommand"
             onitemdatabound="dl_ItemDataBound">
     <HeaderTemplate>
     <table width="100%"><tr><th >Sl No.</th><th >UPDATE</th><th >DELETE</th><th>
         <asp:LinkButton ID="lb1" runat="server" Text="EID" onclick="lb1_Click"></asp:LinkButton></th><th >
             <asp:LinkButton ID="lb2" runat="server" Text="NAME" onclick="lb2_Click"></asp:LinkButton></th><th >
             <asp:LinkButton ID="lb3" runat="server" Text="SALARY" onclick="lb3_Click"></asp:LinkButton></th></tr></table>
     </HeaderTemplate>
     <ItemTemplate>
     <table style="text-align:left"  width="100%">
     <tr>
     <td width="16%"><%#Container.ItemIndex+1 %></td>
     <td width="16%"><asp:LinkButton ID="lb8" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton></td>
     <td width="16%"><asp:LinkButton ID="lb9" runat="server" Text="Delete" CommandName="delete" runat="server"></asp:LinkButton></td>
     <td width="16%" style="text-align:center"><%#Eval("eid") %></td><td width="16%"><%#Eval("name") %></td>
     <td width="16%"><asp:Label ID="lb3" runat="server" Text='<%#Eval("sal") %>'></asp:Label></td>
     </tr>
     </table>
     </ItemTemplate>
     <EditItemTemplate>
      <table style="text-align:left"  width="100%">
     <tr>
     <td width="16%"><%#Container.ItemIndex+1 %></td>
     <td width="16%"><asp:LinkButton ID="lb10" runat="server" Text="Cancel" CommandName="Cancel"></asp:LinkButton>&nbsp;<asp:LinkButton ID="lb11" runat="server" Text="Update" runat="server" CommandName="update"></asp:LinkButton></td>
     <td width="16%">&nbsp;</td><td width="16%" style="text-align:center"><%#Eval("eid") %></td>
     <td width="16%"><asp:TextBox ID="tb3" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td>
     <td width="16%"><asp:TextBox ID="tb4" runat="server" Text='<%#Eval("sal") %>'></asp:TextBox></td>
     </tr>
     </table>
     </EditItemTemplate>
     <FooterTemplate>
      <table   width="100%">
      <tr>
      <td colspan="6" width="90%" align="center">TOTAL</td>
      <td width="10%"><asp:Label ID="lb2" runat="server"></asp:Label></td>
      </tr>
      </table>
     </FooterTemplate>
     </asp:DataList>
     </td></tr></table>
       
     </ContentTemplate>
     </asp:UpdatePanel>
     </center>
    </div>
    </form>
</body>
</html>
The page looks like

C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;

public partial class Default7 : System.Web.UI.Page
{
    DatabaseEntities1 o = new DatabaseEntities1();
    int i;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            view();
            ViewState["a"] = 1;
            ViewState["b"] = 1;
            ViewState["c"] = 1;
        }
    }
    void view()
    {
        var m = from x in o.EMPs select x;
        dl.DataSource = m;
        dl.DataBind();
    }
  
    protected void dl_EditCommand1(object source, DataListCommandEventArgs e)
    {
        dl.EditItemIndex = e.Item.ItemIndex;
        view();
    }
    protected void dl_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dl.EditItemIndex = -1;
        view();
    }
    protected void dl_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        i = Convert.ToInt32(dl.DataKeys[e.Item.ItemIndex].ToString());
        var m = (from x in o.EMPs where x.EID == i select x).First();
        o.EMPs.DeleteObject(m);
        o.SaveChanges();
        lb.Text = "One record deleted.";
        view();
    }
    protected void dl_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        i = Convert.ToInt32(dl.DataKeys[e.Item.ItemIndex].ToString());
        var m = (from x in o.EMPs where x.EID == i select x).First();
        m.NAME = ((TextBox)dl.Items[e.Item.ItemIndex].FindControl("tb3")).Text;
        m.SAL = Convert.ToInt32(((TextBox)dl.Items[e.Item.ItemIndex].FindControl("tb4")).Text);
        o.SaveChanges();
        lb.Text = "One record updated.";
        dl.EditItemIndex = -1;
        view();
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        EMP p = new EMP();
        p.EID = Convert.ToInt32(tb.Text);
        p.NAME = tb1.Text;
        p.SAL = Convert.ToInt32(tb2.Text);
        o.EMPs.AddObject(p);
        o.SaveChanges();
        lb.Text = "One record saved.";
        view();
    }
    protected void dl_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            i = i + Convert.ToInt32(((Label)e.Item.FindControl("lb3")).Text);
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            ((Label)e.Item.FindControl("lb2")).Text = i.ToString();
        }
    }
    protected void lb1_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32( ViewState["a"]) == 1)
        {
            var m = from x in o.EMPs orderby x.EID descending select x;
            dl.DataSource = m;
            dl.DataBind();
            ViewState["a"] = 2;
        }
        else
        {
            var m = from x in o.EMPs orderby x.EID ascending select x;
            dl.DataSource = m;
            dl.DataBind();
            ViewState["a"] = 1;
        }
    }
    protected void lb2_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ViewState["b"]) == 1)
        {
            var m = from x in o.EMPs orderby x.NAME descending select x;
            dl.DataSource = m;
            dl.DataBind();
            ViewState["b"] = 2;
        }
        else
        {
            var m = from x in o.EMPs orderby x.NAME ascending select x;
            dl.DataSource = m;
            dl.DataBind();
            ViewState["b"] = 1;
        }
    }
    protected void lb3_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ViewState["c"]) == 1)
        {
            var m = from x in o.EMPs orderby x.SAL descending select x;
            dl.DataSource = m;
            dl.DataBind();
            ViewState["c"] = 2;
        }
        else
        {
            var m = from x in o.EMPs orderby x.SAL ascending select x;
            dl.DataSource = m;
            dl.DataBind();
            ViewState["c"] = 1;
        }
    }
  
}

No comments:

Post a Comment