在C#代码中包含HTML文件 [英] Include HTML file in C# code

查看:53
本文介绍了在C#代码中包含HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中有一个左菜单.左侧菜单是HTML文件[LeftFrame.htm],并使用以下代码包含在aspx页面中.

I have a page that has a left menu in it. This left menu is an HTML file [LeftFrame.htm] and is included in the aspx page using the following code.

<!-- #include file="LeftFrame.htm"-->

现在,我需要将文件名从C#代码更改为LeftFrameOthers.htm.

Now I need to change the file name to LeftFrameOthers.htm from C# code.

例如:

if ( arg == 1 )
{
    divMenu.InnerHTML = "<!-- #include file="LeftFrame.htm"-->";
}
else
{
     divMenu.InnerHTML = "<!-- #include file="LeftFrameOthers.htm"-->";
}

但是它会产生错误,并且不会加载左侧菜单.有没有一种方法可以通过C#代码进行管理.

But it creates an error and left menu is not loaded. Is there a way to manage this from C# code.

我不想为此使用两个div..

I don't want to use two divs for this purpose like..

<div id="divOwnLeftFrame" runat="server" style="DISPLAY: block">
           <!-- #include file="LeftFrame.htm"--></div><div id="divOthersLeftFrame" runat="server" style="DISPLAY: block">
          <!-- #include file="LeftProfileFrame.htm"-->
</div>

并从C#代码更改div的显示属性.

and change the display property of the divs from C# code.

我正在使用VS 2003

推荐答案

ASP.NET中包含文件的使用通常已被功能更强大的用户控件所取代.这是使用包含文件的另一种选择:

The use of include files in ASP.NET has generally been superseded by user controls which are much more powerful. Here's another alternative to using include files:

ASPX:

<asp:Literal ID="FrameLiteral" runat="server" />

后面的代码:

private string GetFrameContent(int arg)
{
    string filename = (arg == 1) ? "LeftFrame.htm" : "LeftFrameOthers.htm";
    string path = Server.MapPath("./" + filename);
    string content = System.IO.File.ReadAllText(path);

    return content;
}

// Populate Literal
FrameLiteral.Text = GetFrameContent(arg);

不如使用用户控件那样优雅,但是它应该可以完成工作,并且相对整洁.

Not as elegant as doing it with user controls, but it should do the job and is relatively neat and tidy.

这篇关于在C#代码中包含HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆