public string GetString() //自定义方法,供前台调用
{
StringBuilder strBuilder = new StringBuilder() //用StringBuilder接受所有拼接的字符串,因为StringBuilder效率比拼接字符串效率高,当然以下代码还需要你自己去完善,需要完整代码的话,继续追问
strBuilder.AppendLine("<table>")
//接下来取出数据
foreach(dataRow row in datas) //遍历取出数据放到tr中
{
strBuilder.AppendLine("<tr><td>i</td><td>d["date"]</td><td>d["goodsCompany"]</td>.....</tr>.")
i++
}
strBuilder.AppendLine("</table>")
return strBuilder.ToString()//得到strBuilder返给GetString()这个自定义方法
}
2、前台调用
找一块区域,调用GetString()
<%=GetString()%>//这样就得到了,哈哈,完毕,下面介绍其他的两种方法
3、如果需要异步调用的话,就需要ajax配合json技术了
4、想偷懒也不注重效率的话,就用数据绑定控件吧,比如Reapter
ps:有不懂的继续追问
文件1: Deafault.aspx<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HtmlTable演示</title>
</head>
<body>
<h1>HtmlTable实例演示</h1>
<form id="Form1" runat="server">
<table id="MyTable" cellpadding="5" cellspacing="0" border="1" runat="server" >
<tbody>
</tbody>
</table><p>
输入表格行数:
<input type="text" id="MyTextOne" value="1" runat="server"/></p>
<p>输入表格单元格:
<input type="text" id="MyTextTwo" value="1" runat="server" /></p>
<p><input id="Submit1" type="submit" value="产生表格" runat="server" onserverclick="Submit1_ServerClick" /></p>
</form>
</body>
</html>
文件2:Deafault.aspx.cs
using System
using System.Data
using System.Configuration
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit1_ServerClick(object sender, EventArgs e)
{
int row = 0
int numrows = Convert.ToInt32(MyTextOne.Value) // 获得文本框中整型数
int numcells = Convert.ToInt32(MyTextTwo.Value)
for (int j = 0 j < numrows j++)
{
HtmlTableRow r = new HtmlTableRow()
if (row % 2 == 1)// 设置行的背景色
r.BgColor = "Gainsboro"
row++
for (int i = 0 i < numcells i++)
{
HtmlTableCell c = new HtmlTableCell() //创建单元格对象
c.Controls.Add(new LiteralControl("行: " + (j + 1).ToString() + ", 单元格: " +(i + 1).ToString()))
r.Cells.Add(c) //添加该单元格对象
}
MyTable.Rows.Add(r)//添加行对
}
HtmlTableRow r2 = new HtmlTableRow()
HtmlTableCell c2 = new HtmlTableCell()
c2.Controls.Add(new LiteralControl("吉林大学远程教育学院"))
c2.ColSpan = 3 //合并单元格,colspan属性可以实现单元格跨越多列
r2.Cells.Add(c2)
MyTable.Rows.Add(r2)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)