Solved

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

Posted on 2011-02-10
8
233 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Author Comment

by:knowlton
Comment Utility
I'll buy that.  :)


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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
Comment Utility
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
Comment Utility
I was hoping that would not be your answer, LOL.


0
 
LVL 5

Author Closing Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now