What
is Repeater Control in Asp.Net
The Repeater is asp.net control. This
control is used to display a repeated list of items that are bound to the
control. The Repeater control may be bound to a database table, an XML file, or
another list of items. Here we will show how to bind an Database to a Repeater
control with code.
//Execute this
query for Sql Server 2008
use test
drop table Employee
create table Employee(SrNo int primary key,Name nvarchar(50) unique,Gender nvarchar(50),Age int,JobType nvarchar(50),Salary int)
insert into Employee values(1,'Somesh Katiyar','Male',24,'Part Time',15000)
Select * from
Employee order by
SrNo asc
//
Example with Repeater Control in Asp.Net
Code for
Default.aspx………………………………………………..
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:314px">
<legend>Enter Employee Details</legend>
<table width="314px"
>
<tr><td>SrNo</td><td>
<asp:TextBox ID="txtsrno"
runat="server"></asp:TextBox></td></tr>
<tr><td>Name</td><td>
<asp:TextBox ID="txtname"
runat="server"></asp:TextBox></td></tr>
<tr><td>Age</td><td>
<asp:TextBox ID="txtage"
runat="server"></asp:TextBox></td></tr>
<tr><td>Gender</td><td><asp:RadioButtonList ID="RbGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td></tr>
<tr><td>Job Type</td><td>
<asp:DropDownList ID="ddljobtype"
runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Part
Time</asp:ListItem>
<asp:ListItem>Full
Time</asp:ListItem>
</asp:DropDownList>
</td></tr>
<tr><td >Salary</td><td>
<asp:TextBox ID="txtsalary"
runat="server"></asp:TextBox></td></tr>
<tr><td colspan="2">
<asp:Button ID="btnSave"
runat="server"
Text="Save"
onclick="btnSave_Click"
/>
<asp:Label ID="lblmessage"
runat="server"
Text="Label"
Visible="False"></asp:Label>
</td></tr>
<tr><td colspan="2">
<asp:Repeater ID="RepeaterEmployee"
runat="server"
onitemcommand="RepeaterEmployee_ItemCommand">
<HeaderTemplate>
<table style=" border:1px solid #c1650f; width:300px" cellpadding="0">
<tr style="background-color:#c1650f; color:White">
<td colspan="2">
<b><i><center>Employee Details</center</i></b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted
#c1650f; width:300px"
>
<tr><td><b>Sr No:</b><asp:Label ID="lblsrno" runat="server" Text='<%#Eval("SrNo") %>'/></td>
</tr><tr><td><b>Name:</b><asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'/>
<asp:TextBox ID="txtname"
runat="server"
Text='<%#Eval("Name") %>' Visible="false"></asp:TextBox></td></tr>
<tr><td><b>Gender</b><asp:Label ID="lblGender" runat="server" Text='<%#Eval("Gender") %>'/>
<asp:RadioButtonList ID="rbsex"
runat="server"
RepeatDirection="Horizontal"
Visible="false">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<%--<asp:HiddenField ID="HiddenField1"
runat="server" Value='<%#Eval("Gender") %>'>
</asp:HiddenField>--%></tr>
<tr><td><b>Age:</b><asp:Label ID="lblAge" runat="server" Text='<%#Eval("Age") %>'/>
<asp:TextBox ID="txtage"
runat="server"
Text='<%#Eval("Age") %>' Visible="false"></asp:TextBox>
</td></tr>
<tr><td><b>Job Type:</b><asp:Label ID="lbltype" runat="server" Text='<%#Eval("JobType") %>'/>
<asp:DropDownList ID="ddltype"
runat="server"
Visible="false">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Part
Time</asp:ListItem>
<asp:ListItem>Full
Time</asp:ListItem>
</asp:DropDownList></td></tr>
<tr><td><b>Salary:</b><asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary") %>'/>
<asp:TextBox ID="txtsalary"
runat="server"
Text='<%#Eval("Salary") %>' Visible="false"></asp:TextBox></td></tr>
</table></td></tr>
<tr><td><table style="background-color:#EBEFF0;border-top:1px dotted
#c1650f;border-bottom:1px solid #c1650f; width:300px" >
<tr><td>
<asp:ImageButton ID="imgedit"
runat="server"
Height="28px"
ImageUrl="~/Image/edit.jpg"
Width="29px"
CommandArgument='<%#Eval("SrNo") %>' CommandName="edit"
ToolTip="Edit"
/>
<asp:ImageButton ID="imgdelete"
runat="server" Height="28px" ImageUrl="~/Image/delete.jpg" Width="29px"
CommandArgument='<%#Eval("SrNo") %>' CommandName="delete"
ToolTip="Delete"
onclientclick="return
confirm('Are you sure you want to delete?')"/>
<asp:ImageButton ID="imgupdate"
runat="server"
Height="28px"
ImageUrl="~/Image/update.jpg"
Width="29px"
CommandArgument='<%#Eval("SrNo") %>' CommandName="update"
ToolTip="Update"
Visible="false"/>
<asp:ImageButton ID="imgcancel"
runat="server"
Height="28px"
ImageUrl="~/Image/cancel.jpg"
Width="29px"
CommandArgument='<%#Eval("SrNo") %>' CommandName="cancel"
ToolTip="Cancel"
Visible="false"
/>
</td></tr>
</table></td></tr>
<tr><td colspan="2"> </td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</td></tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
Code for
Default.aspx.cs………………………………………………..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
public partial class _Default :
System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs
e)
{
con = new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial Catalog=test;Integrated
Security=True");
if (!IsPostBack)
{
BindRepeater();
}
}
//Code save data in database with save button
protected void
btnSave_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("insert
into Employee values(@srno,@name,@gender,@age,@jobtype,@salary)",
con);
cmd.Parameters.AddWithValue("@srno", Convert.ToInt32(txtsrno.Text));
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@gender", RbGender.SelectedValue);
cmd.Parameters.AddWithValue("@age", Convert.ToInt32(txtage.Text));
cmd.Parameters.AddWithValue("@jobtype",
ddljobtype.SelectedItem.Text);
cmd.Parameters.AddWithValue("@salary", Convert.ToInt32(txtsalary.Text));
con.Open();
cmd.ExecuteNonQuery();
lblmessage.Visible
= true;
lblmessage.ForeColor
= Color.Green;
lblmessage.Text
= "Save Successfully";
con.Close();
BindRepeater();
}
catch (Exception
ex)
{
lblmessage.Visible
= true;
lblmessage.ForeColor
= Color.Green;
lblmessage.Text
=ex.Message;
}
}
// funcation for bind Repeater...........................
private void
BindRepeater()
{
SqlCommand cmd = new SqlCommand("Select
* from Employee order by SrNo asc ", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataSet ds = new DataSet();
SqlDataAdapter da= new SqlDataAdapter(cmd);
da.Fill(ds);
RepeaterEmployee.DataSource
= ds;
RepeaterEmployee.DataBind();
con.Close();
}
// code for update delete cancle with commandname in Repeater
Control
protected void
RepeaterEmployee_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("delete
from Employee where SrNo= @SrNo", con);
cmd.Parameters.AddWithValue("@SrNo", e.CommandArgument);
cmd.ExecuteNonQuery();
cmd.Dispose();
BindRepeater();
}
if (e.CommandName == "edit")
{
((Label)e.Item.FindControl("lblsrno")).Visible
= true;
((Label)e.Item.FindControl("lblname")).Visible
= false;
((Label)e.Item.FindControl("lblGender")).Visible
= false;
((Label)e.Item.FindControl("lblAge")).Visible
= false;
((Label)e.Item.FindControl("lblSalary")).Visible
= false;
((Label)e.Item.FindControl("lbltype")).Visible
= false;
((TextBox)e.Item.FindControl("txtname")).Visible = true;
((RadioButtonList)e.Item.FindControl("rbsex")).Visible = true;
((DropDownList)e.Item.FindControl("ddltype")).Visible = true;
//((RadioButtonList)e.Item.FindControl("rbsex")).Items.FindByText(((HiddenField)e.Item.FindControl("HiddenField1")).Value).Selected
=true;
((RadioButtonList)e.Item.FindControl("rbsex")).Items.FindByText(((Label)e.Item.FindControl("lblGender")).Text).Selected
=true;
((DropDownList)e.Item.FindControl("ddltype")).DataBind();
((DropDownList)e.Item.FindControl("ddltype")).SelectedItem.Selected = false;
((DropDownList)e.Item.FindControl("ddltype")).Items.FindByText(((Label)e.Item.FindControl("lbltype")).Text).Selected=
true;
((TextBox)e.Item.FindControl("txtage")).Visible = true;
((TextBox)e.Item.FindControl("txtsalary")).Visible = true;
((ImageButton)e.Item.FindControl("imgedit")).Visible = false;
((ImageButton)e.Item.FindControl("imgdelete")).Visible = false;
((ImageButton)e.Item.FindControl("imgupdate")).Visible = true;
((ImageButton)e.Item.FindControl("imgcancel")).Visible = true;
}
if (e.CommandName == "update")
{
string name = ((TextBox)e.Item.FindControl("txtname")).Text;
string gender = ((RadioButtonList)e.Item.FindControl("rbsex")).SelectedItem.Text;
string age= ((TextBox)e.Item.FindControl("txtage")).Text;
string salary = ((TextBox)e.Item.FindControl("txtsalary")).Text;
SqlDataAdapter da = new SqlDataAdapter("Update Employee set Name=
@Name,Gender=@Gender,Age=@Age,Salary=@Salary where SrNo=@SrNo",
con);
da.SelectCommand.Parameters.AddWithValue("@Name",name);
da.SelectCommand.Parameters.AddWithValue("@Gender",gender);
da.SelectCommand.Parameters.AddWithValue("@Age",Convert.ToInt32(age));
da.SelectCommand.Parameters.AddWithValue("@Salary",Convert.ToInt32(salary));
da.SelectCommand.Parameters.AddWithValue("@SrNo", e.CommandArgument);
DataSet ds = new DataSet();
da.Fill(ds);
BindRepeater();
}
if (e.CommandName == "cancel")
{
((Label)e.Item.FindControl("lblsrno")).Visible
= true;
((Label)e.Item.FindControl("lblname")).Visible
= true;
((Label)e.Item.FindControl("lblGender")).Visible
= true;
((Label)e.Item.FindControl("lblAge")).Visible
= true;
((Label)e.Item.FindControl("lbltype")).Visible
= true;
((Label)e.Item.FindControl("lblSalary")).Visible
= true;
((TextBox)e.Item.FindControl("txtname")).Visible = false;
((RadioButtonList)e.Item.FindControl("rbsex")).Visible = false;
((DropDownList)e.Item.FindControl("ddltype")).Visible = false;
//((RadioButtonList)e.Item.FindControl("rbsex")).Items.FindByText(((HiddenField)e.Item.FindControl("HiddenField1")).Value).Selected
=true;
//((RadioButtonList)e.Item.FindControl("rbsex")).Items.FindByText(((Label)e.Item.FindControl("lblGender")).Text).Selected
= true;
((TextBox)e.Item.FindControl("txtage")).Visible =false;
((TextBox)e.Item.FindControl("txtsalary")).Visible =false;
((ImageButton)e.Item.FindControl("imgedit")).Visible = true;
((ImageButton)e.Item.FindControl("imgdelete")).Visible =true;
((ImageButton)e.Item.FindControl("imgupdate")).Visible = false;
((ImageButton)e.Item.FindControl("imgcancel")).Visible = false;
}
}
}
sir what is difference b/w repeater and datalist control.
ReplyDeleteThe DataList control is similar to the Repeater control. However, it has some additional properties and templates that you can use to display its data in a diverse fashion. The Repeater control does not have any built-in layout or style. We are forced to specify all formatting-related HTML elements and style tags. On the other hand, a DataList control provides more flexibility to display data in a desired layout. It also provides data selection and editing capabilities. How does it do it? Well, in addition to the five templates (Item Template, AlternatingItem Template, Separator Template, Header Template, Footer Template that a repeater has, the DataList control has two more templates: SelectedItemTemplate, and EditItemTemplate. These templates are useful for allowing data selection and data editing functionalities.
ReplyDeleteFurthermore, the RepeatDirection and RepeatColumns properties of a DataList control can be exploited to lay out the data in horizontal or vertical fashions.