Forum Moderators: open
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
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?
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.
...<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.
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]
mrMister, the includes are generated by the same application that consumes them. And I know Server.Execute works - that's what I'm using...?
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.
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?
<% 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...
<% 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>
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.
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.
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);
}
}
}
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!