Friday, 16 March 2012

sorting and ItemDataBound in Repeater

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

<!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>
    <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:Repeater ID="rp" runat="server" OnItemDataBound="rp_ItemDataBound">
     <HeaderTemplate>
     <table width="100%">
     <tr>
     <th  width="25%">SlNo.</th>
     <th  width="25%"><asp:LinkButton OnClick="x" ID="lbt" runat="server" Text="EID"></asp:LinkButton></th>
     <th  width="25%"><asp:LinkButton OnClick="y" ID="lbt1" runat="server" Text="NAME"></asp:LinkButton></th>
      <th  width="25%"><asp:LinkButton OnClick="z" ID="lbt2" runat="server" Text="SALARY"></asp:LinkButton></th>
     <th></th>
     </tr>
   
     </HeaderTemplate>
     <ItemTemplate>
  
     <tr>
     <td width="25%"><%#Container.ItemIndex+1 %></td>
     <td  width="25%"><%#Eval("eid") %></td>
     <td  width="25%"><%#Eval("name") %></td>
     <td  width="25%"><asp:Label ID="lb" runat="server" Text='<%#Eval("sal") %>'></asp:Label></td>
     </tr>
  
     </ItemTemplate>
     <FooterTemplate>
     <tr>
     <td>&nbsp;</td>
     <td  align="center">TOTAL</td>
      <td>&nbsp;</td>
     <td  align="left"><asp:Label ID="lb1" runat="server"></asp:Label></td>
     </tr>
     </table>
     </FooterTemplate>
     </asp:Repeater>
     </td>
     </tr>
     </table>
     </ContentTemplate>
     </asp:UpdatePanel>
    </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 Default8 : 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;
        rp.DataSource = m;
        rp.DataBind();
    }
    protected void x(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ViewState["a"]) == 1)
        {
            ViewState["a"] = 2;
            var m = from x in o.EMPs orderby x.EID descending select x;
            rp.DataSource = m;
            rp.DataBind();
        }
        else
        {
            ViewState["a"] = 1;
            var m = from x in o.EMPs orderby x.EID ascending select x;
            rp.DataSource = m;
            rp.DataBind();
        }
    }
    protected void y(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ViewState["b"]) == 1)
        {
            ViewState["b"] = 2;
            var m = from x in o.EMPs orderby x.NAME descending select x;
            rp.DataSource = m;
            rp.DataBind();
        }
        else
        {
            ViewState["b"] = 1;
            var m = from x in o.EMPs orderby x.NAME ascending select x;
            rp.DataSource = m;
            rp.DataBind();
        }
    }
    protected void z(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ViewState["c"]) == 1)
        {
            ViewState["c"] = 2;
            var m = from x in o.EMPs orderby x.SAL descending select x;
            rp.DataSource = m;
            rp.DataBind();
        }
        else
        {
            ViewState["c"] = 1;
            var m = from x in o.EMPs orderby x.SAL ascending select x;
            rp.DataSource = m;
            rp.DataBind();
        }
    }
    protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            i = i + Convert.ToInt32(((Label)e.Item.FindControl("lb")).Text);
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            ((Label)e.Item.FindControl("lb1")).Text = i.ToString();
        }
    }
    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.AddToEMPs(p);
        o.SaveChanges();
        view();
        lb.Text = "One record saved.";
    }
}

No comments:

Post a Comment