Add custom user control to published web project - asp.net

I am beginning to wonder if this is even possible. It just seems so simple.
I have a published web project. I want to add some .ascx files (with .cs & designer.cs files) to that published web site. These are simple custom user controls that access methods already part of the original application.
Question? Is it possible to just drop these in the published web project without building the entire solution? If not why?
When I drop these files in and run my application I get the error:
"Parse Error: Could not load type 'the name of my custom controls namespace'".
There is not a lot of code to show so this is all I have.
Default.aspx
<%# Page Language="C#" MasterPageFile="~/MasterPages/TwoColumn.master" AutoEventWireup="true"
Inherits="ApplicationName.Web.Default" CodeBehind="Default.aspx.cs" %>
<%# Register TagPrefix="uc1" TagName="CustomControl" Src="~/Controls/Custom/CustomControl.ascx" %>
<asp:Content ID="content" contentplaceholder="cph" runat="Server">
<uc1:CustomControl ID="cc1" runat="server" CustomProperty="Hello World" />
</asp:Content>
CustomControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="CustomControl.ascx.cs"
Inherits="ApplicationName.Web.Controls.Custom.CustomControl" %>
<asp:PlaceHolder ID="ph1" runat="server></asp:PlaceHolder>
CustomControl.ascx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ApplicationName.Web.Controls.Custom
{
public partial class CustomControl : System.Web.UI.UserControl
{
///My logic
}
}
Again it seems so easy. What am I missing? Or is this not possible? Thanks.
UPDATE:
I figured this out. The above scenario is possible. The problem is not with the name-space as the error message suggests. Rather it is the code-behind declaration. Code-behind files for any type of file are compiled when the application is published. I am still confused as to why it appears to be editable when you browse through a web directory, I would think it would be stored in a .dll file or something. Maybe someone can shed some light on this.
Anyways, replacing code-behind with code-file rectifies the problem as code-files are not compiled and are therefore readable at application run-time.
Some links that were helpful can be found here and here.

It is possible but you still have to compile your user control and drop that dll into the proper bin directory for your app. That's usually the cause of the type loading error you described.

This approach could be sloppy, you are basically either
1) Creating the User Control in the wrong project
2) Trying to add the same User Control to two projects
Have you thought about a cleaner approach and just creating a class that inherits from System.Web.Ui.Control and then adding this in a .common project? Then pulling this into the corrct project? The problem with your approach is on precompilation and deployment you could end up trying to put two user controls into the same folder which will break the build....
The alternate approach (and the microsoft way) would be like this...
The code - write a custom control
namespace MyProject.Common.Controls
{
public class PolicyTab : System.Web.UI.Control
{
protected override void CreateChildControls()
{
base.CreateChildControls();
HtmlGenericControl policyTab = new HtmlGenericControl();
policyTab.InnerHtml = "<strong> Some policy code here! </strong>";
this.Controls.Add(policyTab);
}
}
}
The page reference - how to reference it in your UI project
<CommonControls:PolicyTab runat="server" ID="temp"></CommonControls:PolicyTab>
Web.config - what you need to import this control into all of your UI pages
<add tagPrefix="CommonControls" namespace="MyProject.Common.Controls" assembly="MyProject.Common"/>

Related

The name "Label1" does not exist in the current context

I've got a weird issue in Visual Studio where I can't reference controls from the code-behind page.
To give a really simple example, my page is like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="New.aspx.cs" Inherits="ITDashboard.idea.New" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
And my codebehind is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ITDashboard.idea
{
public partial class New : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "hello";
}
}
}
The application is called ITDashboard, and this page is in a folder called "idea".
Have looked around, and some posts suggest deleting the designer file and recreating it. However, there's no option in VS2012 that I could see. Also I get the same error with a new blank page, even at the site root.
I don't have enough points yet to add a comment, so please forgive this comment in the answer box!
I've seen this before. "This" = VS not adding controls to the designer file. From what I was able to find, it's just a corruption in the page. Or if it's happening on all pages, it's a corruption of the project. You didn't do anything to cause it, so there is nothing you can do in the future to avoid it. Luckily, in 13+ years of working with various versions of VS, I've only seen it two or three times.
Andrew nailed it. If the problem is isolated to this page, then create a new page and replicate your corrupt page one control at a time. If it's at the project level (and it sounds like it is), you'll have a little more work to do.
Here's a good tip on how to recover from a corrupt project:
How can I recover a corrupt .csproj file in Visual Studio 2010?
Good luck!
Thought I'd post in case somebody has the same thing.
All the web posts I saw were pointing to "Convert to web application" (now under Project in VS2013). However this wasn't working for me.
Finally got it working by changing the .net Framework type (arbitrarily to 4.5). I then re-ran the Convert to web application option and it recreated the designer.cs files.
Also, any new controls I add are automatically added to the designer.cs properly now.
if you are having redouble setting the text of Label1 ...change Label1.text to Label1.Text.
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'label1' does not exist in the current context ConsoleApplication6

How to change namespace name in an asp.net webform

How to change the namespace name in an asp.net webform
For example in my project i have created 3 folders Master,Transaction,Reports in Master folder there are 10 forms by default th namespace comes ProjectName.Master,but when i manually change the name by adding my formname it doesn't work eg:-ProjectName.Master.MasterFormName.And moreover when i double click on a button then it doesn't go in the code behind.
Changing a namespace in c# code alone isn't sufficient.
You also need to tell your .master file what class it inherits from.
Set the new namespace in you .master file and you will be good to go.
<%# Master Language="C#" AutoEventWireup="true" CodeFile="ProjectName.Master.cs"
Inherits="ProjectName.Master.MasterFormName.MasterFormName" %>
Just my personal opinion: putting your class name in the namespace doesn't make a lot of sense.

Must custom controls be placed in the App_Code?

I wrote a custom control inherited from WebControl. (Note: not a user control).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace Taopi.WebComponents
{
public class RatingLabel : WebControl
{
public RatingLabel()
: base("span")
{ }
//...
I placed it in /App_Code, and on a web page it is registered and used as following:
<%# Register TagPrefix="uc" Namespace="Taopi.WebComponents" %>
...
<uc:RatingLabel Rating='<%# Eval("rating") %>' runat="server" />
They run well until I move RatingLabel to /Components, which is folder cerated by me. I got an error saying "Unknown server tag uc: RatingLabel" when I try to run the website.
I believe the registration is wrong, so what modification is needed? Must custom controls be placed in the App_Code?
I have another question that where do you usually place your custom controls (except for refering a external DLL)? Are there any "suggested" locations?
I've run into this before. The only way I've found you can store code outside of the AppCode folder is to add a "Class Library" project or external DLL as you suggested (which is my preferred approach anyhow as it offers use across multiple projects).
Alternatively, if you use a "Web Application" project type instead of a "Web site" project, you can store code anywhere.

Strongly-typed ASCX in WebForms 3.5?

I'm looking to get rid of the code-behind for a control in my WebForms 3.5 application. Again bitten by the bug of how it's done in MVC, I'd like to get a step closer to this methodology by doing:
<%# Control Language="C#" Inherits="Core.DataTemplate<Models.NewsArticle>" %>
This gives me the parser error you'd expect, so I remembered back to when this was an issue awaiting a fix in the MVC Preview, and changed it to:
<%# Control Language="C#" Inherits="Core.DataTemplate`1[[Models.NewsArticle]]" %>
But this doesn't work either! How is it that the MVC team were able to harness this ability? Was it something special about the MVC project type rather than the latest VS2008 Service Pack?
Short of giving up and requiring future templates to have code-behind files, what are my best options to get this as close to the generic user control method as possible?
Well, it appears like I've managed to do it. After looking at the PageParserFilter implemented by the MVC team for ViewUserControl<T>, I was able to construct something similar for my own DataTemplate<T> purposes. Sweet. I can now use the line:
<%# Control Language="C#" Inherits="Core.DataTemplate<Models.NewsArticle>" %>
And, without any code behind file, it parses! I'll report back if I find that I've broken something else in the process!
With WebForms you lose pretty much everything that makes them useful without a code behind page, because then VS can't auto generate the designer file that holds the actual definitions for all your runat="server" controls.
What you can do is have a common base page class, and make that generic:
public class DataTemplate<T> : Page {
public T Model {get;set;}
}
public partial class MyCodeBehindClass :
DataTemplate<Models.NewsArticle> {
...
}
This would allow all the drag-drop component stuff that WebForms does to work unhindered, while also allowing you to access a strongly typed model on the page:
<%# Control Language="C#" Inherits="MyCodeBehindClass" %>
<% foreach( var item in Model ) { %>
<!-- do stuff -->
<% } %>

ASP.NET control does not render

I have an extremely simple control i'm trying to render which looks like this:
using System;
using System.Web;
using System.Web.UI;
namespace CORE.BusinessObjects.Web.Controls
{
public class TestControl : Control
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("Hello from TestControl!");
}
}
}
I'm calling the control in the following manner:
<%# Register TagPrefix="Custom"
Namespace="CORE.BusinessObjects.Web.Controls" %>
<Custom:TestControl ID="testControl" runat="server" Visible="true">
</Custom:TestControl>
Am i doing something wrong? I have also failed to run ANY control samples i found online. Nothing executes. I can only execute the constructor of the control. Every other method i tried to override like Render() or CreateChildControls() doesn't get executed.
Thanks.
EDIT: I forgot to mention that the control is included on a page with a Master page. The control actually runs fine in the Master page, but not outside of it.
You should try the following:
a. Inherit from the System.Web.UI.Control.WebControl class.
b. Make sure that you create a control library and your custom control library is compiled and referenced in the relevant web project. Optionally, add it to the toolbox and drag it onto the form from there. That should also add the reference in one step.
c. Make sure that your control declaration has the Assembly attribute which points to the name of the referenced control assembly:
<%# Register TagPrefix="Custom" Assembly="CORE.BusinessObjects" Namespace="CORE.BusinessObjects.Web.Controls" %>
<Custom:TestControl ID="testControl" runat="server" Visible="true" />
Trying inheriting from CompositeControl or WebControl

Resources