2

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?

2
  • Can you display what the expected HTML outcome would look like? Commented Jun 2, 2013 at 17:12
  • With PHP the result it this d.pr/i/QMWe in this case count = 2 Commented Jun 2, 2013 at 17:14

2 Answers 2

10

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.