怎样在c#中加入HTML代码?
我想实现直接用C#显示出HTML代码。应该怎么写?就是用C#根据数据的多少自动的添加表格的行数。
例如我想在c#中加入下面的html代码,应该怎么写?
<tr valign="top" align=center>
<td height="20" style="width: 201px">
</td>
</tr>
[解决办法]
用Response.write()输出。
大致思路:
1、在本页面的后台声明一个ArrayList对象list,并对该对象赋值(可以在onload里面也可以是后期调用方法,这个根据不同需要而定)。
2、在页面源码模式下调用该ArrayList对象<% for(int i=0;i<list;i++) %>
3、在循环中直接用Response.write()输出即可以实现。
给楼主一个事例——百度分页的实现:
这个和你想要方法思路是一样的
<%
if (currentPage > pageCounts) //如果当前页大于总页数,就设置当前页码为总页数
{
currentPage = pageCounts;
}
if (currentPage < 1) //如果当前页小于1,就设置当前页码为1
{
currentPage = 1;
}
if (currentPage != 1) //当页码大于1的时候显示“上一页”
{
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage - 1) + "">上一页</a>");
}
if (currentPage - 4 > 1) //如果显示的最小页码不是1,则在最小页码前添加省略号
{
if (currentPage - 4 > 4)
{
Response.Write("<a href="ShengLuo.aspx?page=" + 1 + "" style="text-decoration:none"> 1 </a>");
Response.Write("<a href="ShengLuo.aspx?page=" + 2 + "" style="text-decoration:none"> 2 </a>");
Response.Write("<a href="ShengLuo.aspx?page=" + 3 + "" style="text-decoration:none"> 3 </a>");
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage - 4 - 1) + "" style="text-decoration:none">... </a>");
}
if (currentPage - 4 <= 4)
{
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage - 4 - 1) + "" style="text-decoration:none">... </a>");
}
}
for (int j = 1; j < 5; j++) //写出前4页页码
{
if (currentPage - (5 - j) <= 0)
{
j = 5 - currentPage;
continue;
}
if (currentPage - (5 - j) > 0)
{
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage - (5 - j)) + "" style="text-decoration:none">" + (currentPage - (5 - j)) + " </a>");
continue;
}
break;
}
Response.Write("<a href="ShengLuo.aspx?page=" + currentPage + "" style="text-decoration:none">" + currentPage + " </a>"); //写出本页页码
for (int i = 1; i < 5; i++) //写出后4页页码
{
if (currentPage+i <= pageCounts)
{
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage + i) + "" style="text-decoration:none">" + (currentPage + i) + " </a>");
}
}
//如果最大页码小于总页数,则在最大页码右侧添加省略号
if (currentPage + 4 < pageCounts - 4)
{
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage + 4 + 1) + "" style="text-decoration:none">... </a>");
Response.Write("<a href="ShengLuo.aspx?page=" + (pageCounts - 2) + "" style="text-decoration:none">" + (pageCounts - 2) + " </a>");
Response.Write("<a href="ShengLuo.aspx?page=" + (pageCounts - 1) + "" style="text-decoration:none">" + (pageCounts - 1) + " </a>");
Response.Write("<a href="ShengLuo.aspx?page=" + pageCounts + "" style="text-decoration:none">" + pageCounts + " </a>");
}
if (currentPage + 4 >= pageCounts - 4 && currentPage + 4 < pageCounts)
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage + 4 + 1) + "" style="text-decoration:none">... </a>");
if (currentPage != pageCounts)
{
Response.Write("<a href="ShengLuo.aspx?page=" + (currentPage + 1) + "">下一页</a>");
}
%>