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;
}
Related
I'm attempting to understand the membership class and how it works in asp.net, however when looking at the Membership.cs file, I see the following code,
public static MembershipProvider Provider {
get {
Initialize();
if (s_Provider == null) {
throw new InvalidOperationException(SR.GetString(SR.Def_membership_provider_not_found));
}
return s_Provider;
}
}
I don't see a local method, and the class doesn't seem to inherit from any source that would provide code for it. How is it that the Initialize() method is able to give value to the s_Provider variable and where does its code live?
The class is a partial class. There is another file in the assembly that has the same full name, and is also marked as partial, and that contains a definition for that method.
You can use the Visual Studio "Go to Definition" feature in the context menu of Initialize to open up that file and navigate to the definition of that method.
I have a web site(not web app) which has a default.aspx and default.aspx.cs.
And it has an App_Code folder with class A in it.
So default.aspx.cs. Looks like:
namespace test1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
And class A:
namespace test1
{
public class A
{
_Default mDefaultRef;
public pageLogicIndividual(_Default defaultRef)
{
mDefaultRef = defaultRef;
}
}
}
I can use A in _Default. eg. if I type test1., while working in class _Default, the IntelliSense will show both classes.
But if I type test1., while working in class A, the IntelliSense will only show A.
Why can't I use _Default in A?
Error : the type or namespace name does not exist in the namespace
(are you missing an assembly reference )
Edit:
I'll try to clarify.
While working in the .cs file where class _Default resides I can type test1., which is the namespace, and the intellisense will show me class _Default and Class A.
But if I'm working in the .cs file where class A resides and type test1., which is the namespace, then the intellisense will only show me class A.
I have had this challenge in the past.
Open up the App_Code folder node in Visual studio.
Right click on the concerned class, then click properties
In the properties pane, change Build Action to Compile.
It should work fine now.
Your problem is with your misleading namespace that you've added yourself after the new file has been created because ASP.NET Web Sites do not have namespace. The namespaces are available in Web Applications projects. i.e. after a new WebSite is created, namespace doesn't added to the files.
So you don't need to place your class A inside the test1 namespace because you can use A in default.aspx.cs even without namespace but you can not access other WebForm page classes from a Webform page or App_Code classes.
BTW if you want to use the necessary and reusable methods within a class of the Default Web Form, you can move those methods out to A class which is under App_Code and as I said already you can use it within all the Web Form CodeFiles without providing namespace for it.
In a nutshell, you cannot access page classes from App_code classes.
This restriction comes from website project compilation model. Special App_code folder contains shared classes (and possibly other resources) which are available to pages (and to each other). During compilation App_code is compiled first in a single assembly. This is the key point. Pages, controls and MasterPages are compiled in another assembly(ies) which may have references to the App_code assembly (but not vise versa). So this is one-way road.
No namespace declaration should circumvent this behavior.
Pages can see each other in ASP namespace (ASP.default_aspx) but pages usually don't have public properties / methods (user controls .ascx usually have).
Read better explanation on MSDN Code-Behind Pages
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
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 ?
I'm accessing Masterpage properties from a regular ASP.NET C# page by doing the following:
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
That works fine in a page's code-behind, but when I try to move it to a function in my base class file like this:
public class MyBaseClass : System.Web.UI.Page
{
public void portalFuncs()
{
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
}
}
... I get the following error message:
Compiler Error Message: CS0246: The type or namespace name 'SecondMasterPage' could not be found (are you missing a using directive or an assembly reference?)
My base class file is in my App_Code directory and other functions in there work without errors. Why won't this function work? Also if a function like this particular one won't work in my base class file, where could I put it so it would be available to be called from any page?
Unless I'm missing something, you should just be able to go into the codebehind for your SecondMasterPage file, and check the namespace of it. Perhaps the namespace is incorrect or something different than you want.
In your base class, use a using my.masterpage.namespace; statement to get it to work.