Friday 9 August 2013

How to Bind, Insert, Update, Delete in Repeater Control in Asp.Net.







Description:-

            In this Example we explain that How to create CRUD operation in Repeater Control or How to Insert Update Delete in Repeater in Asp.Net.

Repeater Control are best Control because you can easily  create Tables,Unordered lists, Ordered lists, anchor tags, database based menus, Image galleries etc. using repeater control.

Repeater Control are used to Bind data which are Repeated more time.in this Example we Create Examle of Like and Comment same like in Social Website.in which you can easily insert update and delete the comment.



There are Five Types of Templates of Reapeter Control are as Described below

ItemTemplate: ItemTemplate are used to  defines each item is rendered from data source collection.

AlternatingItemTemplate: AlternatingItemTemplates are used to change the background color and css-styles of AlternatingItems in DataSource collection

HeaderTemplate: HeaderTemplate are used to display Header text or Header Image for DataSource collection and apply different styles for header text .

FooterTemplate: FooterTemplate are used to display footer Template for element for DataSource collection

SeparatorTemplate: SeparatorTemplate are used to  separator each element which separates each Item in Item collection. Usually, SeparateTemplate will be <br> html element or <hr> html element.


In this we Create ItemCommand of Repeater same as RowCommand in in Gridview.like as follows:

<asp:Repeater ID="RepDetails" runat="server" onitemcommand="rept_ItemCommand">

in which ItemCommand we Handle Edit and Delete Command of Repeater and we pass the Command Argument in Edit Delete Event click.

First you have to create the following Table 



to Show Example Of How to Create Paging or Custom Paging in Reapeter so plz click Here Paging or Custom Paging in Reapeter Control

to Show Example of How to Create Insert,Update,Delete in Gridview plz Click Here CRUD operation in GridView


repeter.aspx:-



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



<!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 id="Head1" runat="server">
<title>Repeator Control Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter Name: </td>
<td><asp:TextBox ID="txtName" runat="server"/></td>
</tr>

<tr>
<td valign="top">Enter Comments:</td>
<td><asp:TextBox ID="txtComment" runat="server" Rows="5" Columns="20" TextMode="MultiLine"/></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
<div>
<asp:Repeater ID="RepDetails" runat="server" onitemcommand="rept_ItemCommand">
<HeaderTemplate>
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:#df5015; color:White">
<td colspan="2">
<b>Comments</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<tr>

</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("comment") %>'/>
      <asp:TextBox ID="txtComment" runat="server" Text='<%#Eval("comment") %>' Visible="false"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("unm") %>'/>
      <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("unm") %>' Visible="false"></asp:TextBox>
</td>
<td>Created Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("postdate") %>'/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
     <tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #c1650f;border-bottom:1px solid #c1650f; width:300px" >
<tr>
   <td>
        <asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="edit">Edit</asp:LinkButton>
            <asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="delete" onclientclick="return confirm('Are you sure you want to delete?')">Delete</asp:LinkButton>
            <asp:LinkButton ID="lnkUpdate" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="update" Visible="false">Update</asp:LinkButton>
            <asp:LinkButton ID="lnkCancel" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="cancel" Visible="false">Cancel</asp:LinkButton>
   </td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

 



repeter.aspx.cs:-



using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI.WebControls;

public partial class repetar : System.Web.UI.Page
{
    private SqlConnection con = new SqlConnection("Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindcomment();
        }
    }
    // This button click event is used to insert comment details and bind data to repeater control
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into comment (unm,comment,postdate) values(@userName,@comment,@postedDate)", con);
        cmd.Parameters.AddWithValue("@userName", txtName.Text);

        cmd.Parameters.AddWithValue("@comment", txtComment.Text);
        cmd.Parameters.AddWithValue("@postedDate", DateTime.Now);
        cmd.ExecuteNonQuery();
        con.Close();
        txtName.Text = string.Empty;

        txtComment.Text = string.Empty;
        bindcomment();
    }
    //Bind Data to Repeater Control
    protected void bindcomment()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from comment Order By postdate desc", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            RepDetails.Visible = true;
            RepDetails.DataSource = ds;
            RepDetails.DataBind();
        }
        else
        {
            RepDetails.Visible = false;
        }

        con.Close();
    }

    protected void rept_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
          
            ((TextBox)e.Item.FindControl("txtName")).Visible = true;
            ((TextBox)e.Item.FindControl("txtComment")).Visible = true;
          
            ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkDelete")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = true;
        }
        if (e.CommandName == "update")
        {
            string Name = ((TextBox)e.Item.FindControl("txtName")).Text;
            string comment = ((TextBox)e.Item.FindControl("txtComment")).Text;
           
            SqlDataAdapter adp = new SqlDataAdapter("Update comment set unm= @Name,comment=@comment where id = @Id", con);
            adp.SelectCommand.Parameters.AddWithValue("@Name",Name);
            adp.SelectCommand.Parameters.AddWithValue("@comment",comment);
          
            adp.SelectCommand.Parameters.AddWithValue("@Id", e.CommandArgument);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            bindcomment();
        }
        if (e.CommandName == "delete")
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("delete from comment where id = @Id", con);
            cmd.Parameters.AddWithValue("@Id", e.CommandArgument);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
            bindcomment();
        }
        if (e.CommandName == "cancel")
        {
          
            ((TextBox)e.Item.FindControl("txtName")).Visible = false;
            ((TextBox)e.Item.FindControl("txtComment")).Visible = false;
          
            ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkDelete")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = false;
        }

    }
}
 


7 comments:

  1. Its really helpful for me to understand where we i lost in my previous interview. Thanks.
    If anyone wants to Learn .NET in Chennai go to the Besant Technologies which is No.1 Training Institute in Chennai.

    http://www.besanttechnologies.com/training-courses/dot-net-training

    ReplyDelete
  2. Hi thanks for the article. It helped me a lot. While googling I have found this article which is also very helpful to understand edit delete update using asp .net repeater:

    http://www.codingfusion.com/Post/Repeater-Edit-Update-Delete-in-Asp-net-using-Ado

    ReplyDelete
  3. Thanks for the article. It has given me so much information that I shared with my students in the class. Come up with more such articles.
    Shashaa
    best Dot Net training in Chennai | best Dot Net training in Chennai | best Dot Net training in Chennai

    ReplyDelete
    Replies
    1. Thanks for comment.also visit the blog for new post.

      Delete
  4. Thanks for sharing information with clear explanation. This is really awesome to understand.

    Thanks,
    Dot Net Training in Chennai | Online Dot Net Course in Chennai

    ReplyDelete