Search...

Monday, March 5, 2012

Find controls in repeater Asp.Net




*************************************************************************

/*Add Stylesheet in your application names StyleSheet.css*/

 body {}
.repeater{width:100%;background:#ffffff;border:1px solid #000000; border-collapse:separate;}
.repeater th{border:0px;background:url('Images/hfbg.jpg') repeat-x;color:#ffffff;text-align:center;font-size:18px;}

.repeater tr,td{height:24px;}
.repeater tfoot td{border:0px;background:url('Images/hfbg.jpg') repeat-x;color:#ffffff;text-align:center;font-size:18px;}
.repeater td{padding-left:10px;border:1px solid #b5b5b5}

.altRow{background:url('Images/alt.png') repeat-x}

.repeaterPager{background:url('Images/hfbg.jpg') repeat-x;text-align:center;}

*************************************************************************

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

<!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>Find controls in repeater Asp.Net</title>
    <link href="Resources/StyleSheet/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <table class="repeater">
        <thead>
        <tr>
        <th>Employee Name</th>
        <th>Salary</th>
        <th>Department</th>
        </tr>
        </thead>
        <tbody>
        </HeaderTemplate>
     
        <ItemTemplate>
        <tr>
        <td><%# Eval("Employeename")%></td>
        <td><%# Eval("Salary")%></td>
        <td><asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Department_id")%>'></asp:Literal></td>
        </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
        <tr class="altRow">
        <td><%# Eval("Employeename")%></td>
        <td><%# Eval("Salary")%></td>
        <td><asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Department_id")%>'></asp:Literal></td>
        </tr>
        </AlternatingItemTemplate>
     
        <FooterTemplate>
        </tbody>
        <tfoot>
        <tr>
        <td colspan="3"></td>
        </tr>
        </tfoot>
        </table>
        </FooterTemplate>
        </asp:Repeater>

    </div>
    </form>
</body>
</html>

*************************************************************************

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private Employee employee = new Employee();
    private Department department = new Department();

    private delegate void BindEmployee();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindEmployee bindEmployee = new BindEmployee(BindGrid);
            bindEmployee();
        }
    }

    private void BindGrid()
    {
        Repeater1.DataSource = employee.ReadAll();
        Repeater1.DataBind();
        foreach (RepeaterItem repeaterItem in Repeater1.Items)
        {
            Literal deptId = (Literal)repeaterItem.FindControl("Literal1");
            deptId.Text = department.ReadAllById(deptId.Text)[0].Departmentname;
        }
    }
}

*************************************************************************

using System;
using System.Web.Configuration;
public class Connection
{
public Connection(){}
     /// <summary>
     /// Get Connection string.
     /// <summary>
  public static String Cs
    {
        get
        {
            return WebConfigurationManager.ConnectionStrings["cs"].ConnectionString;
        }
    }
}

*************************************************************************

#region Namespaces
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
#endregion

/// <summary>
/// class Department
/// </summary>
 public class Department
 {
#region Properties
      private Int32 _Id;
     /// <summary>
     ///Gets or Sets the Int32 value of Id
     /// </summary>
      public Int32 Id { get { return _Id;} set { _Id = value;} }

      private String _Departmentname;
     /// <summary>
     ///Gets or Sets the String value of Departmentname
     /// </summary>
      public String Departmentname { get { return _Departmentname;} set { _Departmentname = value;} }

#endregion

#region Private Meambers
      private SqlCommand cmd;
      private SqlConnection con;
      private SqlDataReader table;
      private String ConnectionString = String.Empty;
#endregion

#region Constructor
     /// <summary>
     ///Default constructor
     /// </summary>
      public Department()
      {
        ConnectionString=Connection.Cs;
      }

     /// <summary>
     ///Parameterised constructor
     /// </summary>
      public Department(String connectionString)
      {
        ConnectionString=connectionString;
      }
#endregion

#region Public Methods


     /// <summary>
     /// This meathod returns all records of Department table by column Id.
     /// </summary>
     /// <param name="Id"></param>
     /// <returns>All rows of Department table.</returns>


      public List<Department> ReadAllById(String id)
       {
        using(con = new SqlConnection(ConnectionString))
         {
          try
           {
            List<Department> list = new List<Department>();
            if (con.State == ConnectionState.Closed)
            {
             con.Open();
            }
            cmd = new SqlCommand("Select * From [dbo].[Department] Where [Id]=@ID",con);
            cmd.Parameters.AddWithValue("@Id",id);
            using(table = cmd.ExecuteReader())
             {
              while (table.Read())
               {
                Department obj = new Department();
                obj._Id = table.GetInt32(0);
                obj._Departmentname = table.GetString(1);
                list.Add(obj);
               }
               return list;
              }
             }
           finally
            {
              if (con.State == ConnectionState.Open)
               {
                con.Close();
               }
            }
         }
      }

#endregion

 }

*************************************************************************

#region Namespaces
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
#endregion

/// <summary>
/// class Employee
/// </summary>
 public class Employee
 {
#region Properties
      private Int32 _Id;
     /// <summary>
     ///Gets or Sets the Int32 value of Id
     /// </summary>
      public Int32 Id { get { return _Id;} set { _Id = value;} }

      private String _Employeename;
     /// <summary>
     ///Gets or Sets the String value of Employeename
     /// </summary>
      public String Employeename { get { return _Employeename;} set { _Employeename = value;} }

      private Decimal _Salary;
     /// <summary>
     ///Gets or Sets the Decimal value of Salary
     /// </summary>
      public Decimal Salary { get { return _Salary;} set { _Salary = value;} }

      private Int32 _Department_id;
     /// <summary>
     ///Gets or Sets the Int32 value of Department_id
     /// </summary>
      public Int32 Department_id { get { return _Department_id;} set { _Department_id = value;} }

#endregion

#region Private Meambers
      private SqlCommand cmd;
      private SqlConnection con;
      private SqlDataReader table;
      private String ConnectionString = String.Empty;
#endregion

#region Constructor
     /// <summary>
     ///Default constructor
     /// </summary>
      public Employee()
      {
        ConnectionString=Connection.Cs;
      }

     /// <summary>
     ///Parameterised constructor
     /// </summary>
      public Employee(String connectionString)
      {
        ConnectionString=connectionString;
      }
#endregion

#region Public Methods

     /// <summary>
     /// This meathod returns all records of Employee table.
     /// </summary>
     /// <returns> All rows of Employee table.</returns>


      public List<Employee> ReadAll()
       {
        using(con = new SqlConnection(ConnectionString))
         {
          try
           {
            List<Employee> list = new List<Employee>();
            if (con.State == ConnectionState.Closed)
            {
             con.Open();
            }
            cmd = new SqlCommand("Select Top(1000)* From [dbo].[Employee]",con);
            using(table = cmd.ExecuteReader())
             {
              while (table.Read())
               {
                Employee obj = new Employee();
                obj._Id = table.GetInt32(0);
                obj._Employeename = table.GetString(1);
                obj._Salary = table.GetDecimal(2);
                obj._Department_id = table.GetInt32(3);
                list.Add(obj);
               }
               return list;
              }
             }
           finally
            {
              if (con.State == ConnectionState.Open)
               {
                con.Close();
               }
            }
         }
      }
#endregion

 }

*************************************************************************

<connectionStrings>
<add name="cs" connectionString="Data Source=Your Data Source;Initial Catalog=softwarekaffee;Persist Security Info=True;User ID=Your User Id;Password=Your Password"/>
</connectionStrings>

*************************************************************************

Go
Create database softwarekaffee

Go
use softwarekaffee

Go
create table Department
(
Id int identity primary key,
DepartmentName nvarchar(30)
)
Insert Into Department Values ('Sales');
Insert Into Department Values ('Production');
Insert Into Department Values ('Purchase');
Insert Into Department Values ('Stock');

Go
create table Employee
(
Id int identity primary key,
EmployeeName nvarchar(30),
Salary decimal(7,2),
Department_Id int references Department(Id)
)

Insert Into Employee Values ('Kaushik',41000,1);
Insert Into Employee Values ('Rahul',25000,2);
Insert Into Employee Values ('Sumit',26000,3);
Insert Into Employee Values ('Ravi',40000,4);
Insert Into Employee Values ('Rajeev',41000,1);
Insert Into Employee Values ('Amit',25000,2);
Insert Into Employee Values ('Gautam',26000,3);
Insert Into Employee Values ('Ranveer',40000,4);
Insert Into Employee Values ('Devendra',40000,4);
Insert Into Employee Values ('Sunil',40000,4);
Insert Into Employee Values ('Manas',41000,1);
Insert Into Employee Values ('Mishri Lal',25000,2);
Insert Into Employee Values ('Avinash',26000,3);
Insert Into Employee Values ('Alok',40000,4);
Insert Into Employee Values ('Pooja',41000,1);
Insert Into Employee Values ('Geeta',25000,2);
Insert Into Employee Values ('Lalita',26000,3);
Insert Into Employee Values ('Meenakshi',40000,4);
Insert Into Employee Values ('Rajendra',40000,4);
Insert Into Employee Values ('Sunita',40000,4);
Insert Into Employee Values ('Sandeep',26000,3);
Insert Into Employee Values ('Vinod',40000,4);
Insert Into Employee Values ('Remo',40000,4);

*************************************************************************


No comments: