Instantiate a web user control in a class in app_code - c#

I am writing a class called Evaluatieform that works with web user controls, their events and eventually adds them to a panel so the web page only needs to instantiate the Evaluatieform class and call a method that returns the panel so it can show it on the website.
Now the problem is that I can't instantiate my user controls I have defined.
First error : The type or namespace name 'DomeinsCriteriums' could not be found(are you missing a using directive or an assembly reference?)
So the next thing I try is to drag the user controls needed into the app_code folder. This however, does not work because the compiler does not allow a web user control in that folder.
I read somewhere else to use
ASP.webusercontrol_ascx usercontrol = new ASP.DomeinsCriteriums_ascx();
However, this does not work aswell.
Can anybody give me a quick solution for this problem?

All you need to do to load a user control is this.
Control ItemX = (Control)Page.LoadControl("/controls/yourusercontrol.ascx");
However if you are getting a namespace error. Simply add the namespace to the top of your page.
using DomeinsCriteriums;

Related

Custom module with possibility to add modules into module to create an expander module in DNN 9.2

​I'm using DNN 9.2 and searching for a possibility to create an own module that will work like the Atlassian Confluence's Expander Macro where I could add additional content.
In my case I want to add other modules, which will be visible if the parent is expanded and hidden if the parent is collapsed.
I thought about to use a Pane control in my module to place several other modules into it. It is an approach that imitates the Evoq's Grid module functionality, but with the additional possibility to collapse and expand its content, because of no such (expander) module already exists.
I already tried to achieve it by adding a Pane control from DotNetNuke.UI.Skins namespace in Page_Load method and calling pane.ProcessPane(). As I can see in database a added module by moving it into our custom (expander) module is related to our pane which is located in our module.
Actually I have some problems by loading and rendering the page, because the module is located under and not in our custom module like it is referenced in database.
Following is my actual code:
In *.ascx file:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="View.ascx.cs" Inherits="Prototype.View" %>
<div ID="TestModuleDiv" runat="server"></div>
In *.ascx.cs (code behind file):
using System;
using System.Web.UI.HtmlControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.UI.Skins;
protected void Page_Load(object sender, EventArgs e)
{
var paneName = "MyTestPane" + ModuleContext.ModuleId;
var paneControl = new HtmlGenericControl("div");
paneControl.ID = paneName;
TestModuleDiv.Controls.Add(paneControl);
var paneId = paneControl.ClientID.Replace("dnn_", "");
PortalSettings.ActiveTab.Panes.Add(paneId);
var pane = new Pane(paneId, paneControl);
pane.ProcessPane();
}
Does somebody have some more information how I could achieve my wanted behavior?
The aim is to create an expander module that could contain several other modules, but we are not sure how we could build it in a best practice way.
Edit
Sorry, I missed the fact that I'm searching for an approach with minimized JS magic. Therefore I think it has to happen in code behind.
Furthermore the page must remember which "expander" module was collapsed and which expanded.
You might want to take a look at this open source project:
https://github.com/redtempo/dnnstuff.aggregator
The work already has been done.
Well we have reached an approach that will work like we have expected.
The whole adding/moving modules into the expander module works out of the box and automatically with DNN's own functionality side. We have nothing to implement additionally.
We created an own Panel control that inherits from HtmlContainerControl and has some logic to use the parent skin as container and furthermore the parent module to get the active Tab (page) from it. This will all happen in method OnInit.
Also in OnInit method the OnInitComplete methods is registered. In this method we are moving all (previously added) modules into the pane. Otherwise the expander module added modules will not be placed inside the expander, but under it.
After that we have to register our own pane control in active tab and have to call finally ProcessPane on the pane.
I hope it will help everybody who needs something similar.
I'm very sorry for posting no code, but is an implementation for a customer which I'm not allowed to provide to third persons.

Dynamically add .aspx page or Usercontrol inside another page

I want to prepend an aspx page with another page at runtime.
I know the better solution would be to use a usercontrol, But for that I would have to put all the elements of my aspx page in a user control & do that for several pages.
Isnt there a way to load an aspx page inside another page like a user control?
Also I would want to make all the fields readonly of the appended page. Is there a way I can do this?
If usercontrol is the only way, then I have a query.
When I loaded my userControl in my PageLoad event, I cannot cast the control to my usercontrol name, & because of that I cannot set a property of my control.
I want to do this, but this gives error at compile time:
MyControl myUserControl =(MyControl)Page.LoadControl("~/Controls/MessageControl.ascx");
Here MyControl is not available (It is not recognizable by the compiler) I tried Ctrl+Dot on MyControl, the compiler is not adding any reference to it also, It gives the following error
The type or namespace name 'MyControl' could not be found (are you
missing a using directive or an assembly reference?)
When I do this, it works:
var ctrl = LoadControl("~/Controls/MessageControl.ascx");
MyPlaceholder.Controls.Add(ctrl);
But now ctrl is not casted which prevents me to set a property of my user control.
Also does it matter If I instantiate the control in Page_Init instead of Page_load? Because in Page_Init I am unable to make a DB call, probably the assemblies/db config hasnt loaded in Page_Init..

Can't access master page from user control

My master page code looks something like this:
namespace Recipes
{
public partial class MasterPage : System.Web.UI.MasterPage
{
...
public void UpdateUserLogin()
{
NicknameLbl.Text = ((Recipes.BasePage)Page).CurrentUser.Nickname;
}
...
}
}
I want to call the UpdateUserLogin() method from a user control, something like this:
((Recipes.MasterPage)this.Page.Master).UpdateUserLogin();
But for some reason the compiler doesn't know Recipes.MasterPage (are you missing an assembly blablabla).
Sorry I can't show the exact error message, it's in French.
Maybe the problem is that I added the Recipes namespace around MasterPage manually, it wasn't added by VS.
By the way I'm using VS Web Developer Express 2008.
Do you have any idea how I can make this call work?
Both the MasterPage and the UserControl are child controls of the page they are used by. Your UserControl could potentially be used in a page that doesn't use your MasterPage, and so calling UpdateUserLogin() would not be valid.
You can check it like this, however, and make your call conditionally:
if (Page.Master is MasterPage)
{
((MasterPage)Page.Master).UpdateUserLogin();
}
UPDATE
It seems you were already aware of that, sorry. Your question is about the reference not working. What is the namespace of your UserControl?
I would recommend data binding the NicknameLbl to the CurrentUser.Nickname property. Then the NicknameLbl text will get updated automatically if the property changes.
include a MasterType directive at the top of the Content page ASPX file:
'<%# MasterType virtualpath="~/DetailsMaster.master" %>'
include a public method in the Master page
public void UpdateUserLogin(string value)
{
NicknameLbl.Text = value;
}
access the method from the Content page using the Master syntax:
Master.UpdateUserLogin(Some Text");
http://www.codeproject.com/KB/aspnet/Master_and_Contents.aspx
If your project is a Web Site Project (instead of a Web Application Project), then you do not have a project namespace. All code that is referenced from aspx.cs or master.cs files needs to be stored inside the App_Code directory, as the ASP.Net compiler will create several assemblies instead of just 1, and its not predictable which assembly will contain which aspx code.
Update after 1st comment:
The .ascx.cs and .aspx.cs stay where VS puts them. But it you want to reference classes etc, this needs to be placed inside App_Code, e.g. your Recipes.MasterPage or Recipes.BasePage objects.

How to access a user control's fields and methods without regitering the control?

I have a ASP.Net project that is setup in such a way that it can be dropped into any site and "just work." All the paths are relative to the current file, not relative to the "~". The paths are determined by ThePath = this.TemplateSourceDirectory;
This is working for everything expect registering a custom control that is created and added to one of the pages. I can add the control just fine with the Page.LoadControl but I cant cast it as the correct type to access anything.
How can I add a reference to the class from within the code itself?
If you don't know the control's specific type ahead of time, this isn't possible. The class must derive from UserControl, so you can cast it to a UserControl and you'll have access to all the methods and properties on that class. If there's some special information or functionality you need to require all controls to have, and you need to be able to assume those are always present, then you will have to write your own class that derives from UserControl, and require all custom controls to derive from that instead:
//all custom controls must inherit from this
public abstract class SpecialControlBase : UserControl
{
public abstract void DoSomethingSpecial();
}
Then you could cast all controls at load-time to this SpecialControlBase, and have access to the DoSomethingSpecial method.
But as far as the most-specific members of a class loaded at runtime, think about it - if I write my own control called RexsUserControl and drop it into your application, there's no way you could know what methods and fields I've put on my control, so you can't write any code that references those members specifically.
Rex M, If you register the control on the ASPX page, you can cast the control on the code behind.
In the ASPX:
<%# Register TagPrefix="Mine" TagName="Ctrl" Src="~/Test/User/Controls/UserCtrl.ascx" %>
Then, in the ASPX.CS:
User_Controls_UserCtrl myUserCtrl = LoadControl("~/Test/User/Controls/UserCtrl.ascx");
however, if you don't register the control in the ASPX first, you get this error:
CS0246: The type or namespace name 'User_Controls_UserCtrl' could not be found (are you missing a using directive or an assembly reference?)

The type name {myUserControl} does not exist in the type {myNamespace.myNamespace}

I have a problem (obviously the question :)
I have a project-- MyProject... hence the rest of the project uses a default of any classes as namespace "MyProject"... no problem.
In my project, I created a custom user control that has many other controls on it (label, textboxes, etc). So, that class is ALSO within the default namespace of "MyProject". All compiles no problem. Just to confirm scope visibility, on this user control, I made sure that the DESIGNER code and the Code-Behind (My code) are BOTH within the same "MyProject" namespace (they are), AND they are both respectively PUBLIC PARTIAL CLASS MyUserControl.
Now the issue. I create a simple form (also in namespace "MyProject" by default). From the toolbox, the "MyUserControl" exists so I drag it onto MyNewForm. Drag/Drop is fine.
Save all, compile, fail... The Designer is adding an extra "MyProject" reference thus making it appear that the user control is actually located at MyProject.MyProject.MyUserControl .. instead of MyProject.MyUserControl.
As soon as I manually remove the extra "MyProject.", save and compile, all is fine. However, if I re-edit the form, change something, M$ changes it back to the original "MyProject.MyUserControl" reference.
All that being said, here are the snippets from my project...
namespace MyProject
{
partial class MyNewForm
{
...
private void InitializeComponent()
{
// THIS is the line that has the extra "MyProject." reference
// when I manually remove it, all works perfectly
this.MyUserControl1 = new MyProject.MyUserControl();
}
}
private MyUserControl MyUserControl1;
}
Then, in the MyUserControl definition I have...
namespace MyProject
{
public partial class MyUserControl : UserControl
...
}
and from the MyUserControl via the Designer...
namespace MyProject
{
public partial class MyUserControl : UserControl
...
}
Thanks for the help...
What the designer is doing is ok.
--> You have somehere in your project a namespace called MyProject.MyProject.
(Try finding it in "Class View")
PS. to anyone who has same problem but has not found any solution...
Assuming you have created a new WindowsFormApplication;
Create a new WindowsFormApplication project using same name as its solution name.
The default pre-created Form name comes called "Form1". And change its name same as the project name.
Add new UserControl class into the project.
Build/Rebuild the project and check the usercontrol is located at Toolbox.
Drag the usercontrol onto the form and start debugging.
Error: The type name 'userControlName' does not exist in the type 'projectName.FormName'
I had research on net for any solution but couldn't come up with any answer...
But if you change the form name any other different from the project name, it'll be resolved.
If you insist on that the form name and project name has to be same depending on your project needs, a custom DLL could be created and use the usercontrol in it.
Then to use as a control, add the DLL file to "ToolBox" using "Choose Items..."
Finally it is going to be ready to use.
PS2. struggling the same problem for hours, this is the solution I found.
The Namespace Name and Class Name need to be different. The code generated by adding the WCF automatically references the Namespace but if the Class name is the same as the Namespace name, the generated code looks at the Class and nothing will compile.
Name of User Control and Form are same. Using different names will solve the issue.
Since this was a top search result when I had this error, just want to post my cause and solution.
I had two projects within a solution, sharing a 'common' class file which was added as a link.
I added a second 'helper' class file as a link, used its code within the first, and got the error.
The problem was I had not added the second 'helper' class as a link in both projects.
So the other project had an updated 'common' class, but no knowledge of the 'helper' class it now used.
Note to self: pay more attention to the project column of the error list :)
Just encountered this where I had a MasterPage that had an explicit
<%# Import Namespace="MyNamespace" %>
in the .master file
This also happens when you use different pages but with same name. In my case I had created "Grants.xsd" dataset and "Grants.aspx" page. Somehow they got in conflict resulting in this error.
You can easily trouble shoot this by hovering over the culprit keyword (class name) and in Visual Studio 2013, it will tell you exactly where the conflict is.

Categories

Resources