I'm building a webpage on which I display the n newest newsposts on the front page, and this is the method that gets the posts and returns them to the .aspx-file. I use a asp:substitution-control to insert the string into the webpage.
I think my way of doing this i kind of messy and ugly, and not very flexible if i want to do little changes on the template. Is there any way for me to achieve what i want while having the template itself in a separate .aspx or .ascx file? I've tried, but cant come up with a smart way of handing the data onto the template-file.
Plus, when (if?) i've put the template in a separate file - how do i repeat it within another .aspx-file (within a contentplaceholder, if that matters. The layout is controlled by a master-page) with different values for each instance?
public static string getShortPosts(int postNumber)
{
DbConnection d = new DbConnection();
DataTable tbl = d.selectQuery("SELECT TOP (" + postNumber + ") news.id, news.title, news.newsContent, news.excerpt, news.date, news.userid, news.urlTitle, " +
"news.isPublished, users.firstName, users.lastName FROM news INNER JOIN users ON news.userid = users.userid " +
"ORDER BY id DESC;");
DataTableReader r = tbl.CreateDataReader();
StringBuilder s = new StringBuilder("");
while (r.Read())
{
/* THIS */
s.Append(
"<div class=\"newsBox\">" + System.Environment.NewLine +
"<h2>" + r["title"]+ "</h2>" + System.Environment.NewLine +
"<p>" + r["excerpt"] + "</p>" + System.Environment.NewLine +
"</div>" +System.Environment.NewLine+System.Environment.NewLine
);
}
return s.ToString();
}