Solved

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

Posted on 2011-02-10
8
243 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Tom 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Author Comment

by:Tom 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:Tom 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:Tom Knowlton
ID: 34873923
I was hoping that would not be your answer, LOL.


0
 
LVL 5

Author Closing Comment

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

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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