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.
Related
This is specifically for Visual Studio 2015.
In my Site.master I have
<%# Master Language="C#" AutoEventWireup="true" CodeFile="~/Site.master.cs" Inherits="SiteMaster" %>
<asp:Literal ID="Literal1" runat="server" />
And in my Site.master.cs I have
Literal1.Text = "test";
In addition in Site.master.cs I have
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebMatrix.Data;
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = "test";
}
And it won't build. I get the error:
error CS0103: The name 'Literal1' does not exist in the current context.
There is no designer file. I don't know how to get the designer file back for VS2015. I've tried several older suggestions from previous versions of VS.
I'd like to get it to build whether I need a designer file or not.
Try to clean and rebuild your project obviously when the project is not running. Sometimes when your project is running visual studio cannot generate designer files properly. Try change the label Id or remove and re-insert it.
To solve this error I created a new master page. I deleted everything below the header and pasted everything below the header from the original (not including the original's header.)
Also, in the code behind I did not delete the class, but instead pasted the original functions into the new class.
Then on the pages that use the old master page I changed them to use the new master page.
I am trying to configure internationalization for my DNN project. I have added two languages, and now I want to access DNN's Language control in order to make it visible on project's pages.
I have found a snippet
<dnn:LANGUAGE runat="server" ID="dnnLANGUAGE" ShowLinks="True" ...
and I tried to use it. Compiler does not recognize dnn: tagPrefix so I have to register it, that is, I have to pass class's namespace inside Register tag in my ascx page. But I can not find the namespace anywhere. I have also browsed www.dnnsoftware.com/dnn-api , but could not find it.
You need to add the following line at the top of your ascx page.
<%# Register TagPrefix="dnn" TagName="LANGUAGE" Src="~/Admin/Skins/Language.ascx" %>
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
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"/>
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