0

So I'm trying to use C# to grab stored HTML in a database and then display it between a set HTML header and footer (I have a couple of these for different pages, so a masterpage would make life more complicated rather than less. I also come from a PHP background where this is sort of the way things work).

<!--#include virtual="header.html" --> 
<% @Page Language="C#" Debug="true" %>
<% @Import Namespace="System.Data.Odbc" %>
<% @Import Namespace="System.Web.Configuration" %>

<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
           //Gets the HTML Successfully
           //Response.Write(fetched_html);
    }
</script>
<!--#include virtual="footer.html" --> 

Unfortunately with the current workflow the code generates content before the header or footer are put on the page, putting all the stored HTML at the top of the page before the header. I've tried a couple different Page_Events and none of them seem to work, so I need to know what to do.

Is there an equally simple way to reference and insert a simple html header file that would render before any of the Page_Events? I've tried Page_PreLoad and others to no success. Either the code is not fetched at all or I run into the same issue.

  • 3
    ASP.NET is not PHP. Stop thinking in terms of includes and learn to use master pages properly. – John Saunders May 04 '12 at 19:52
  • Master pages, as most of ASP.NET, are messy, unwieldy, inefficient, and remove a lot of the simplicity and control I've gotten used to in more standardised languages like PHP. While I realise they may be useful in a variety of situations, they are not how I code and due to the variety of pages I need (all in the same directory) for this project, they would actively be a hindrance to work with. Thank you for your incredibly useful comment though. –  May 04 '12 at 20:05
  • You obviously can't get your head out of your PHP and learn how master pages are meant to be used. They have none of the problems you describe, as each page can have a different master page if required, and master pages can inherit from each other. And calling PHP "more standardized" is an outrageous comment. It would be funny if it weren't pathetic and irrelevant. – John Saunders May 04 '12 at 21:19
  • I've found a lot of people decrying ASP.NET as non-standard. Not to say PHP is completely standardised (dollar signs, anyone?), but most of the people I've talked to will at least put it as more standard than ASP.NET and C# are. A simple google search will produce many results. C# is a bit better about it of course, but it still retains many of the quirks that make it a bit more bloated than I like. In the end, we all have coding styles we like. Personally, I find .NET and C# entirely unwieldy and actively dislike working in them. Doesn't mean they're bad - just not my style. –  May 04 '12 at 21:27
  • Friendly suggestion: [so] is more about objective questions and answers. You may want to leave your opinions at the door when you enter here. Opinions without facts to back them up frequently result in closed and deleted questions and answers here. "a lot of people decrying" is very far from being objective fact. – John Saunders May 04 '12 at 21:48
  • @user798080 - Out of curiosity, what brings you to .NET then? – zimdanen May 05 '12 at 01:21
  • Work =P. Otherwise I'd be staying far away. Enterprise-oriented C# code feels cluttered and way too messy for my liking. I just have to find a way to bend C# to my will and everything will get better. I'm most of the way to doing so, so that works. I used to do C# at my job before last and I had it working under my iron fist, but it has been a number of years, so... Need to relearn the ropes, so to speak. –  May 05 '12 at 03:10

2 Answers2

1

Use a MasterPage with a ContentPlaceholder. Then, on the content page, use a Literal and put the HTML in there.

See "The difference between literal and label".

John Saunders
  • 160,644
  • 26
  • 247
  • 397
zimdanen
  • 5,508
  • 7
  • 44
  • 89
  • I think this is along the lines of what I'd like to do. However, is there any way to specify a file in the asp:Literal? –  May 04 '12 at 20:03
  • Read the file and output the contents? :) – zimdanen May 04 '12 at 20:10
  • Similar question to what you just asked: http://stackoverflow.com/questions/887646/include-html-file-in-c-sharp-code – zimdanen May 04 '12 at 20:11
  • Got it working. It's a bit silly I had to write my own thing for this as you'd expect .NET would have better control over its workflow, but that's Microsoft for you I guess. =P –  May 04 '12 at 20:31
  • @user798080: very professional comment. Shows lack of objectivity as well as lack of perspective: ASP.NET has been in use for a decade, and most people don't have a problem with how master pages work. – John Saunders May 04 '12 at 21:20
  • Coming from someone who works at Microsoft commenting on ASP.NET, I really do see your point in terms of objectivity. I know people rarely promote the products they work on as better than the alternatives. But this is besides the point - regardless of how long ASP.NET has been in use (VB Classic has been 'in use' longer. Guess we should all use that), it isn't really how I enjoy coding. –  May 04 '12 at 21:24
1

Try this:

  1. Assuming you have data holding some where in memory like datatable or dataset or collection.

  2. To display a html text on the page we generally use <asp:Literal/>. So place a asp literal control on the page. It works in between <body> </body> tag.

  3. Now render the text to the control using its Text property. e.g.

    ltl.text = dt.rows[0]["htmlcol"].tostring();

To dynamically add text inside <head> </head>, u need HtmlMeta,Page etc class.