Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a problem with ASP.NET. I'm using C#.

With a SQL query i have the number exactly of rows in a Database, i need to write the same number of div tag to show the results.

This is the code

count is a variable that contain the number of rows.

Projects is a List

for (int i = 0; i < count; i++)
{
    form1.Controls.Add(new Literal() { 
         ID = "ltr" + i,

         Text = "<div class= 'container' >Name = " + Projects[i].Name + " ;</div>" });
}

But there is a problem, i must place these div into another div with ID = Container.

in this way Literal controls aren't placed into div#Container

How can i do to place the For results into a div?

share|improve this question
Can you display what the expected HTML outcome would look like? – Chris Gessler Jun 2 at 17:12
With PHP the result it this d.pr/i/QMWe in this case count = 2 – Gianmarco Spinaci Jun 2 at 17:14

2 Answers

up vote 2 down vote accepted

Instead of Literal control, which is designed to render text, not html tags, you can use either HtmlGenericControl:

HtmlGenericControl div = new HtmlGenericControl();
div.ID = "div" + i;
div.TagName = "div";
div.Attributes["class"] = "container";
div.InnerText = string.Format("Name = {0} ;", Projects[i].Name);
form1.Controls.Add(div);

or Panel control, which is rendered into div, with Literal inside it:

Panel div = new Panel();
div.ID = "panel" + i;
div.CssClass = "container";    
div.Controls.Add(new Literal{Text = string.Format("Name = {0} ;", Projects[i].Name)});    
form1.Controls.Add(div);
share|improve this answer

Instead U Should Use Repeater Control, it'll be quite easy to work with.

Steps

  1. Take a SqlDatasource.
  2. Take a repeater Control.
  3. Set its datasource to .
  4. Design the itemTemplate as you like.
  5. Done..!

If have any doubt follow this LINK

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.