在 C# 代码中包含 HTML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/887646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include HTML file in C# code
提问by rahul
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.
我有一个页面,里面有一个左侧菜单。此左侧菜单是一个 HTML 文件 [LeftFrame.htm],并使用以下代码包含在 aspx 页面中。
<!-- #include file="LeftFrame.htm"-->
Now I need to change the file name to LeftFrameOthers.htm from C# code.
现在我需要将文件名从 C# 代码更改为 LeftFrameOthers.htm。
Eg:
例如:
if ( arg == 1 )
{
divMenu.InnerHTML = "<!-- #include file="LeftFrame.htm"-->";
}
else
{
divMenu.InnerHTML = "<!-- #include file="LeftFrameOthers.htm"-->";
}
But it creates an error and left menu is not loaded. Is there a way to manage this from C# code.
但它会产生一个错误,并且未加载左侧菜单。有没有办法从 C# 代码管理这个。
I don't want to use two divs for this purpose like..
我不想为此目的使用两个 div,例如..
<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>
and change the display property of the divs from C# code.
并从 C# 代码更改 div 的显示属性。
I am using VS 2003
我正在使用 VS 2003
采纳答案by Mun
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:
ASP.NET 中包含文件的使用通常已被更强大的用户控件所取代。这是使用包含文件的另一种选择:
ASPX:
ASPX:
<asp:Literal ID="FrameLiteral" runat="server" />
Code behind:
后面的代码:
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.
不像使用用户控件那样优雅,但它应该可以完成工作并且相对整洁。
回答by Noldorin
An ASP.NET Literal Controlwould seem ideal for this purpose. The code would be virtually identical to what you posted:
一个ASP.NET文字控制似乎非常适合这一目的。代码与您发布的内容几乎相同:
Markup
标记
<div id="divOwnLeftFrame" runat="server" style="display: block;">
<asp:Literal id="menuLiteral" runat="server" Mode="PassThrough" />
</div>
Code-behind(simplified)
代码隐藏(简化)
menuLiteral.Text = String.Format("<!-- #include file="LeftFrame{0}.htm"-->",
(arg == 1) ? "" : "Others";
Let me know if that does the job.
让我知道这是否有效。
回答by D. Patrick
You could create a set of LeftFrame user controls which you could replace in the codebehind. That way, you could dynamically chose which control you load at runtime.
您可以创建一组可以在代码隐藏中替换的 LeftFrame 用户控件。这样,您可以动态选择在运行时加载的控件。
For example . . .
例如 。. .
ILeftFrame {}
LeftFrame : ILeftFrame {}
LeftFrameOthers : ILeftFrame {}
Then, the control itself
然后,控件本身
LeftFrameControl : System.Web.Ui.UserControl {}
Then, in your page, you have a
然后,在您的页面中,您有一个
<myns:LeftFrameControl runat="Server">.
That control would have a property allowing you to specify which ILeftFrame you are using, would load it, would execute the appropriate behaviors, and all would be well in the world of object orientation.
该控件将有一个属性,允许您指定正在使用的 ILeftFrame,加载它,执行适当的行为,并且在面向对象的世界中一切都会很好。
回答by Pete Duncanson
A some what dirty way around this might be to do something like this:
解决这个问题的一些肮脏的方法可能是做这样的事情:
<div id="ALeftMenu">
<%
if ( arg == 1 )
{
%>
<!-- #include file="LeftFrame.htm"-->";
<%
}
else
{
%>
<!-- #include file="LeftFrameOthers.htm"-->
<%
}
%>
</div>
Not perfect but quick, simple and would do the trick.
不完美,但快速,简单,可以解决问题。
回答by Naeem Sarfraz
Another alternative could be...
另一种选择可能是......
Html:
网址:
<div style="DISPLAY: block">
<asp:placeholder id=="phContent1" runat="server" visible="false">
<!-- #include file="LeftFrame.htm"-->
</asp:placeholder>
<asp:placeholder id=="phContent2" runat="server" visible="false">
<!-- #include file="LeftFrameOthers.htm"-->
</asp:placeholder>
</div>
Your C# code:
你的 C# 代码:
if ( arg == 1 )
{
phContent1.Visible = true;
}
else
{
phContent2.Visible = true;
}