Friday, April 26, 2013

How to Bind, Edit, Update and Delete in Repeater control in Asp.net?



Introduction: Most of developer thinks Repeater is only used to display the data. Here I try to explain how we can edit, update and delete the data using repeater control via Itemcommand.

Description:

I have create table name Repeater.

Now open the Visual Studio>Go to File>New>Website. Add the Connectionstring in web.config file of website.
<configuration>
       <connectionStrings>
    <add name="connection" connectionString="Data Source=SYS-1F78031ED0A;Initial Catalog=TestBlog;Integrated Security=True"/>
       </connectionStrings>
       <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>



After that add new web form to website. Darg and drop a Repeater control from Toolbox>Data to .aspx page.

<table style=" border:1px solid #e8d5f1; width ="800px" cellpadding="0">
        <tr><td><asp:Label ID="Lblerror" runat="server" Text="Label"></asp:Label> </td></tr></table>
      
        <asp:Repeater ID="repcomment" runat="server"
            onitemcommand="repcomment_ItemCommand">
                     <HeaderTemplate>  
        <table style=" border:1px solid #53caf5; width ="800px" cellpadding="0">
<tr style="background-color:#1e54f5; color:White">
<td colspan="2">
<b>Latests Comment</b>
</td>
</tr>
</HeaderTemplate>
        <ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#D0F5A9;border-top:1px dotted #df5015; width:500px;" >
<tr>
<td>
Subject:
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Post_Subject") %>' Font-Bold="true"/>
 <asp:TextBox ID="txtsubject" runat="server" Text='<%#Eval("Post_Subject") %>' Visible="false"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Post_Comment") %>'/>
<asp:TextBox ID="txtcomment" runat="server" Text='<%#Eval("Post_Comment") %>' Visible="false"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<table style="background-color:#D0F5A9;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px;" >
<tr>
<td>User: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("Post_Username") %>'/></td>
<asp:TextBox ID="txtusername" runat="server" Text='<%#Eval("Post_Username") %>' Visible="false"></asp:TextBox>
<td>Posted Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("Date") %>'/></td>
 <td>
        <asp:LinkButton ID="LinkEdit" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="Edit">Edit</asp:LinkButton>
            <asp:LinkButton ID="LinkDelete" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="Delete">Delete</asp:LinkButton>
            <asp:LinkButton ID="LinkUpdate" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="Update" Visible="false">Update</asp:LinkButton>
            <asp:LinkButton ID="Linkcancel" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="Cancel" Visible="false">Cancel</asp:LinkButton>
            </td>
  </tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>

</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
        </asp:Repeater>

Add namespace to aspx.cs page.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Create a function to bind Repeater control.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        Lblerror.Visible = false;
        if (!IsPostBack)
        {
            Bindrepeatar();
        }
      

    }
    private void Bindrepeatar()
    {
        try
        {
            SqlCommand cmd = new SqlCommand("select * from Repetar", con);
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                repcomment.DataSource = ds;
                repcomment.DataBind();
            }
            else
            {
                repcomment.Visible = false;
                Lblerror.Text = "No Data Found";
              
            }
        }
        catch (Exception ex)
        {

        }
    }

Now go to the Repeater properties>Events and double click on ItemCommand.
protected void repcomment_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edit")
            {
                ((Label)e.Item.FindControl("lblSubject")).Visible = false;
                ((Label)e.Item.FindControl("lblComment")).Visible = false;
                ((Label)e.Item.FindControl("lblUser")).Visible = false;
                ((TextBox)e.Item.FindControl("txtsubject")).Visible = true;
                ((TextBox)e.Item.FindControl("txtcomment")).Visible = true;
                ((TextBox)e.Item.FindControl("txtusername")).Visible = true;
                ((LinkButton)e.Item.FindControl("LinkEdit")).Visible = false;
                ((LinkButton)e.Item.FindControl("LinkDelete")).Visible = false;
                ((LinkButton)e.Item.FindControl("LinkUpdate")).Visible = true;
                ((LinkButton)e.Item.FindControl("Linkcancel")).Visible = true;
            }
            if (e.CommandName == "Cancel")
            {
                ((Label)e.Item.FindControl("lblSubject")).Visible = true;
                ((Label)e.Item.FindControl("lblComment")).Visible = true;
                ((Label)e.Item.FindControl("lblUser")).Visible = true;
                ((TextBox)e.Item.FindControl("txtsubject")).Visible = false;
                ((TextBox)e.Item.FindControl("txtcomment")).Visible = false;
                ((TextBox)e.Item.FindControl("txtusername")).Visible = false;
                ((LinkButton)e.Item.FindControl("LinkEdit")).Visible = true;
                ((LinkButton)e.Item.FindControl("LinkDelete")).Visible = true;
                ((LinkButton)e.Item.FindControl("LinkUpdate")).Visible = false;
                ((LinkButton)e.Item.FindControl("Linkcancel")).Visible = false;
            }
            if(e.CommandName == "Update")
            {
                string str = ((TextBox)e.Item.FindControl("txtusername")).Text;
                string str1 = ((TextBox)e.Item.FindControl("txtsubject")).Text;
                string str2 = ((TextBox)e.Item.FindControl("txtcomment")).Text;
                SqlDataAdapter adp = new SqlDataAdapter("Update Repetar set Post_Username= @Post_Username, Post_Subject=@Post_Subject,Post_Comment=@Post_Comment where Id = @Id", con);
                adp.SelectCommand.Parameters.AddWithValue("@Post_Username", str);
                adp.SelectCommand.Parameters.AddWithValue("@Post_Subject", str1);
                adp.SelectCommand.Parameters.AddWithValue("@Post_Comment", str2);
                adp.SelectCommand.Parameters.AddWithValue("@Id", e.CommandArgument);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                Bindrepeatar();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "");
            }
            if (e.CommandName == "Delete")
            {
                SqlCommand cmd = new SqlCommand("delete from Repetar where Id = @Id", con);
                cmd.Parameters.AddWithValue("@Id", e.CommandArgument);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "");
                Bindrepeatar();
            }
        }
        catch (Exception ex)
        {

        }
    }
Now check the result.


Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment