Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems Referencing files based on Application variable 1

Status
Not open for further replies.

jen89

Programmer
Aug 16, 2001
105
US
I am fairly new to .NET, although I've had about 1.5 years experience with Classic ASP. I am rather embarressed to be having this problem, and have already wasted more than 2 days trying to get it running.

I am using a Infragistics Menu component as the menu in my app. I need to use this in several levels of folders, so I thought that I would make it a Web User Control. My impression was that this would be like an include file with a control in it. I can get this to work for 1 level, but for some reason, I can't figure out how to properly use the virtual paths here. If I use / for root, it goes all the way back to instead of my application root. If I write out the whole path, I will have to make a seperate header file for each folder.

The part that has me truly baffled is that I cannot even seem to use the application variable that we have set up in our global.asa (Application("AppPath")). When I do this, I get the error:
Code:
Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
</code]

The code that triggers this is:
[code]
Line 124:    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Line 125:        Me.Controls.Add(Me.Page.LoadControl(&quot;../Includes/Header.ascx&quot;))

It is supposed to add the menu to the page.

I even tried using the application var <%=Application(&quot;App_Path&quot;)%> in a totally seperate html file and got the same error. This was a simple reference to an image folder, and was not included into the ascx file in any way at all. It simply won't allow me to use these tags at all in those two files.

Am I missing something really obvious? Could this have anything to do with my file names? (Header.ascx and Header.html?) Is there any other way at all to refer to the root of my project? I would really appreciate any help that anyone can provide, as I am at a standstill here. Thanks,

Jen
 
On my projects, I put an application setting in the web.config file that specifies the absolute root path of my project. You can then just refer to it any time you need it:

In Web.Config:
Code:
<appSettings>
  <add key=&quot;appRoot&quot; value=&quot;d:\inetput\[URL unfurl="true"]wwwroot\&quot;[/URL] />
</appSettings>
This goes right under the <configuration> tag of your web.config file.

Then, anywhere you need access to that var, just bring in the System.Configuration namespace:

Code:
using System.Configuration;

and then just ask:

Code:
ConfigurationSettings.AppSettings[&quot;appRoot&quot;];

Then, all this mapping of paths, etc... is rendered completely obsolete. And, when you publish your app, you simply change the .config file, which makes your app very portable.

hth! :)
paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
That sounds like just what I am looking for. I'm on my way out now, but I'll give it a try in the morning. Thanks so much!
 
How can I do this in an html file? is it possible w/out using <% %>?
 
OK, I don't think I really understand how I need to do this. I believe that I edited the web.config file correctly. I need to be able to reference the value in either an ascx or html file to point to the correct folder. I tried using <%=ConfigurationSettings.AppSettings[&quot;appRoot&quot;]%> and <%=System.Configuration.ConfigurationSettings.AppSettings[&quot;appRoot&quot;]%> to get the value out of the web.config file with no luck. I got an error:

Code:
BC30203: Identifier expected.

I didn't know where to use the Using System.Configuration part. Could you give me a little more information on where I might be going wrong? I am afraid that even if I get this working successfully, I will get the same error about the control containing server tags <% %>.
 
OK, last reply. I was thinking too Classic ASP. I was trying to set the fileURL property in HTML the old fashioned way, by writing
Code:
fileURL=&quot;<%ConfigurationSettings.appSettings[&quot;AppRoot&quot;]%>/menus/header.ascx&quot;

I got it to work by using the code behind page (imagine that!) to set it this way:
Code:
UltraWebMenu2.FileUrl = ConfigurationSettings.appSettings[&quot;AppRoot&quot;] & &quot;/menus/header.ascx&quot;

For anyone who reads this thread later, if you are using VB, use
Code:
imports System.Configuration
instead of
Code:
Using System.Configuration;
I assume that is C#. Thanks very much for your great advice, Paul!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top