a small questions on web part, sharepoint - c#

I created a empty web part, then created a usercontrol, in UserControl.ascx.cs, I wrote like that:
namespace tasks_email.ControlTemplates.tasks_email
{
public partial class UserControl1 : UserControl
{
public tasks_email WebPart { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
then it said:"tasks_email.Controltemplates.tasks_email" is a 'namesspace' but is used like a 'type'
Can any body tell me where the problem is and give me some suggestion?

If you want to create a webpart with a user control inside of it you can just use the "Visual web part" template in visual studio. It will create a webpart, and a user control, and add the user control to the webpart all for you, and even package the whole thing into one collapsible component in solution explorer. It's quite convenient. Doing all of that work yourself (for every new webpart) is just a pain.

namespace tasks_email.ControlTemplates.tasks_email
Here you declare tasks_email as a namespace.
public tasks_email WebPart { get; set; }
Here you declare a member called WebPart, with a type tasks_email. That's not a type - you've already said it's a namespace. I can't guess what you really meant.

Did you try "namespace tasks_email.ControlTemplates"? Because the way you do it, you use tasks_email like a type indeed ; and not as a namespace

Related

How to reference UserControl?

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;
}

Unable to access class from app_code

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

Inaccessible public classes

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

Data manipulation in PlugIn-Concept

I have a strategic question for my C#-project. I want to implement a plugin-concept and now struggling with the best way for the plugins to manipulate the data of the main project.
At first I created a "Master" PlugIn Project that defines the Interface for the plugins and an attribute to identify a class as a pluginclass when I load it in my main project.
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class GraphViewerPlugInAttribute : Attribute
{
}
public interface IGraphViewerPlugIn
{
Panel GetRightSidePanel();
string Name();
}
This project is then referenced in the main project and the pluginclasses. The pluginclass implements the interface ...
[GraphViewerPlugIn]
public class myTestPlugIn : IGraphViewerPlugIn
{
public Panel GetRightSidePanel()
{
Panel myPanel = new Panel();
myPanel.BackColor = System.Drawing.Color.Red;
return myPanel;
}
public string Name()
{
return "TestPlugIn";
}
}
... and the main project loads all plugins that are stored in a certain directory.
This works quite well so far, I can get the special Panel from the PlugIn, but now I want to manipulate data of the main project from within the PlugIn. What would be the best way to do that?
I have some kind of data-container-class that is defined in the "Master Plugin Project" in mind. The plugin puts the data into that container and the main project will be recognized by an event that the data has changed and can now look what data has changed and then apply the changes.
Is that the right direction, what do I have to consider and which techniques (i.e. events, static classes ...) shall I use to implement that?
One approach is to add a configuration button which activates a method on the interface designed for configuration.
This method can make use of any code it would like to configure the plug in, including making it's own calls to windows forms.
If you are using windows forms or winfx, and not a custom UI, this is the ideal method because it removes all logic related to the plugin from the application itself, except for notifcation that a configure window was requested.
public interface IGraphViewerPlugIn
{
Panel GetRightSidePanel();
string Name();
void ShowConfigurationDialog();
}
Of course, you then implement ShowConfigurationDialog as such:
public void ShowConfigurationDialog()
{
Form form = new MyConfigurationDialog();
form.ShowDialog();
}
where MyConfigurationDialog is your designer created form for configuring the plugin.

Accessing Page Class From Another Page Class

How do I access a page class from another class. For example I have:
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
}
Why can I not access it from another class in App_Code folder?
public class MyClass
{
public MyClass() {}
public void DoSomething(object o)
{
// The following line won't compile.
MyPage page = o as MyPage;
}
}
I just figured it out (thanks to Fujiy) that for some reason this is the case with website project, but is not a problem with web application project in VS. If anyone has any clues as to why, please share your thoughts. Thank you :)
I see nothing wrong with your code as posted, most likely you've got a namespace problem.
edit: gah, just noticed that you mentioned this was a website project. It's been a while since I deigned to start one of those :) but I believe this stems from the fact that App_Code is run-time compiled. It would take a better man than me to explain why that creates the problem, but long story short I'd just avoid website projects in general.
You can do this, afaik:
System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;
Be careful where you call that though, because obviously at different points in your code the page won't be available. For example, Application_Start in global.asax.

Categories