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

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
 
T

Teemu Keiski

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
 
M

MattC

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
 
M

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

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
 
M

Mark Rae

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top