designer.cs issues with using user control in Visual Studio - c#

During my development, I have a web user control project and another web project which will use the user controls from the web user control project.
So I copy the DocControl.ascx file to my web project and try to use the properties of the DocControl.ascx.
But VS does not know the properties of the control. So when I check the designer.cs, the reference is like that
protected global::System.Web.UI.UserControl Control;
Which should be
protected global::MSN.DocControl Control;
So I changed the name of the control from System.Web.UI.UserControl to MSN.DocControl and I can use the properties of the DocControl.ascx.
But my issue is whenever I modify ( eg. put a lable in aspx) the aspx file, the reference in designer.cs become
protected global::System.Web.UI.UserControl Control;
So I has to change it whenever I modify my aspx.
What should I do so I don't need to change the designer.cs
Thanks in advance......

I have solved it by moving
protected global::MSN.DocControl Control;
from designer.cs to .cs page.
So whenever you made any changes, it will be OK.
#kokbira - > hope that it helps you.

In my case, it was a bad src path in my register line. This caused no error messages, but would generate the generic control instead of the specific class, with the same symptoms you describe.
I had this (which has the wrong Src path):
<%# Register TagPrefix="uc" TagName="Pipes" Src="/Controls/Pipes.ascx" %>
...
<uc:Pipes id="ucPipes" runat="server" />
and it generated this, which is generic, and has none of the control's properties:
protected global::System.Web.UI.UserControl ucPipes;
When I did the correct path, with Category folder, it worked:
<%# Register TagPrefix="uc" TagName="Pipes" Src="/Category/Controls/Pipes.ascx" %>
...
<uc:Pipes id="ucPipes" runat="server" />
and generated this correct one, so all properties worked:
protected global::Company.Category.Controls.Pipes ucPipes;

Related

I need help identifying a programming technique so that I can research and fix it

The Code:
ParentControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ParentControl.ascx.cs"
Inherits="ParentControl" %>
<%# Register Src="~/ChildControl.ascx" TagPrefix="Prefix" TagName="ChildControlTag" %>
ParentControl.ascx.cs
public partial class ParentControl : UserControl
{
List<ASP.childcontrol_asxc> controlList = new List<ASP.childcontrol_asxc>();
... inside a public property setter ...
var control = new ASP.childcontrol_ascx(); // <ABC> what the heck is this reference?
controlList.Add(control);
...
}
ChildControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ChildControl.ascx.cs"
Inherits="ChildControl" %>
<asp:Repeater ID="childRepeater" runat="server" EnableViewState="false"
ViewStateMode="Disabled">
ChildControl.ascx.cs
public partial class ChildControl : UserControl
{
...
protected void Page_Load(object sender, EventArgs e)
{
childRepeater.DataSource = xyz; // <XYZ> crashes if I don't use the weird reference
}
}
The Problem:
I don't understand what the reference comment marked with <ABC> is all about. That's not the class I defined... but Intellisense doesn't complain. If I press F12 on it, it takes me to the <%# Control ... %> tag at the top of ChildControl.ascx instead of the control definition in ChildControl.ascx.cs.
I don't understand what this other version of the control is, why it's in the ASP namespace, or what the implications of using it instead of the control directly are. Worse, this compiles fine on my local workstation, but throws a compiler error on the TFS server we use for CI.
The Question:
Does anyone know the name for this method of using/referencing the custom User Controls, and/or have links/information I can look at to get a better handle on what this is and what the implications are? I've been unable to find anything useful via Google. While a fix/workaround would be great to have - I would still like to be able to research the technique.
Explorations:
I've tried replacing the usages with the actual class name, instead of the weird ASP.class_ascx references... it compiles fine if I do this, but unfortunately it fails at runtime. It seems like the other reference changes how it interacts with the asp.NET lifecycle - server controls defined in the aspx of the child control are null in the Page_Load() of the child control (marked <XYZ>). Under the weird ASP.control_ascx reference approach, the repeaters are properly defined when it hits the Page_Load() of the child controls, but I don't understand why.
Miscellaneous:
The project is using the Roslyn compiler on my local workstation, via the DotNetCompilerPlatform NuGet package but I believe its just using the VS/TFS 2015 built in compiler on the CI server. This might explain why TFS throws compiler errors - it tells me The type or namespace name 'usercontrol_ascx' does not exist in the namespace 'ASP' (are you missing an assembly reference?). I'm looking at configuring TFS to use the compiler from the NuGet package which will then hopefully compile - but this is still weird and I'd like to understand it.
This is the class that the ASPX file compiles to. It inherits your class and adds code that renders your ASPX content.

Creating a C# Module in DNN

I want to embed a C# class in a module so that I can call the functions using buttons and click events. I have no idea how to do this. I've managed to write the class I want to use, but where do I put the code? I created a module in DNN and got this:
<%# Control Language="C#" ClassName="MailingSystem" Inherits="DotNetNuke.Entities.Modules.PortalModuleBase" %>
<h1>Congratulations</h1>
<p>You have successfully created your module. You can edit the source of the module control by selecting the View Source Action from the Action Menu.</p>
<script runat="server">
</script>
I can't put my code in here, I get all sorts of errors about namespaces not allowed, can't import classes with "Using", and so on. So what am I supposed to do? My class is working, I just need to wrap it in a module and put it on a DNN page.
It is better to start with a DotNetNuke Module Template, like this one. It isn't as easy as creating an aspx page.
simply you can double click on design part of the page , then the page load section will be appear in the page and you can put your c# code there.
You may want to do something like this:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
/// code goes here
}
</script>
If you don't want want to go the whole module template route. Do the following.
Create an webusercontrol (.ascx)
Go to the code behind file (.ascx.cs) and change the class to inherit from DotNetNuke.Entities.Modules.PortalModuleBase (your will need to add DotNetNuke.dll as reference)
Add what ever controls you want to the ascx and attach any event handlers. I prefer to do this in the page init method
In ASCX:
<asp:Button ID="btnButton" Text="Click me" runat="server" />
In Code Behind:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
btnButton.Click += btnButton_Click;
// OR
btnButton.Click += (sender, e)=> { // Button clicked! Do something };
}
protected void btnButton_Click(object sender, EventArgs e)
{
// Your button has been clicked, Do something
}
Compile code
Get [yourprojectname].dll file from the bin folder of your project and copy it into DNN's bin folder. Then, copy your module control ascx into a dedicated folder in DNN's DesktopModules Folder
Example Path: DesktopModule > YourProjectName > [YourASCXName].ascx
Login to DNN, go to Host>Extensions and click add extension. Go through the wizard making sure to set your extension type to Module (there are many different types of extensions in DNN).
Once added, you will be be taken back to the module extensions page. Scroll down and find your module extension. Click edit, go to module definitions and add a module definition with a meaningful name.
Example: YourProjectNameMainView
Then, add your ASCX file as a view to that module extension. Click save and you are done with setup
You should be able to drop your (VERY BASIC) module on a page and use it!

Problem With Custom Control on Page Using a MasterPage

I have a content page which has a related master page.
I register a prefix <%# TagPrefix ..... and can load other custom controls at that namespace.
However, one particualar control at the same namespace, when added to the aspx page, breaks it.
The control in question inherits from asp:Panel, has a parameterless constructor, defines a few public accessors, and creates some standard child controls, and nothing much else.
Are there some fundamental restrictions to creating custom asp controls that I am breaking unknowingly?
Add the control back to the page. Delete the designer file for the page: .aspx.designer.cs
Then right click on the page and select Convert to Web Application. This should give you the actual error the page has when attempting to write the control definition to your designer file.
I suspect there is a compilation error in your custom control.
My control was attempting to access Page.Header which was null as the master page had not marked the tag with runat="server".
I guess that is a fundamental restriction that I was looking for...

asp.net Cannot create UserControl in class

My ultimate goal is to create a UserControl in a class in my App_Code folder and render the html from that into a string.
The rending of HTML can be done using this example I believe:
How do I get the HTML output of a UserControl in .NET (C#)?
My Question:
I created my user control
public partial class Controls_MyUserControl : System.Web.UI.UserControl
I've registered it in my web.config
<add tagPrefix="UC" src="~/Controls/MyUserControl.ascx" tagName="MyUserControl" />
I can reference this on a page just fine
<UC:MyUserControl ID="MyUserControl1" runat="server" />
Yet when I try to create an object of this control type in a class (in my app_code folder) it doesn't allow me.
Controls_MyUserControl dummy = new Controls_MyUserControl();
The potentially strange thing is when I put this code into a code behind of a page, it works.
Any ideas on what I need to do to have the usercontrol to be able to be created in my class in the app_code folder?
My guess is, is that I need to reference the control in a "using" statement at the top, but I'm not sure what that'd be.
It's not in a namespace to my knowledge (at least there's no namespace, in the code behind of the actually user controls). Though I'm not sure if it inherits one from the System.Web.UI.UserControl.
Thanks in advance
Some workaround as posted by Scott Alen http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-app_code-folder.html
Suppose you have all your user controls in ~/UserControls/
Then on your code behind add the following;
using YourSpaceName.UserControls;
This will set reference to any user control in that location.
Note: if you are using a user control within another, make sure to create a folder for each of them. I have experiencing problems, specifically with VB where it would compile but give me a runtime error. Since I encounter that problem I create my user controls each in it's own folder.

Controls null when using a UserControl between projects

TL;DR: All controls within a usercontrol that's being used outside it's home project are null when that usercontrol's Page_Init/Page_Load methods are called.
The setup is like this:
Projects "UI.Frontend", "UI.ControlPanel", and "UI.Common" are "ASP.NET Web Application"s. UI.Common is never meant to be accessed directly- it just contains UserControls that are needed in both the frontend and the control panel.
So, an aspx file (SomeFrontendPage.aspx) in UI.Frontend contains the lines:
<%# Register tagprefix="BP" Namespace="UI.Common" Assembly="UI.Common" %>
and later:
<BP:MyControl runat="server" ID="ctlMyControl" />
while over in UI.Common, there's a control named MyControl (normal ascx, ascx.cs, and ascx.designer.cs files). Now, when I open SomeFrontendPage.aspx in a browser, ctlMyControl gets loaded and it's init+load methods get executed. The problem is all subcontrols of MyControl never get initialized. Example (if MyControl.ascx has a textfield of ID txtBlah):
protected void Page_Init(object sender, EventArgs e)
{
txtBlah.Text = "test";
}
The above code will run, but will cause a null pointer (well, "Object reference not set to an instance of an object") since txtBlah will be null.
Edit:
An example control would be:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="Common.MyControl" %>
Whatever: <asp:TextBox ID="txtWhatever" runat="server" />
You may find you have more problems that what is immediately shown. ASCX files aren't embedded in assemblies by default, and when they are, you then need to create a virtual path provider to access them.
Can we see an example control?

Categories