The name 'Global' does not exist in the current context
I'm getting the above error when trying to reference a property I've created in Global.asax:
public static String ThemeColor
{ get; set; }
from the C# on the aspx page (outputting some javascript):
alert("<%=Global.ThemeColor %>");
Any ideas why?
Several options:
The class name isn't Global, Maybe you changed it?
You are missing the using of the namespace
You really should not use the Global.asax to handle the theme color.
css seems to be a more appropriate place for it...
Why don't you make a separate class of the theme-color and at the application-start event in global.asax set the themecolor to something.
If you're putting these sorts of values in Global.asax you need a doctor.
Create a class called "GlobalSiteValues" or whatever. Make sure the namespace it lives in is either the same as the aspx page, or registered in web.config (or non-existent or use the full name).
Then this will work (once you have set the value, obviously)
public class GlobalSiteValues
{
public static string MyString{ get;set }
public static int MyInt{ get;set; }
}
... and in the aspx page (in script block)...
var abc = "<%= GlobalSiteValues.MyString %>";
alert(abc);
Or why not set up a "context class" for your site. Like HttpContext.Current ?
Related
My singleton object does not keep its state when loading a new page in blazor.
I have this interface:
public interface IPreLaunchSession
{
bool IsPreLaunchAuthenticated { get; set; }
}
The class for this interface
public class PreLaunchSession : IPreLaunchSession
{
public bool IsPreLaunchAuthenticated { get; set; }
public PreLaunchSession()
{
}
}
I register it as a singleton
builder.Services.AddSingleton<IPreLaunchSession,PreLaunchSession>();
Then inject it into the page
#inject Services.IPreLaunchSession PreLaunchSession
In these pages where its injected, I test for the bool variable as well as set it.
In another section of code, I redirect using NavigationManager. Something like this:
Navigation.NavigateTo("/MyOtherPath");
This seems to work for a while but then a <NavLink/> is used to go to another page and as soon as this happens then the PreLaunchSession object seems to be recreated and the bool variable in it is reset to default (which is false)
Any help here would be great in understanding why a singleton object gets recreated with navigation happening.
Have a look at how to persist value in a singleton state container in blazor web assembly on page reload
When refreshing the app (F5 etc) the app is essentially turned off and turned on again.
This would cause the whole app to restart and all memory of persisted singleton objects would be lost.
I ended up using localstorage to persist between full refreshes or tab changes.
I work on an application that has a particular design flaw, where some content should be accessible in multiple areas of the application depending on what role(s) a user may have. In the past this has led developers to copy and paste pages into different places with slightly different names, which ultimately leads to bugs when things get out of sync and also just feels yucky.
I want to try to abstract out the (99%) common code into its own class, however the code behind each page references various controls on the page. I can't seem to figure out how to reference the UserControl classes.
For example a page might have
<%# Register Src="~/uc/ResearchParamBar.ascx" TagName="ResearchParamBar" TagPrefix="uc1" %>
...
<uc1:ResearchParamBar ID="ResearchParamBar1" runat="server"/>
And the code behind would have
private void InitializeComponent()
{
// wire the event to the destination user control handler
ResearchParamBar1.SourcePageID = ObjectID;
...
}
The user control is defined like
public partial class ResearchParamBar : BaseUserControl
I was trying to pass a ResearchParamBar in the constructor to the abstract class, but it's not clear to me how to reference that class.
The type or namespace name 'ResearchParamBar' could not be found (are you missing a using directive or an assembly reference?)
public abstract class SummaryBase : WRBasePage
{
public abstract string ObjectID
{
get;
}
//Cannot figure out how to reference ResearchParamBar
private ResearchParamBar ResearchParamBar1;
public SummaryBase(ResearchParamBar researchParamBar)
{
ResearchParamBar1 = researchParamBar;
}
I have a simple Web Service with one method. (This method calling from another application)
In this method I need to call ASPX page of sided site - not html code, but method of this page.
Can anyone ask, how can I do this?
It is generally a good idea to have shared/reusable code stored separately from both the web service and the ASPX page so that any number of dependents may access it.
But to answer your question specifically:
I'll assume that the web service class is able to reference the ASPX page class (.aspx.cs or aspx.vb).
The example below shows two ways of accomplishing what you're asking. You can either instantiate your Page class and use the method just as you would any other normal class, or if the method is static, use it as is without instantiating your Page class.
Example:
public class MyAspxPage : Page
{
private Object _myObj = new object();
public object GetObject()
{
return _myObj;
}
public static object GetAnObject()
{
return new object();
}
}
public class MyWebService : WebService
{
public void MyWebServiceMethod1()
{
MyAspxPage page = new MyAspxPage();
object result = page.GetObject();
}
public void MyWebServiceMethod2()
{
object result = MyAspxPage.GetAnObject();
}
}
you don't want to be sharing code in aspx pages. You should create a project of type class library in your solution. Put your code that you want to be common code called from aspx pages and web services into the class project in a class.
I have a calculation class file where I am doing something. I made an object x of the calculation class in .cs file of aspx and called it in aspx using server tag.
Now I am getting hits for that page.
Is that page is going to use the a single object for that class or for every hit will it create a new object?
Is this a good practice of doing coding?
calculation.cs
using system;
public calculation()
{
//some decelerations
}
public string getProduct()
{
return (A*B*C).toString();
}
page.aspx.cs
public calculation cal = new calculation();
page.aspx
<%# cal.getProduct()%>
It is completely dependent upon how you have instantiated the object.
If it's a singleton with a static variable scope then it will be created once for each app domain. If the app recycles then it will be created again. ASP .NET Singleton
If it's a singleton that's stored in the HttpContext, then it will be created once per request. See example here of how to do this: http://dotnetslackers.com/community/blogs/simoneb/archive/2006/08/21/The-ASP.NET-Singleton_2D00_per_2D00_Request-pattern.aspx
If it's a regular variable, then it will be created once every time that page code is called, even it is on the same request.
I'm using a public LoginContext class to manage user logins in my web app.
Unfortunately, even though I have the LoginContext class declared publicly, my partial class Login at Login.aspx.cs can't seem to access it.
My code is as follows:
// ~/App_Code/LoginContext.cs
namespace stman
{
public class LoginContext
{
}
}
// ~/Login.aspx.cs
namespace stman
{
public partial class Login : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
LoginContext log = new LoginContext(); // error is here
}
}
}
The error that comes up on the line where I instantiate LoginContext reads as follows:
The type or namespace name 'LoginContext' could not be found (are you missing a using directive or an assembly reference?)
When I try to generate a new class for LoginContext, it goes into the web app's root folder where it can no longer access the public Database class that I need in LoginContext.
I have no idea what's causing all of these problems, but based on what I've learned over the last 18 months doing this professionally, they shouldn't exist right now...
Can anyone help clear things up here? Specifically I'd like to know:
What I'm doing wrong
Why it's wrong
Who can I fix it?
Thanks in advance!
EDIT
I've had a look and it seems neither the Database class in ~/App_Code/Database.cs or the LoginContext class in ~/App_Code/LoginContext.cs are accessible to the page - or any page in the website.
In LoginContext.cs properties, marked it as BuildAction = Compile.
You can achieve this behaviour when these classes are located in different projects.
If this is true then you should use full path to the class starting from the project name
ProjectName.stman.LoginContext
try using constructor
namespace stman
{
// Database is a class that handles the sql queries and such
public class LoginContext : Database
{
public LoginContext () : base("Name=LoginContext")
{
}
}
}
From what I read from the App_Code behavior may be different in web site projects and web application. I wonder what kind of project you're working on? One possible solution would be to make this project in web application project, this Link can help you to make this project:
Source