Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

...is not declared. It may be inaccessible due to its protection level (tryiig to set a variable in code behind)

Posted on 2011-04-21
15
Medium Priority
?
2,259 Views
Last Modified: 2012-05-11
I am trying to update my site.master to use a variable for where it points to in order to get the css file:
<link href=""<%= AppsRootBaseUrl %>"/includes/app_style.css" rel="stylesheet" type="text/css" />

Open in new window


The code behind Page_load has this:
Dim AppsRootBaseUrl As String = "/AppsRoot/includes"

When I try to run, I get this:

InnerException {"C:<path in here>\Site.Master(11): error BC30451: 'AppsRootBaseUrl' is not declared. It may be inaccessible due to its protection level.

Can someone please tell me what I am doing wrong?  (the purpose for this override is to allow me to point to different locations for the css file based on which enviornment the web application is running in.  The setting will eventually originate from the web.config instead of having it hardcoded the way I have it above)
0
Comment
Question by:ToolTimeGang
  • 8
  • 7
15 Comments
 
LVL 31

Expert Comment

by:MlandaT
ID: 35443413
change:
Dim AppsRootBaseUrl As String = "/AppsRoot/includes"

Open in new window

to
Public AppsRootBaseUrl As String = "/AppsRoot/includes"

Open in new window

0
 

Author Comment

by:ToolTimeGang
ID: 35443661
Thank you for your prompt response!
I feel silly that I didn't think of using public instead of dim.  So I changed it to public.
However, its not working.  Remember that the code lives in Page_Load, so it does not allow a Public variable.

so I tried putting it here instead:
Public Class Site
    Inherits System.Web.UI.MasterPage
    Public AppsRootBaseUrl As String = "/AppsRoot/includes"

Then this line....
<link href=""<%# AppsRootBaseUrl %>"/app_style.css" rel="stylesheet" type="text/css" />

...renders to this:
<link href="""/app_style.css" rel="stylesheet" type="text/css" />

So I'm still missing something somewhere.
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 35443767
Ah yes! Indeed.... you put the variable in the right place. It has to be global to the class.

The rendering... change your code to:

<link href='<%# AppsRootBaseUrl & "/app_style.css" %>' rel="stylesheet" type="text/css" />

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ToolTimeGang
ID: 35443861
Ok.  I made that change (thx).  Now it is rendering to this:

<link rel="stylesheet" type="text/css" />
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 35443995
try adding a runat="server" on the <head> tag.
0
 
LVL 31

Accepted Solution

by:
MlandaT earned 2000 total points
ID: 35444011
0
 

Author Comment

by:ToolTimeGang
ID: 35444015
It is there:
<head id="Head1" runat="server">
    <title><asp:Literal ID="litPageTitle" runat="server" /></title>
    <link href='<%# AppsRootBaseUrl & "/app_style.css" %>' rel="stylesheet" type="text/css" />
0
 

Author Comment

by:ToolTimeGang
ID: 35444035
Neato.  Just saw your last post.  I'll take a crack at it and let you know what comes of it tomorrow.
Thanks a bunch!
0
 

Author Comment

by:ToolTimeGang
ID: 35448163
I was able to get both examples working just fine.
However, when I use a dynamic method for my css location, the design view of the web pages lose their look/feell.

I continue to run into the same wall.   It seems the css file must be kept as part of the sln.
I was hoping to point to a css file outside of the sln so I could share it with other web apps (instead of each web app having the same css file in it).

Bummer.
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 35448301
Interesting... been thinking how we can still try to get your previous syntax to work....

I came accross someone that said that they had this:
<link rel="stylesheet" href="<%=Global_asax.GetApplicationPath()%>styles/main.css" type="text/css">

Open in new window

and it was working for them...  http://bytes.com/topic/c-sharp/answers/698034-c-cant-dynamically-set-url-css-file-but-can-vb-net This is in fact quite similar to what you had before... only slightly different syntax.

And another alternate method here... http://www.aspnettutorials.com/tutorials/themes/usingcss-csharp.aspx
Notice how they give the <link... an id in the declaration
<link href="dark.css" rel="stylesheet" type="text/css" id="stylesheet" />

Open in new window

and then just access it in server side code like so:
protected void SwitchStylesheets(object sender, EventArgs e)
{
if (radioDark.Checked)
stylesheet.Href = "dark.css";
if (radioLight.Checked)
stylesheet.Href = "light.css";
}

Open in new window


Both approaches seem to deal with your objective to still use the CSS in design view. Worth a try... maybe?
0
 

Author Comment

by:ToolTimeGang
ID: 35448768
Absolutely worth a try!  Thanks a bunch!! I will let you know what comes of it today.
0
 

Author Comment

by:ToolTimeGang
ID: 35513019

This is the closest I can get for now.
I am going to include an id with the link for the css (html source).  This link will point to a css that is kept in the sln.
In the page_load, I will have code to override the css location.  This is where I will be able to point to a css file that can be shared by multiple applications.
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 35688223
I think that should work. Have you given it a try just yet?
0
 

Author Comment

by:ToolTimeGang
ID: 35689299
Yes.  It does the trick.  Not the most elegant, but it works.
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 35689336
Cool then! All the best with you application.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question