The article is about edit, update, and delete records in a simple way inside repeater control.
The HTML source page with repeater control should look like the following
<asp:Repeater ID="cpRepeater" runat="server"
onitemcommand="cpRepeater_ItemCommand"
onitemdatabound="cpRepeater_ItemDataBound">
<HeaderTemplate>
<table width="500px" border="1px">
<tr style="background-color:#fb7700">
<td >Check</td>
<td >Member</td>
<td >Type</td>
<td >Options</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#ffffff">
<td >
<asp:CheckBox ID="chkDelete" runat="server" />
</td>
<td >
<asp:Label ID="lblID" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
<asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Member")%>'></asp:Label>
<asp:TextBox ID="txtName" BackColor="#d4d0c8" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Member")%>' Visible="false"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="ddlType" runat="server">
</asp:DropDownList>
</td>
<td ><asp:LinkButton ID="lnkEdit" runat="server" CommandName="edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Edit</asp:LinkButton>
<asp:LinkButton Visible="false" ID="lnkUpdate" runat="server" CommandName="update" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Update</asp:LinkButton>
<asp:LinkButton Visible="false" ID="lnkCancel" runat="server" CommandName="cancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Cancel</asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="delete" OnClientClick='javascript:return confirm("Are you sure you want to delete?")'
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="background-color:#15880a">
<td colspan="5">
</FooterTemplate>
</asp:Repeater>
<asp:LinkButton ID="lnkDelSelected" ForeColor="White" runat="server" onclick="LinkButton1_Click" OnClientClick='javascript:return confirm("Are you sure you want to delete?")'>Delete Selected</asp:LinkButton>
Connection string in Web.Config
<appSettings>
<add key="ConnectionString" value="Data Source=MADHU\MUKRAM_SQL2005;Initial Catalog=EcareDiary;User ID=sa;Password=;"/>
</appSettings>
Aspx.cs looks like:
To display the records.
private void BindRepeater()
{
SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand SqlCmd = new SqlCommand("select * from CPMEMBERS", SqlCnn);
SqlDataAdapter SqlAd1 = new SqlDataAdapter(SqlCmd);
DataSet ds = new DataSet();
SqlAd1.Fill(ds, "CPMEMBERS");
cpRepeater.DataSource = ds;
cpRepeater.DataBind();
}
The code in ItemDataBound event of repeater.
Populating the dropdownlist dynamically.
protected void cpRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList ddlType = (DropDownList)e.Item.FindControl("ddlType");
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand SqlCmd = new SqlCommand("select * from CPMEMBERS", SqlCnn);
SqlDataAdapter SqlAd1 = new SqlDataAdapter(SqlCmd);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlAd1.Fill(dt);
ddlType.DataTextField = "Type";
ddlType.DataSource = dt;
ddlType.DataBind();
ddlType.SelectedValue = DataBinder.Eval(e.Item.DataItem, "UpdatedType").ToString();
}
}
Updating and deleting of the records using Item Command Event of repeater
if (e.CommandName == "update")
{
SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand SqlCmd = new SqlCommand("update CPMEMBERS set Member=@Member, UpdatedType=@UpdatedType where id=@ID", SqlCnn);
SqlCmd.Parameters.Add("@Member", SqlDbType.VarChar).Value = txtName.Text;
SqlCmd.Parameters.Add("@UpdatedType", SqlDbType.VarChar).Value = ddlType.SelectedItem.Text;
SqlCmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = e.CommandArgument;
try
{
SqlCnn.Open();
SqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
if (SqlCnn.State == ConnectionState.Open)
SqlCnn.Close();
}
BindRepeater();
}
if (e.CommandName == "delete")
{
SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand SqlCmd = new SqlCommand("delete CPMEMBERS where id=@ID", SqlCnn);
SqlCmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = e.CommandArgument;
try
{
SqlCnn.Open();
SqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
if (SqlCnn.State == ConnectionState.Open)
SqlCnn.Close();
}
BindRepeater();
}
Output looks like the following.
Output
In edit mode(after clicking edit button)
Repeater after updating
After Deleting the multiple rows repeater looks as