Forum Moderators: open

Message Too Old, No Replies

Include file puzzle

Asp.net

         

garann

7:36 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



I have a file I need to include in my ASP.NET page with a few objects that have been set with
runat="server"
. But I need the name of the file to include to be determined by ASP.NET as well.

So, I need something like this:

<!-- #include virtual="<%= "myPage.inc" %>" -->

Where "myPage.inc" contains:

<div id="myContent" runat="server"></div>

So, yeah, I'm trying to get ASP.NET to parse my code twice. I know this is an old puzzle, but I thought there was a tricky way to get around it in classic ASP, and thought there might be something similar in ASP.NET, maybe an actual include control? Creative solutions, as long as they don't include the words "user control", are most welcome!

Thanks for any help!
Garann

TheNige

8:04 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



Please do some research on ASP.Net User Controls. They will do what you need much more elegantly.

garann

8:20 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



Thanks, but I know all about user controls. That's why I specified that I didn't want to use them - the problem isn't nearly as simple as the example above, and user controls to try and answer my need would be anything but elegant.

I'm hoping for something more low-level, maybe writing to the working version of the .aspx page once the parser has it but before it actually does the parsing. Any help?

TheNige

9:26 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



Sorry about that.

What exactly are you trying to accomplish here? Dynamically including HTML on the pages?

You can still use user controls and the "LoadControl" method to add the control to the page at runtime. You can add the control you need based on what ever logic you are using.

Maybe do a search on dynamically adding user controls to the page.

garann

9:42 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



For anyone coming up against this same issue: This [4guysfromrolla.com] appears to be the old school method I was thinking of. I was able to do the same thing in .NET like so:

...<div class="content">
<% Server.Execute("/users/"+Session["user"].ToString()+".aspx"); %>
<div class="leftBlock" id="groupMng" runat="server">...

True, it requires tacky inline scripting, but it allows for dynamic includes where includes are preferable to user controls, database calls, iframes, or the other options.

ADDED: TheNige, thanks again for your comments, but I need to create an include file for each of my users. Dynamically creating and modifying user controls for each user sounds pretty messy. That's why I was looking at includes. If that didn't work, I would have gone to a database next.

TheNige

11:26 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



So are you creating new ASPX pages for all of your users?

Could you give a more high-level explanation of what you are trying to do. I know that you have a solution already but I'm just curious about these include files that you need.

Thanks

mrMister

11:37 pm on Jun 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL, I've seen some crazy programming in my time, but this one looks like its got bells on.

You're using some external application to generate the code for an aspx file which is in turn loaded up as an include, am I right?

Server.Execute still works. Read it up in the documentation (System.Web.HttpServerUtility.Execute)

You can do exactly the same trick with user controls though, it's just it's a lot more efficient. You really should tell TheNige what exactly you're up to, he might be able to educate you, saving you time, memory and processor cycles in the process.

[edited by: mrMister at 11:56 pm (utc) on June 8, 2006]

garann

11:52 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



TheNige, the short story is that users come to a tool to lay out their pages and place a variety of content. Their layout is preserved as a .aspx file which is really just an include with some inline scripting to get around the additional headache that would result from trying to pull their content using a code-behind. Then they go and view their page and they see their include file populated.

mrMister, the includes are generated by the same application that consumes them. And I know Server.Execute works - that's what I'm using...?

mrMister

12:02 am on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Include files work exactly the same as they did before ASP.Net came along.

They're a feature of IIS, they work independently from, and have absolutely nothing to do with ASP.

Tou say you want an "include" control. That's pretty much what Controls.Add(LoadControl("yourcontrol.ascx")) does.

garann

12:16 am on Jun 9, 2006 (gmt 0)

10+ Year Member



mrMister, I know includes have nothing to do with ASP. They're a feature of Internet Explorer, right? ;)

Seriously, I think maybe you misunderstood something I said, and I'm sorry if it offended you. I promise, know what SSI is used for and how it works and the only reason I'd prefer to use it instead of user controls is that I have to produce one file for each user and it seems much easier to do that with dirty, old school inline script than a compiled user control. If I'm missing a way to avoid producing a user control per user, I'd love to hear it, but at this point I think my second best option is a database, not user controls.

mrMister

12:26 am on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the only reason I'd prefer to use it instead of user controls is that I have to produce one file for each user

Why do you have to produce one file per user?

it seems much easier to do that with dirty, old school inline script than a compiled user control

All code is the .Net framework is compiled. The only difference between the different types is the point at which they are compiled. ASPX files and User Controls are compiled at runtime, Codebehind and Server Controls are pre-compiled.

Are you mistaking User Controls for Server Controls perhaps?

It really would be a lot easier if you were to paste one of your include files here so we know what you're trying to do?

garann

12:36 am on Jun 9, 2006 (gmt 0)

10+ Year Member



I'm thinking I'd need one file per user because each user's layout and content is individual to them. This is what the files look like. Each of the DIVs, except the "content" one, is optional and can be in any order:

<% mother.DataBlock.ConnString = connString; %><div class="content"><div class="rightSidebar"><div id="groups" runat="server"><h2>Your Groups</h2><%= Data.Groups(user) %></div>
</div><div class="intro"><p>Grain of salt.</p>
</div><div class="split"><div class="splitSide"><div id="news" runat="server"><h2>News</h2><%= Data.News("") %></div>
</div><div class="splitText"><div id="tasks" runat="server"><h2>Your Tasks</h2><%= Data.Tasks(user,true) %></div>
</div></div></div>

Messy, but I don't have to register a user control or deal with a code behind, so I thought it was the best I could do...

mrMister

12:43 am on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So why not create a user control for each DIV? How many different ones are there?

garann

12:50 am on Jun 9, 2006 (gmt 0)

10+ Year Member



The order and presence of all those internal DIVs changes. So users can have different internal DIVs and they can be positioned differently. There are six list DIVs that pull data from a database, then infinite user content DIVs that can contain text, links, images, or whatever.

<% mother.DataBlock.ConnString = connString; %><div class="content"><div class="leftSidebar"><div id="groups" runat="server"><h2>Your Groups</h2><%= Data.Groups(user) %></div>
</div><div class="intro"><p>Apples, peaches, pumpkin pie.</p>
</div><div class="assignments"></div><div class="wideText"><div id="tasks" runat="server"><h2>Your Tasks</h2><%= Data.Tasks(user,true) %></div><div id="news" runat="server"><h2>News</h2><%= Data.News("") %></div>
</div></div>

mrMister

12:58 am on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The way I'd do it is to store an XML file for each user detailing the sections, the order and the dynamic contents

user-john.xml


<xml>
<groups>apples,peaches,pumpkin pie</groups>
<news/>
<tasks/>
<other></other>
</xml>

user-jane.xml


<xml>
<tasks/>
<groups>grain of salt</groups>
<news/>
</xml>

(you can use CDATA sections for the user-generated HTML)

Then transform it using the XSLT objects or parse it using the XML DOM objects

By doing this way, the server will run a lot quicker because its not having to recompile the aspx file each time the user changes their preferences. It'll also use a lot less memory.

I don't see how using some kind of dynamic include would be any more elegant than dynamically writing a user control or using server.execute.

TheNige

4:04 am on Jun 9, 2006 (gmt 0)

10+ Year Member



garann, I think I know where you are coming from now:

1) There is some kind of user page generator, that allows the user to select some kind of pre-defined objects, which are rendered as divs. They can set the order of these divs and can also place custom code in them.

2) This generator then saves this user specific page to the file system as user-1.aspx

3) When the user visits the site and a certain "template loader" page, the loader will check which user it is and then load their particular template that was pre-generated

Is this correct? Also when how would something like "<%= Data.Tasks(user,true) %>" get put into the template? Does you template generator do this?

There are many ways to skin a cat. If your template files indeed need to have objects with runat=server or "<%= Data.Tasks(user,true) %>" in them, then I think that your options are to do what you are doing with the server.execute, or what I would probably do is to generate user controls instead of aspx file and then add them to the page using the Controls.LoadControl method

If your user templates are just HTML with no server side controls then I'd use mrMister's XML version or store the HTML in a database and load it up when needed.

Ocean10000

3:08 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here is some code I wiped up from Memory that might help you out a little. I haven't had a chance to check it for errors. This should take a user control/aspx page and load it as a control in the current page.

void AddDyanmicControl(string ControlPath)
{
//Just Maping the path to a true file path to allow
//us to check if the file exists or not, Saveing a
//error throw
string FilePath =this.Request.MapPath(ControlPath);
if (System.IO.File.Exists(FilePath)==true)
{
//Loads the Control (at least should)
System.Web.UI.Control UserControl = LoadControl(ControlPath);
if (UserControl!=null)
{
this.Controls.Add(UserControl);
}
}
}

garann

5:06 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



mrMister, thanks for the XML. That's a better idea than a database, since it maintains the structure of my HTML so nicely. I hadn't even considered XML. Duh. :)

TheNige, that's it exactly, and the scriplets are added by the template generator.

Since I'm overwhelmingly being told that generating a user control is as easy as generating the aspx file, I'll look into it. If anyone has a link to a tutorial on that topic, it would be much appreciated. Thanks to all of you for sharing your thoughts!