NVelocity 主从表
经常来博客园找资料。今天也发布一点点原创。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NVelocityTemplateEngine;
using NVelocityTemplateEngine.Interfaces;
using System.Collections.Generic;
namespace DianXin.UserWeb
{
public partial class wei9931_MasterSalver : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(ProcessFileTemplate());
}
private string ProcessFileTemplate()
{
IList<user_slave> user_slave10 = new List<user_slave>();
user_slave temp_10 = new user_slave(10,"语文1");
user_slave10.Add(temp_10);
user_slave temp_11 = new user_slave(10, "数学1");
user_slave10.Add(temp_11);
IList<user> users = new List<user>();
user user1=new user(10,"wei9931",user_slave10);
users.Add(user1);
IList<user_slave> user_slave20 = new List<user_slave>();
user_slave temp_20 = new user_slave(20, "语文2");
user_slave20.Add(temp_20);
user_slave temp_21 = new user_slave(20, "数学2");
user_slave20.Add(temp_21);
user user2 = new user(20, "baidu.com",user_slave20);
users.Add(user2);
IList<user_slave> user_slave30 = new List<user_slave>();
user_slave temp_30 = new user_slave(30, "语文3");
user_slave30.Add(temp_30);
user_slave temp_31 = new user_slave(30, "数学3");
user_slave30.Add(temp_31);
user user3 = new user(30, "baidu.com",user_slave30);
users.Add(user3);
string templateDir = HttpContext.Current.Server.MapPath("template");
string templateName = "wei9931_masterslaver.vm";
INVelocityEngine fileEngine = NVelocityEngineFactory.CreateNVelocityFileEngine(templateDir, true);
IDictionary context = new Hashtable();
context.Add("arrayList", users);
return fileEngine.Process(context, templateName);
}
public class user_slave
{
private int id;
private string studentclass;
public int Id
{
set { id = value; }
get { return id; }
}
public string Studentclass
{
set { studentclass = value; }
get { return studentclass; }
}
public user_slave(int id, string studentclass)
{
this.id = id;
this.studentclass = studentclass;
}
}
public class user
{
private string name;
private int id;
private IList<user_slave> user_slave_tab;
public IList<user_slave> User_slave_tab
{
set { this.user_slave_tab = value; }
get { return user_slave_tab; }
}
public string Name
{
set { name = value; }
get { return name; }
}
public int Id
{
set { id = value; }
get { return id; }
}
public user(int id, string name, IList<user_slave> user_slave_tabs)
{
this.name = name;
this.id = id;
this.user_slave_tab = user_slave_tabs;
}
}
}
}
以下是模板文件:
_____________________________________________________________________________
<table border="1">
#foreach ( $customer in $arrayList )
<tr> <td>$customer.name</td>
<td>
<table border="1">
<tr>
#foreach( $selection in $customer.User_slave_tab )
<td>$selection.studentclass</td>
#end
</tr>
</table>
</td>
</tr>
#end
</table>