Include HTML file in C # code

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

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

      

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

For instance:

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

      

But it creates an error and the menu on the left does not load. Is there a way to manage this from C # code.

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

<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 divs from C # code.

I am using VS 2003

+1


a source to share


5 answers


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

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 custom controls, but it should do the job and is relatively neat and tidy.

+1


a source


You can create a set of LeftFrame controls that you could replace in code. This way you can dynamically choose which control you load at runtime.

For instance.,.

ILeftFrame {}

LeftFrame : ILeftFrame {}
LeftFrameOthers : ILeftFrame {}

      

Then self control



LeftFrameControl : System.Web.Ui.UserControl {}

      

Then in your page you have

<myns:LeftFrameControl runat="Server">.

      

This control will have a property that allows you to specify which ILeftFrame you are using, load it, take the appropriate steps, and you will be fine in the world of object orientation.

+1


a source


Some dirty workaround might be as follows:

<div id="ALeftMenu">
<%

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

%>
</div>

      

Not perfect, but fast, simple and will do the trick.

+1


a source


ASP.NET Literal Control would seem ideal for this purpose. The code will be almost identical to what you posted:

Markup

<div id="divOwnLeftFrame" runat="server" style="display: block;">
    <asp:Literal id="menuLiteral" runat="server" Mode="PassThrough" />
</div>

      

Code (simplified)

menuLiteral.Text = String.Format("<!-- #include file="LeftFrame{0}.htm"-->", 
    (arg == 1) ? "" : "Others";

      

Let me know if this does the job.

0


a source


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:

if ( arg == 1 )
{
    phContent1.Visible = true;
}
else
{
    phContent2.Visible = true;
}

      

0


a source







All Articles