1: I have a .html file that has some markup with some placeholder tags.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
First Name : <FIRSTNAME/> <br />
Last Name: <LASTNAME/>
</body>
</html>
2: I have a Class to hold data returned from db
public class Person
{
public Person()
{
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
3: PersonInfo.aspx I write out this .html with the placeholder replaced by actual values.
public partial class PersonInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person per= new Person();
per.FirstName = "Hello";
per.LastName = "World";
string temp = File.ReadAllText(Server.MapPath("~/template.htm"));
temp = temp.Replace("<FIRSTNAME/>", per.FirstName);
temp = temp.Replace("<LASTNAME/>", per.LastName);
Response.Write(temp);
}
}
4: PersonInfo.aspx is literally empty since I am injecting html from code-behind.
When the PersonInfo.aspx is called the html-template markup with appropriate values in the placeholder will be displayed. Also there are chances I would like to send the final markup in an html email (though that is not part of the question as I know how to email).
Is this the best way to fill values in my html-template or is the any other better alternative?
Note: This is a very simple sample example. My class is very complex involving objects as properties and also my html template have 40-50 placeholders.
So code in my step 3 will need 40-50 Replace statements.
Feel free to ask if any doubts and really appreciate any inputs.