Solved

Register server tag for customized System.Web.UI.WebControls.Image

Posted on 2011-02-10
8
238 Views
Last Modified: 2012-05-11
I created an "App_Themes" aware image derived from System.Web.UI.WebControls.Image.

It shows-up in my Toolbox and I can use it.


But I want to know how I can declare it, such that I can have it available EVERYWHERE like the <asp:  ...> server controls are.

How can I do this?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Campus_Webstore
{
    public class ThemeAwareImage : Image
    {           

        public ThemeAwareImage()
        {
        }

        public override string ImageUrl
        {
            get
            {
                return base.ImageUrl;
            }
            set
            {              
                Page p = (Page)HttpContext.Current.Handler;
                base.ImageUrl = "~/App_Themes/" + p.StyleSheetTheme + "/images/" + value;    
            }
        }
    }
}

Open in new window

0
Comment
Question by:knowlton
  • 5
  • 3
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34870204
Tom,

Here are some things to get the ball rolling:

1) If the custom control is defined in a referenced DLL, it should show up on the toolbox when you rebuild the solution.  Then, you should be able to drag from the toolbox to the web page, and link everything up.

2) The @Register directive:

@ Register
http://msdn.microsoft.com/en-us/library/c76dd5k1.aspx

Creates an association between a tag prefix and a custom control, which provides developers with a concise way to refer to custom controls in an ASP.NET application file (including Web pages, user controls, and master pages).
0
 
LVL 5

Author Comment

by:knowlton
ID: 34872544
I don't mean by using the @Register directive.  I know about that.

I am talking about being able to do this:

<asp:TextBox ...

or

<asp:ListBox ...


How does Visual Studio recognize the <asp> tag?



I want to do the same thing with my derived controls.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34872848
It sounds like you are looking for the TagPrefixAttribute...

TagPrefixAttribute.TagPrefix Property
http://msdn.microsoft.com/en-us/library/system.web.ui.tagprefixattribute.tagprefix.aspx

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly:TagPrefix("CustomControls", "custom")]


namespace CustomControls
{

	// Simple custom control
	public class MyCS_Control : Control
      {
            private String message = "Hello";

            public virtual String Message
            {
                  get
                  {
                        return message;
                  }
                  set
                  {
                        message = value;
                  }
            }

            [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
            protected override void Render( HtmlTextWriter writer)
            {
            writer.Write("<span style='background-color:aqua; font:8pt tahoma, verdana;'> "
                        + this.Message + "<br>" + "C# version. The time on the server is " + System.DateTime.Now.ToLongTimeString()
                        + "</span>");

            }

      }

}

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Author Comment

by:knowlton
ID: 34873388
I'll buy that.  :)


So, what would the markup look like for a brand new webform that had only this control on it?

0
 
LVL 5

Author Comment

by:knowlton
ID: 34873453
I want my derived control to work ***just like**** the built-in asp .net server controls.......in that I do not have to declare them with the @Register directive in order to use them.





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Campus_Webstore.WebForm3" %>




/////////////// THIS IS NEEDED OR MY DERIVED CONTROL IS NOT RECOGNIZED ///////////////////
<%@ Register assembly="Campus Webstore" namespace="Campus_Webstore" tagprefix="ThemeAwareImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:Button ID="Button1" runat="server" Text="Open Notepad" onclick="Button1_Click" />       
    </div>


///////// IF I REMOVE THE @REGISTERED DIRECTIVE, I GET A RUNTIME ERROR /////////////
    <ThemeAwareImage:ThemeAwareImage ID="ThemeAwareImage2" runat="server" />
    




/////////////// THE PAGE "KNOWS" THE <asp> TAG, EVEN THOUGH NOTHING REGISTERED ABOVE ///////////////////
<asp:Button ID="Button2" runat="server" Text="Button" />




    </form>
</body>
</html>

Open in new window

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 34873883
I don't believe that you will get a web control that works without the @Register.  ASP.NET controls are special beasts, that can be defined under the covers.
0
 
LVL 5

Author Comment

by:knowlton
ID: 34873923
I was hoping that would not be your answer, LOL.


0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 34873927
Sometimes the correct answer is, "You can't do that."  :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question