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
Related
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.
I created a server control which consist only of fews buttons.
CWNavigation.vb
<ToolboxData("<{0}:CWNavigation runat=""server""></{0}:CWNavigation>")> _
<DefaultProperty("Id")> _
Public Class CWNavigation
Inherits WebControl
I then referenced it in my ASPX page. Take note that the control are in the same solution, same project located in Commun/Navigation/CWNavigation.vb.
<%# Register TagPrefix="NAV" TagName="CWNavigation" Src="~/Commun/Navigation/CWNavigation.vb" %>
I added it to the page.
<NAV:CWNavigation ID="CWNavigationService" runat="server" />
But the designer file along with the code-behind generate it as.
Protected WithEvents CWNavigationService As Global.System.Web.UI.UserControl
But this is wrong.. it must be CWNavigation. Is there anything ive done wrong ?
Thanks!
Since its a custom Server Control you should register it as an assembly. Something like this...
<%# Register Assembly="Control.Assembly.CWNavigation" TagPrefix="NAV" TagName="CWNavigation" Namespace="Namespace.Of.Control.Assembly" %>
Or add it to your ToolBox (Context Menu->Choose Items) and then drag and drop (which will have Visual Studio wire it up for you).
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 am using an ASP.Net Web Application project. I have a user control which has an asp.net button in it. When i use that user control on the page, the button does not appear, but if i put the button directly on the page, the buttons shows up. Any idea what the problem is?
Also, inside that user control, i can override the render method and the test passed to the render method works, but I still do not get a button
The assembly is registered in the web.config
EDIT:After dave's post, i found that anything put in the .ascx file does not work, while overriding that user control's render method works
The Page
<%# Page Title="Home" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Site.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
<uc:SomeCustomControl ID="myControl" runat="server" />
<asp:Button runat="server" Text="outControl" />
</div>
</asp:Content>
the control .ascx file
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="Site.Controls.TestControl" %>
<asp:Button runat="server" Text="InControl" /><!--cant see this button-->
<p>I can't see this</p><!--cant see this text-->
the code behind for the .ascx file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Site.Controls
{
public partial class TestControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("I can see this");
base.Render(writer);
}
}
}
And the page source
I can see this
<input type="submit" name="ctl00$ContentPlaceHolder1$Button1" value="outControl" id="ctl00_ContentPlaceHolder1_Button1" />
If you register the control in the top of the page like this
<%# Register TagPrefix="uc" TagName="SomeCustomControl" Src="~/PathTo/TestControl.ascx" %>
(the key point here is the Src="..." part )
then the markup and controls in the TestControl.ascx will be visible and usable. if you only specify the namespace of the control for instance in web.config <pages> directive (or in the top of the page) then ONLY the codebehind is used, and any markup in the codefront (.ascx) is ignored
Is there any code in the user control affecting the button's visibility?
Does the rest of the user control appear, or does it only contain the button? If it's only a button then the entire user control may not be rendering properly. Add some text to the user control as a test to make sure it is just the button that's not working, not the entire user control.
You could also try enabling tracing for the page. The user control and button should show up in the control hierarchy listing. If they don't, then one or both isn't rendering properly.
Is this page and control in a standard default application? Is there any chance that a web.config or machine.config is having some influence over your UserControl? Perhaps an HttpModule?
In addition to checking from a "default" web site setup, you may also want to do some debugging and check the properties of your objects. You can do a write in your render method. Maybe look at how many controls are in the user control at render time. (maybe something is removing or clearing the controls container).
After converting a Asp.net website to web application, I receive 'Type MyNameSpace.MyMaster is not defined' error on pages that use the MasterType directive.
eg
In the aspx file
<%# MasterType VirtualPath="~/MyMaster.master" %>
In the designer.vb file the following code is generated and has the error described above:
Public Shadows ReadOnly Property Master() As MyNameSpace.MyMaster
Get
Return CType(MyBase.Master, MyNameSpace.MyMaster)
End Get
End Property
If I remove the namespace the error goes away but this code is regenerated every time I make a change to the aspx page.
If I use TypeName without the namespace(eg Typename="MyMaster") instead of VirtualPath in the directive, the code is generated without the namespace reference and therefore no error. However this fails at runtime instead. Including the namespace behaves in the same way as using the VirtualPath.
I am pretty sure this has something to do with the conversion process as new web application projects do not have this problem.
Any ideas what is going on here?
UPDATE:
Problem Solved!
In the conversion process I had added a Namespace with the same name as my root Namespace to a class. Simply removed that to fix the problem.
You could namespace your masterpage class. Namespaces are created by default in web app projects.
namespace TestNS
{
public partial class TestMP : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Read this to find out about any other gotchas:
http://msdn.microsoft.com/en-us/library/aa730880(VS.80).aspx
Problem Solved!
In the conversion process I had added a Namespace with the same name as my root Namespace to a class. Simply removed that to fix the problem.
Change
<%# MasterType VirtualPath="~/Mastername.master" %>
to
<%# MasterType TypeName="Mastername" %>
this will work perfectly