Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to create this repeater in code behind not in design page i.e, aspx. Because i have created bootstrap 4.1.3 version tab-panel. This tab-panel is created in code behind not in design mode. This repeater should call in one of the tab-content so how to achieve this target please anybody explain me sir

What I have tried:

Repeater RptrQuestionsAndOptions = new Repeater();                
foreach (RepeaterItem item in RptrQuestionsAndOptions.Items)
{
    headeritem = new RepeaterItem(item.ItemIndex, ListItemType.Header);
    itemtemplate = new RepeaterItem(item.ItemIndex, ListItemType.Item);
    footeritem = new RepeaterItem(item.ItemIndex, ListItemType.Footer);
}
HtmlTable table = new HtmlTable();
table.Style["min-height"] = "300px";
table.Style["width"] = "100%";
headeritem.Controls.Add(table);

headeritem is declared globally.
Getting error when creating repeater in code behind that it is not creating headeritem. The error is at foreach loop. What i have to do now to reduce that error and achieve my goal. Please help anyone to solve this
Posted
Updated 20-Nov-18 11:53am
Comments
Richard Deeming 20-Nov-18 8:28am    
"Getting error when creating repeater in code behind that it is not creating headeritem."
How about giving us the details of the error, rather than just telling us you're getting an error?

Remember, we can't see your screen, access your computer, or read your mind. The only information we have is what you tell us here.

"headeritem is declared globally."
What does that mean? If you mean it's stored in a static field, then that's never going to work.

1 solution

You need to understand first how the stateless nature of web works and then understand how to work with dynamic controls. You can use google or use your preferred search engine to find those information in the net. Here's one good article to start with: Infinities Loop - TRULY Understanding Dynamic Controls (Part 1)[^]

To get started working with dynamic ASP.NET Web Controls generation, I'd suggest you to start here: How To: Create ASP.NET Web Server Control Templates Dynamically[^]
 
Share this answer
 
Comments
Member 8583441 22-Nov-18 6:34am    
In this i have forget to include the headertemplate, itemtemplate and footertemplate. When this included it works fines as expected.
As example,
Repeater repeater = new Repeater();
TemplateBuilder header = new TemplateBuilder();
header.AppendLiteralString("");
TemplateBuilder itemTemplate = new TemplateBuilder();
itemTemplate.AppendLiteralString("");
TemplateBuilder footer = new TemplateBuilder();
footer.AppendLiteralString("");
repeater.HeaderTemplate = header;
repeater.ItemTemplate = itemTemplate;
repeater.FooterTemplate = footer;
In itemTemplate i have created a method to bind the data from the database.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900