Click here to Skip to main content
15,881,687 members
Articles / Web Development / ASP.NET
Reference

Nested Repeater Editing Asp.net c#

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
25 Feb 2014CPOL1 min read 24K   356   6   1
Nested Repeater Editing in asp.net code behind c#

Introduction 

Nested Repeater shows the Repeater inside Repeater which indicates that the parent Repeater has an child Repeater. Then, it provides to edit both parent and child Repeater with perfect editing.

Image 1 

After Click On Edit It will shows like below snap. 

Image 2

Background

This is useful in the regular situation whenever there is a need to display multiple records related to one record, at that time implement the nested Repeater concept. After implementation, sometimes you need to edit that record on those page. At that time, it is very useful and provides the powerful solution for editing record in nested Repeater.

Using the Code

Using this code, you can edit parent Repeater record and child Repeater record at the same time without any DropDown selection that just provides simple TextBox editing.

As I show in the image, that is useful at the time of displaying state related country and also category related company, etc.

Here, this article gives all the implementation about nested Repeater and provides editing in both. You have to write your own select command, Connection then integrate the below code.

Step 1: Add the below code in your .aspx page:

HTML
<div align="center">
    <table cellpadding="4" cellspacing="1">
        <tr>
            <td align="center" bgcolor="#FFFFCC">
                <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Larger" 
                        ForeColor="#003300" Text="Nested Repeater With Edit Delete"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="center">
    <asp:Repeater ID="rptcountry" runat="server" OnItemCommand="rptcountry_ItemCommand">
            <HeaderTemplate>
            <table cellpadding="4" cellspacing="1" border="1">
                <tr style="background-color: #333300; font-size: large; color: #FFFFCC; font-weight: bold;">
                    <td>
                        Country Name
                    </td>
                    <td>
                        State Name
                    </td>
                    <td >
                        Action
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="lblcountryid" Visible="false" runat="server" Text='<%# Eval("countryid") %>'></asp:Label>
                    <asp:Label ID="lblcountryname" runat="server" Text='<%# Eval("countryname") %>'></asp:Label>
                    <asp:TextBox ID="txtcountryname" runat="server" Text='<%# Eval("countryname") %>'
                            Visible="false"></asp:TextBox>
                </td>
                <td>
                    <asp:Repeater ID="rptstate" runat="server">
                        <ItemTemplate>
                            <table>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblstateid" runat="server" Text='<%# Eval("stateid") %>' Visible="false"></asp:Label>
                                        <asp:Label ID="lblstatename" runat="server" Text='<%#Eval("statename") %>'></asp:Label>
                                        <asp:TextBox ID="txtstatename" runat="server" Text='<%#Eval("statename") %>' Visible="false"></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:Repeater>
                </td>
                <td>
                    <asp:LinkButton ID="lnkEdit" runat="server" CommandName="edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "countryid")%>'>Edit</asp:LinkButton>
                    <asp:LinkButton Visible="false" ID="lnkUpdate" runat="server" CommandName="update"
                            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "countryid")%>'>Update</asp:LinkButton>
                    <asp:LinkButton Visible="false" ID="lnkCancel" runat="server" CommandName="cancel"
                            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "countryid")%>'>Cancel</asp:LinkButton>
                </td>                    
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
            </td>
        </tr>
    </table>
</div> 

Step 2: Then add this simple code to your .CS file:

C#
Class_Main cm = new Class_Main();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DisplayCountryData();
        DisplayStateData();
    }
}

public void DisplayCountryData()
{   // add you connetion and write your command parent table select command
    DataSet ds = cm.select("Select * from country");
    rptcountry.DataSource = ds;
    rptcountry.DataBind();
}

public void DisplayStateData()
{
    for (int i = 0; i <= rptcountry.Items.Count - 1; i++)
    {
        // add you connetion and write your command parent table select command
        Label id = (Label)rptcountry.Items[i].FindControl("lblcountryid");
        Repeater rptState = (Repeater)rptcountry.Items[i].FindControl("rptstate");
        DataSet ds = cm.select("Select * from state where countryid=" + id.Text + "");
        rptState.DataSource = ds;
        rptState.DataBind();
    }
}

protected void rptcountry_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "edit")
    {
        Repeater rptstate = (Repeater)e.Item.FindControl("rptstate");
        ((Label)e.Item.FindControl("lblcountryname")).Visible = false;
        ((TextBox)e.Item.FindControl("txtcountryname")).Visible = true;

        for (int i = 0; i <= rptstate.Items.Count - 1; i++)
        {
            ((Label)rptstate.Items[i].FindControl("lblstatename")).Visible = false;
            ((TextBox)rptstate.Items[i].FindControl("txtstatename")).Visible = true;
        }

        ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = false;
        ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = true;
        ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = true;

    }
    else if (e.CommandName == "update")
    {
        Repeater rptstate = (Repeater)e.Item.FindControl("rptstate");
        
        for (int i = 0; i <= rptstate.Items.Count - 1; i++)
        {
            Label lblStateid = (Label)rptstate.Items[i].FindControl("lblstateid");
            TextBox txtstatename = (TextBox)rptstate.Items[i].FindControl("txtstatename");
            cm.select("update state set statename='" + txtstatename.Text + "' where stateid=" + lblStateid.Text + "");
        }
            
        TextBox txtcountryname = (TextBox)e.Item.FindControl("txtcountryname");
        cm.select("update country set countryname='" + txtcountryname.Text + "' where countryid=" + e.CommandArgument + "");
        DisplayCountryData();
        DisplayStateData();
    }
    else if (e.CommandName == "cancel")
    {
        Repeater rptstate = (Repeater)e.Item.FindControl("rptstate");
        ((Label)e.Item.FindControl("lblcountryname")).Visible = true;
        ((TextBox)e.Item.FindControl("txtcountryname")).Visible = false;
        
        for (int i = 0; i <= rptstate.Items.Count - 1; i++)
        {
            ((Label)rptstate.Items[i].FindControl("lblstatename")).Visible = true;
            ((TextBox)rptstate.Items[i].FindControl("txtstatename")).Visible = false;
        }
            
        ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = true;
        ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = false;
        ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = false;
    }
}

Points of Interest

In this tip, we have seen how to implement nested Repeater and main provide it to editing facility in both repeater at the same time. It seems simple, but many beginners struggle with it, hence I Completed it.

History

  • 26 February, 2014: First version 
Thanking you !!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) THOMSON REUTERS
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 1085055412-Jul-14 0:27
Member 1085055412-Jul-14 0:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.