Write HTML in run time using C#

Y

Yiu

Here is my code:

private static ArrayList info=new ArrayList();
private Label[] label;

private void Page_Load(object sender, System.EventArgs e)
{
label=new Label[info.Count];

Response.Write("<table width=75%border=1>");
for(int cnt=info.Count;cnt>0;cnt--)
{
Response.Write("<tr>");
Response.Write("<td>");
label[cnt-1]=new Label();
label[cnt-1].Text=(String)info[cnt-1];
Page.Controls.Add(label[cnt-1]);
Response.Write("</td>");
Response.Write("</tr>");
}
Response.Write("</table>");
}
public static void setInfo(String s)
{
info.Add(s);
}

I want to create a new line between each label in run time, but i
cannot do this to create new line between label when use the code
above, have any good suggestion for me to solve this problem?
 
D

Dan Brussee

On your line Response.Write("</table>");
Change to
Response.Write("</table><br>");
 
C

Charlie

Hi,
I want to create a new line between each label in run time, but i
cannot do this to create new line between label when use the code
above, have any good suggestion for me to solve this problem?

You could insert a blank row between each label

Response.Write("<tr><td>&nbsp;</td></tr>");

or use a CSS class to control the height of each table cell or the
label itself (labels get rendered as spans)

Regards
Charlie
 
Y

Yiu

Below is my new code:
for(int cnt=info.Count;cnt>0;cnt--)
{
label[cnt-1]=new Label();
label[cnt-1].Text=(String)info[cnt-1];
Page.Controls.Add(label[cnt-1]);
Page.Response.Write("<tr><td>&nbsp;</td></tr>");
}

But have something wrong in the output, the <tr><td>&nbsp;</td></tr>
not print between the label, it print at the head of the HTML.Why?Have
solution?
<span>Free</span><span>Science Museum</span> is my Label output.

Below is my HTML output after add Label use the code above:
<tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowInformation</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post" action="ShowInformation.aspx"
id="Form1">
<input type="hidden" name="__VIEWSTATE"
value="dDwtNTMwNzcxMzI0Ozs+XUGdiuz4iPEe85152UYL3O0Fa+A=" />

<FONT face="新細明體">
<input type="submit" name="BackButton" value="Back"
id="BackButton" style="height:32px;width:64px;Z-INDEX: 101; LEFT:
24px; POSITION: absolute; TOP: 288px" /></FONT></form>
</body>
</HTML>
<span>Free</span><span>Science Museum</span>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top