ASP.Net Repeater dynamically using C# code

Updated on 10 Aug 2012,
Published on 14 Jul 2008

ASP.Net Repeater Databound Control can be generated dynamically using C# code. You can render the data items in dynamically generated Repeater Control. ASP.Net Panel Control can be used to add Repeater Control dynamically inside it as a child control. Method of database connectivity and Databinding using DataSource object of Repeater Control is same as we discussed in the previous article about ASP.Net Repeater Control Databinding using DataSource. While creating a dynamic Repeater control, you also have to generate its ItemTemplate, SeparatorTemplate and HeaderTemplate dynamically to display the data items retrieved from the database. In C# code behind use the DataSource object to bind the data with dynamic Repeater Control and add it as a child control of ASP.Net Panel to render all stuff on the web page.

Repeater Examples:

You can see the live samples and examples of Repeater Control from the following links:

HTML Code to Generate ASP.Net Repeater Control Dynamically

<asp:Panel ID="Panel1" runat="server">
</asp:Panel>

Only ASP.Net Panel control is required. 

C# Code to Generate ASP.Net Repeater Control Dynamically

// Repeater Control Databinding using Datasource
Repeater Repeater1 = new Repeater();
Repeater1.DataSource = myDataSet;
Repeater1.DataBind();

foreach (RepeaterItem repeatItem in Repeater1.Items)
{
    // if condition to add HeaderTemplate Dynamically only Once
    if (repeatItem.ItemIndex == 0)
    {
        RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
        HtmlGenericControl hTag = new HtmlGenericControl("h4");
        hTag.InnerHtml = "Employee Names";
        repeatItem.Controls.Add(hTag);
    }

    // Add ItemTemplate DataItems Dynamically
    RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
    Label lbl = new Label();
    lbl.Text = string.Format("{0} {1} <br />", myDataSet.Tables[0].Rows[ repeatItem.ItemIndex ][ "FirstName" ], myDataSet.Tables[0].Rows[ repeatItem.ItemIndex ][ "LastName" ]);
    repeatItem.Controls.Add(lbl);

    // Add SeparatorTemplate Dynamically
    repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
    LiteralControl ltrlHR = new LiteralControl();
    ltrlHR.Text = "<hr />";
    repeatItem.Controls.Add(ltrlHR);
}

// Add Repeater Control as Child Control
// of Panel Control
Panel1.Controls.Add(Repeater1);

You can see the SQL Connection, SQL Data Adapter, SQL Command and Dataset code and output (same output but different HTML source code) in the previous article: ASP.Net Repeater control Databinding using DataSource.

Output:

Get the output for above discussed code from the following link:

Repeater Dynamic

Continue to next tutorial: ASP.Net Repeater Nested Using C# to learn how to display the related data items in the nested Repeater control placed inside the ItemTemplate of another Repeater control.

17 Responses to "ASP.Net Repeater dynamically using C# code"
Adam
Thank for this code, could you show me how I would add a footer to a dynamic repeater?
Ezineasp.net
Hi Adam

Add the following code below this code line: Panel1.Controls.Add(Repeater1); at the end to add footer template.

RepeaterItem rItem = new RepeaterItem(0, ListItemType.Footer);

Label lbl1 = new Label();

lbl1.Text = "footer text";

rItem.Controls.Add(lbl1);

Repeater1.Controls.Add(rItem);



Above code will add the label control and text to the footer template of repeater control dynamically.
Adam
Thank you for the footer code - worked like a charm!

I really appreciate your time and help and such a speedy response.

Cheers
Adam
Adam Lock
Hi again, just wondered if you could give me another pointer concerning dynamic repeaters!

I've added a textbox and linkbutton to be added as the repeater is created, and added the following code to catch the ItemCommand of the button, so I can get the value entered in the textbox:

AddHandler Repeater1.ItemCommand, AddressOf rptAccessories_ItemCommand

And then I have the command captured in Protected Sub rptAccessories_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)

When I run the code though, the ItemCommand is never hit when I breakpoint it.


Can you give me some help on how to add the ItemCommand to a dynamic repeater?

Many Thanks
Adam
Jerico
I'm designing the membership database
So my problem is to validate the active members within the period of year, if anyone knows this type of validation using sql server stored procedures please help!
Harikrishnan.S
Very good article

that They bind a single coloumn only................

See this::::::::::::::::::::::::::::::::::::::::::::::::::::

if (repeatItem.ItemIndex == 0)
{
RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);

HtmlGenericControl hTag = new HtmlGenericControl("h4");

hTag.InnerHtml = "Employee Names";

repeatItem.Controls.Add(hTag);
}

Thay added only one coloumn here .............Employee Names

Here hw can we add multiple coloumns,,,,,,,,,,,,,,,,,,address,etc





pls reply



Harikrishnan.S
Ezineasp.net

Hi Harikrishnan

You have to generate dynamic HTML table inside the Dynamic Repeater's header as well as Itemtemplate columns to display the data items in tabular form.

Try to use the code given in the following tutorial to generate dynamic HTML tables:

[b]http://www.ezineasp.net/post/Create-dynamic-tables-using-HtmlControls-HtmlTable.aspx[/b]


For any confusion or coding problem you can ask back. Good Luck
Harikrishnan.S
but binded here a single coloumn only................

See this::::::::::::::::::::::::::::::::::::::::::::::::::::

if (repeatItem.ItemIndex == 0)
{
RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);

HtmlGenericControl hTag = new HtmlGenericControl("h4");

hTag.InnerHtml = "Employee Names";

repeatItem.Controls.Add(hTag);
}

Thay added only one coloumn here .............Employee Names

Here hw can we add multiple coloumns,,,,,,,,,,,,,,,,,,address,etc





pls reply



Harikrishnan.S
a
Can u make this code in vb.net
devathidhan
You have given a good sample. please do the same for more.
Sadanand
Hi,

Repeater Repeater1 = new Repeater();
Repeater1.Datasource is giving the below error.

"Error 1 'Repeater' does not contain a definition for 'DataSource'"

Plz reply.

Thanks
Sadanand
likeuclinux
the line: RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header); totally confuse me, for the headerItem seems is not used in any place!
@Likeuclinux, When I commented out the code RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header); and RepeaterItem repeaterItem = new RepeaterItem(rItems.ItemIndex, ListItemType.Item); It appears they are not even needed. I'm not sure why he put them in there.
zee
how to call repeater Event . like (ItemDataBound,ItemComand ,etc;)
SWATI
please tell me that how to select multiple chekbox if u selct single checkbox in header template of gridview.
Ezineasp.net
@Swati Hi you can learn it from the following tutorial: ASP.Net GridView Checkbox Select All using C# Hope it will work for you!
gopi
thank you very much boss.it helps me a lot
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers