Janmaruko Hōrensō
Janmaruko Hōrensō

Reputation: 89

asp.net insert HTML from Code Behind page

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?

Upvotes: 2

Views: 22493

Answers (2)

Umar Sid
Umar Sid

Reputation: 1337

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

Upvotes: 1

Andrei
Andrei

Reputation: 56688

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);

Upvotes: 10

Related Questions