Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register custom server control on ASP.NET page

Tags:

I have a project and I am trying to register a custom server control (there is no .ascx file) on the page. I am currently using

Class Declaration

namespace MyApp.Controls{     public class CustomControl: WebControl{         public string Text         {             get             {                 String s = (String)ViewState["Text"];                 return ((s == null) ? String.Empty : s);             }             set             {                 ViewState["Text"] = value;             }         }                 protected override void RenderContents(HtmlTextWriter output)         {             output.Write(Text);         }     } } 

On my page,

<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" %> <myControls:CustomControl runat="server" Text="What up!" /> 

I receive a Parser Error, with the message "Unknown server tag 'myControls:CustomControl'."

What am I doing wrong?

like image 704
smartcaveman Avatar asked Mar 11 '11 17:03

smartcaveman


People also ask

What is user control and custom control in ASP.NET with example?

User controls are custom, reusable controls, and they use the same techniques that are employed by HTML and Web server controls. They offer an easy way to partition and reuse common user interfaces across ASP.NET Web applications. They use the same Web Forms programming model on which a Web Forms page works.

How do you register a user control Add tag prefix tag name?

Adding User Controls to a Web Forms Page and then change the TagPrefix and TagName as per your requirement. In order to set the TagPrefix and TagName of the UserControl . It is required to register the control first. Drag Drop the WebUserControl will convert it into Anchor tag.


1 Answers

Well, if this control is in another class library, or even if it's in the same one, it wouldn't be a bad idea to specify control's assembly in @Register:

<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" Assembly="MyApp" %> <myControls:CustomControl runat="server" Text="What's up!" /> 

Clean and rebuild your solution too in order to verify everything is compiled rightly!

like image 143
Matías Fidemraizer Avatar answered Sep 22 '22 08:09

Matías Fidemraizer