1

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"-->

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

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.

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>

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

I am using VS 2003

5 Answers 5

2

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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" />

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.

Comments

1

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.

Comments

0

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

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.

11 Comments

But there isn't a peroperty of InnerHTML for literal control. I am using visual studio 2003.
@phoenix: Sorry, forgot to change that in the code. You want the Text property - don't let the name fool you, it's capable of rendering any markup.
Literal has no wrapper tags, just whatever you put there.
@Greg: Yes, exactly. Point being?
While this will work as requested it is somewhat quick and dirty. The response by D.Patrick sounds more complex than it really is and it will help to break the cycle of HTML includes in your markup/code
|
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.