Tuesday, December 14, 2010

How to use Repeator Control

 
 
 

image

repeater.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeator.aspx.cs" Inherits="Database_Controls_UnFormatted_Repeator" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
   
   
    <asp:Repeater ID="Repeater2" runat="server"
        onitemdatabound="Repeater2_ItemDataBound"
        onitemcommand="Repeater2_ItemCommand">
       
        <HeaderTemplate  >
        <table    style =" border: 2px solid #000000;   ">
        <tr>
        <td><asp:LinkButton  ID="chkall" Text ="Check All" runat="server" CommandName ="checkall"></asp:LinkButton></td>
        <td><asp:TextBox ID="t1" Text ="Id" runat="server" ForeColor ="Black" ReadOnly ="true" ></asp:TextBox></td>
        <td><asp:TextBox ID="t2" Text ="Name" runat="server" ForeColor ="Black" ReadOnly ="true" ></asp:TextBox></td>
        <td><asp:TextBox ID="t3" Text ="Salary" runat="server" ForeColor ="Black" ReadOnly ="true" ></asp:TextBox></td>
        <td><asp:LinkButton ID="delall" Text ="Delete All" runat="server" CommandName ="deleteall"></asp:LinkButton></td>
 
       
         </tr>
        </table>
        </HeaderTemplate>
             <%--Item Template Start--%>
       <ItemTemplate >
       <table style =" border: 2px solid #000000;">
       
        <tr>
        <td><asp:CheckBox ID ="chb" runat ="server" /></td>
        <td><asp:TextBox ID="t1" Text ='<%#Eval("id") %>' runat="server"   ReadOnly ="true" ></asp:TextBox></td>
        <td><asp:TextBox ID="t2" Text ='<%#Eval ("name") %>' runat="server"   ReadOnly ="true" ></asp:TextBox></td>
        <td><asp:TextBox ID="t3" Text ='<%#Eval("sal") %>' runat="server"   ReadOnly ="true" ></asp:TextBox></td>
       <td><asp:Button ID="b1" CommandName="Select" Text ="Select" runat ="server" /></td>
        <td><asp:Button ID="btnEdit" CommandName="Edit" Text ="Edit" runat ="server" /></td>
         <td><asp:Button ID="btnDelete" CommandName="Delete" Text ="Delete" runat ="server" /></td>
         
        </tr>
        </table>
       </ItemTemplate>
        <AlternatingItemTemplate>
        <table style ="border:2px solid ;  background-color:Green">
        <tr>
         <td><asp:CheckBox ID ="chb" runat ="server" /></td>
        <td><asp:TextBox ID="t1" Text ='<%#Eval("id") %>' runat="server" ForeColor ="White" BackColor="Gray" ReadOnly ="true" ></asp:TextBox></td>
        <td><asp:TextBox ID="t2" Text ='<%#Eval ("name")  %>' runat="server" ForeColor ="White" BackColor="Gray" ReadOnly ="true" ></asp:TextBox></td>
        <td><asp:TextBox ID="t3" Text ='<%#Eval("sal") %>' runat="server" ForeColor ="White" BackColor="Gray" ReadOnly ="true" ></asp:TextBox></td>
         <td><asp:Button ID="b1" CommandName="Select" Text ="Select" runat ="server" /></td>
         <td><asp:Button ID="btnEdit" CommandName="Edit" Text ="Edit" runat ="server" /></td>
          <td><asp:Button ID="btnDelete" CommandName="Delete" Text ="Delete" runat ="server" /></td>
        </tr>
        </table>
        </AlternatingItemTemplate>
        <FooterTemplate >
        <table style =" border: 2px solid #000000;">
       
        <tr>
        <td><asp:TextBox ID="t1" Text ='<%#Eval("id") %>' runat="server"     Visible ="false"  ></asp:TextBox></td>
        <td><asp:TextBox ID="t2" Text ='<%#Eval ("name") %>' runat="server"   Visible ="false"    ></asp:TextBox></td>
        <td><asp:TextBox ID="t3" Text ='<%#Eval("sal") %>' runat="server"    Visible ="false"   ></asp:TextBox></td>
         <td><asp:Button ID="btnNew" CommandName="New" Text ="New" runat ="server" /></td>
         </tr>
         </table>
        </FooterTemplate>
      
        </asp:Repeater>
    </form>
    </body>
</html>
 
 
 
 
 
 
repeater.cs

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
 
public partial class Database_Controls_UnFormatted_Repeator : System.Web.UI.Page
{
    SqlDataAdapter da;
    DataSet ds;
 
    private void fgh()
    {
        try
        {
 
            da = new SqlDataAdapter("select * from emp", "Data Source=.;Initial Catalog=NIR;Integrated Security=True");
 
            ds = new DataSet();
            da.Fill(ds);
            Repeater2.DataSource = ds;
            Repeater2.DataBind();
        }
        catch (Exception e)
        {
            Response.Write(e.Message);
        }
        
 
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fgh();
        }
/*   unrformatted structure.. to aligned data.. use controls
        <%--<div >
        <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate >
       <table style =" border: 2px solid #000000;   background-color: #FF0000; color: #FFFFFF;">
        <tr>
        <td><%#Eval("id") %></td>
        <td><%#Eval ("name") %></td>
        <td><%#Eval("sal") %></td>
        </tr>
        </table>
       </ItemTemplate>
        <HeaderTemplate  >
        <tablestyle  style =" border: 2px solid #000000;   background-color:Blue ; color: #FFFFFF; font-weight :bold ;">
        <tr>
        <td>ID</td>
        <td>Name</td>
        <td>Salary</td>
        </tr>
        </table>
        </HeaderTemplate>
        <AlternatingItemTemplate>
        <table style ="border:2px solid ; border-color:Blue; background-color:Green">
        <tr>
        <td><%#Eval("id") %></td>
        <td><%#Eval ("name") %></td>
        <td><%#Eval("sal") %></td>
        </tr>
        </table>
        </AlternatingItemTemplate>
       
      
        </asp:Repeater>  
    </div>--%>
*
* */
    }
    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Response.Write(e.Item.ItemIndex.ToString());
 
        if (e.Item.ItemIndex > -1)
        {
            TextBox t = (TextBox)e.Item.FindControl("t3");
            if (Int32.Parse(t.Text) < 5000)
            {
                TextBox tt = (TextBox)e.Item.FindControl("t1");
                TextBox ttt = (TextBox)e.Item.FindControl("t2");
                t.BackColor = System.Drawing.Color.Green;
                tt.BackColor = System.Drawing.Color.Green;
                tt.BackColor = System.Drawing.Color.Green;
 
                t.ForeColor = System.Drawing.Color.Yellow;
                tt.ForeColor = System.Drawing.Color.Yellow;
                ttt.ForeColor = System.Drawing.Color.Yellow;
            }
        }
    }
    protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
 
        if (e.CommandName == "Select")
        {
            int i = e.Item.ItemIndex;
            fgh();
            TextBox t = (TextBox)Repeater2.Items[i].FindControl("t1");
            TextBox tt = (TextBox)e.Item.FindControl("t2");
            TextBox ttt = (TextBox)e.Item.FindControl("t3");
            t.BackColor = System.Drawing.Color.Blue;
            tt.BackColor = System.Drawing.Color.Blue;
            tt.BackColor = System.Drawing.Color.Blue;
 
            t.ForeColor = System.Drawing.Color.White;
            tt.ForeColor = System.Drawing.Color.White;
            ttt.ForeColor = System.Drawing.Color.White;
        }
        else if (e.CommandName == "Edit")
        {
            int i = e.Item.ItemIndex;
 
 
            TextBox tt = (TextBox)e.Item.FindControl("t2");
            TextBox ttt = (TextBox)e.Item.FindControl("t3");
 
 
            Button b = (Button)Repeater2.Items[i].FindControl("b1");
            Button bb = (Button)Repeater2.Items[i].FindControl("btnEdit");
            b.Text = "Update";
            bb.Text = "Cancel";
            b.CommandName = "Update";
            bb.CommandName = "Cancel";
            tt.ReadOnly = false;
            ttt.ReadOnly = false;
 
        }
        else if (e.CommandName == "Cancel")
        {
            fgh();
        }
        else if (e.CommandName == "Update")
        {
            int i = e.Item.ItemIndex;
 
            TextBox t = (TextBox)Repeater2.Items[i].FindControl("t1");
            TextBox tt = (TextBox)Repeater2.Items[i].FindControl("t2");
            TextBox ttt = (TextBox)Repeater2.Items[i].FindControl("t3");
 
            string s = "update emp set name='" + tt.Text + "',Sal=" + ttt.Text + " where id=" + t.Text;
            Response.Write(s);
            save(s);
            fgh();
 
        }
        else if (e.CommandName == "Delete")
        {
            int i = e.Item.ItemIndex;
 
            TextBox t = (TextBox)Repeater2.Items[i].FindControl("t1");
            string s = "delete from emp where id=" + t.Text;
            save(s);
            fgh();
 
        }
        else if (e.CommandName == "New")
        {
            int i = e.Item.ItemIndex;
 
            TextBox t = (TextBox)e.Item.FindControl("t1");
            TextBox tt = (TextBox)e.Item.FindControl("t2");
            TextBox ttt = (TextBox)e.Item.FindControl("t3");
            Button b = (Button)e.Item.FindControl("btnNew");
            t.Visible = true;
            tt.Visible = true;
            ttt.Visible = true;
            b.Text = "Save";
            b.CommandName = "Save";
 
        }
        else if (e.CommandName == "Save")
        {
            int i = e.Item.ItemIndex;
 
            TextBox t = (TextBox)e.Item.FindControl("t1");
            TextBox tt = (TextBox)e.Item.FindControl("t2");
            TextBox ttt = (TextBox)e.Item.FindControl("t3");
            string s = "insert into emp values(" + t.Text + ",'" + tt.Text + "'," + ttt.Text + ")";
            save(s);
            fgh();
 
        }
        else if (e.CommandName == "checkall")
        {
            foreach (RepeaterItem r in Repeater2.Items)
            {
                CheckBox c = (CheckBox)r.FindControl("chb");
                c.Checked = true;
            }
            LinkButton l = (LinkButton)e.Item.FindControl("chkall");
            l.Text = "Clear All";
            l.CommandName = "clearall";
 
        }
        else if (e.CommandName == "clearall")
        {
            foreach (RepeaterItem r in Repeater2.Items)
            {
                CheckBox c = (CheckBox)r.FindControl("chb");
                c.Checked = false ;
            }
            LinkButton l = (LinkButton)e.Item.FindControl("chkall");
            l.Text = "Check All";
            l.CommandName = "checkall";
 
        }
        else if (e.CommandName == "deleteall")
        {
            string s = "delete from emp where id in (0";
            foreach (RepeaterItem r in Repeater2.Items)
            {
                CheckBox ch = (CheckBox)r.FindControl("chb");
                if (ch.Checked)
                {
                    TextBox t = (TextBox )e.Item .FindControl("t1");
                    s = s + "," + t.Text;
                }
              
            }
            s = s + ")";
 
            save(s);
            fgh();
        }
    }
   
    public void save(string strQuerty)
    {
        try
        {
            SqlCommand cmd = new SqlCommand(strQuerty);
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=NIR;Integrated Security=True");
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery ();
            con.Close();
        }
        catch (Exception e)
        {
            Response.Write(e.Message);
        }
 
    }
}
 
 
 
Enjoy dear..
 
Niranjan Kala
Team Leader
+91-9466382734

1 comment :

  1. If you are looking into making cash from your traffic with popup advertisments, you can use one of the most reputable companies: Propeller Ads.

    ReplyDelete