Asp.net static dynamic page output object attributes (Object Association)

Source: Internet
Author: User

Asp.net static dynamic page output object attributes (Object Association)
Asp.net static dynamic page output object attributes (Object Association)

The template engine can not only output a single data, but also directly output object attributes.

Using System; using System. Collections. Generic; using System. Linq; using System. Web; using NVelocity. Runtime; using NVelocity. App; namespace czbk {////// Summary of Handler2 ///Public class Handler2: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = text/html; // plain indicates static output of common text format. html format text User user = new User (); user should be output. username = Wang Damai; user. userphone = 18312345678; Role role = new Role (); role. roleid = 1001; role. rolename = super funny king; user. user_Role1 = role; // assign VelocityEngine vltEngine = new VelocityEngine () to the correlated object; vltEngine. set Property (RuntimeConstants. RESOURCE_LOADER, file); vltEngine. SetProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. Web. Hosting. HostingEnvironment. MapPath (~ /); // The vltEngine folder where the template file is located. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put (u, user); // you can use $ u in the Template to reference Template vltTemplate = vltEngine.GetTemplate(Users.htm); System. IO. stringWriter vltWriter = new System. IO. stringWriter (); vltTemplate. merge (vltContext, vltWriter); string html = vltWriter. getStringBuilder (). toString (); context. response. write (html) ;}public bool IsReusable {get {return false ;}} public class User {private string username; public string Username {get {return username ;} set {username = value ;}} private string userphone; public string Userphone {get {return userphone ;}set {userphone = value ;}} www.bkjia.com private Role User_Role; // introduce an associated class Object public Role User_Role1 {get {return User_Role;} set {User_Role = value ;}}} public class Role {private string roleid; public string Roleid {get {return roleid;} set {roleid = value ;}} private string rolename; public string Rolename {get {return rolename ;} set {rolename = value ;}}}}

 

Front-end code

  

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.