ASP.Net Repeater dynamically using C# code

by top54u.com 09 Jul, 2008
Spotlight.....

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.

 

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.

Above example will generate the HTML source code as below:

 

<div id="Panel1">

<h4>Employee Names</h4>

<span>Nancy Davolio</span>

<hr />

<span>Andrew Fuller</span>

<hr />

<span>Janet Leverling</span>

<hr />

<span>Margaret Peacock</span>

<hr />

<span>Steven Buchanan</span>

<hr />

<span>Michael Suyama</span>

<hr />

<span>Robert King</span>

<hr />

<span>Laura Callahan</span>

<hr />

<span>Anne Dodsworth</span>

<hr />

</div>

Comments

8/10/2008 9:36:23 PM

Adam

Thank for this code, could you show me how I would add a footer to a dynamic repeater?

Adam gb

8/10/2008 10:40:47 PM

top54u.com

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.

top54u.com us

8/10/2008 10:48:36 PM

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 gb

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 9:12:04 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....