asp:Literal & <!--includes-->

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

VS.NET 2003 on WinXPPro + all latest service packs etc.

I have a totally standard WebForm wherein I need to include one of several
HTML files according to various parameters etc.

I'm trying to use an <asp:Literal /> control for this, and intend to modify
its Text property in the Page_Load event, but it's not working. The code I'm
using is as follows:

<table>
<tr>
<td>
<asp:Literal ID=litText Runat=server Text='<!--#include
file="history.htm"-->' />
</td>
</tr>
</table>

However, although the include appears to be correctly written out when I do
a View Source on the page, its contents are not displayed. If I hardcode the
include, it works perfectly, as follows:

<table>
<tr>
<td>
<!--#include file="history.htm"-->'
</td>
</tr>
</table>

Any assistance gratefully appreciated.

Best,

Mark Rae
 
Including cannot assign value to control property that way. Try it this way:

<asp:Literal ID=litText Runat=server >
<!--#include file="history.htm"-->
</asp:Literal>

E.g put the include part inside the Literal's tag
 
The contents of the string Literal is not processed by ASP.NET and so the
control is simply rendered to the page in its literal text form.

As the #include is a server side directive the string on its own is useless
once it reaches the client. Try reading the contents of history.htm and
then using the literal control to output that to the client.

MattC
 
Including cannot assign value to control property that way. Try it this way:

<asp:Literal ID=litText Runat=server >
<!--#include file="history.htm"-->
</asp:Literal>

E.g put the include part inside the Literal's tag

Thanks very much! That solves the problem if I hardcode the reference to the
include file. However, I still need to change it in the Page_Load event.
I've tried the following with no success:

protected Literal litText;

private void Page_Load(object sender, System.EventArgs e)
{
litText.Text = "<!--#include file=\"history.htm\"-->";
}

As before, that causes the include to appear in View Source, but doesn't
write it out to the browser. It's almost like I need an InnerHTML
property...

Regards,

Mark
 
Matt,
The contents of the string Literal is not processed by ASP.NET and so the
control is simply rendered to the page in its literal text form.

As the #include is a server side directive the string on its own is useless
once it reaches the client. Try reading the contents of history.htm and
then using the literal control to output that to the client.

Excellent advice! I did:

StreamReader objStreamReader = File.OpenText(strIncludeFile);
litText.Text = objStreamReader.ReadToEnd();
objStreamReader.Close();

where strIncludeFile is a fully-qualified filespec which I set in code, and
that worked perfectly.

Thanks a lot.

Mark
 
Back
Top