一、前台
<div>
<table id="tb1" border="1px" style="width: 300px;">
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
性别
</td>
<td>
年龄
</td>
</tr>
<%= BinGrid() %>
</table>
</div>
<div>
<asp:Label ID="lblTime" runat="server" Text="2013-02-18"></asp:Label>
</div>
</form>
</body>
</html>
二、后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using BLL;
using Model;
public partial class sample_Default : System.Web.UI.Page
{
private Student stu = new Student();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//显示常日期格式时间
DateTime dt = Convert.ToDateTime(lblTime.Text);
lblTime.Text = dt.ToLongDateString();
}
}
/// <summary>
/// 显示数据
/// </summary>
protected string BinGrid()
{
string Sid, Sname, Gender, Age, Uid = null;
List<StudentInfo> Tree = stu.Student_Select();
string js = "<script type='text/javascript'>";
StringBuilder build = new StringBuilder(js);
foreach (StudentInfo dt in Tree)
{
if (dt != null)
{
Sid = dt.Sid.ToString();
Sname = dt.Sname.ToString();
Gender = dt.Gender.ToString();
Age = dt.Age.ToString();
Uid = dt.Uid.ToString();
if (Sid != null)
{
string jss = "var row = tb1.insertRow();";
jss += "var sid = row.insertCell();";
jss += "sid.innerText='" + Sid + "';";
jss += "var sname = row.insertCell();";
jss += "sname.innerText='" + Sname + "';";
jss += "var gender = row.insertCell();";
jss += "gender.innerText='" + Gender + "';";
jss += "var age = row.insertCell();";
jss += "age.innerText='" + Age + "';";
build.Append(jss);
}
}
}
string last = null;
last = build.ToString() + "</script>";
return last;
}
}