Solved

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

Posted on 2011-02-10
8
234 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 47
Trouble with References... 5 44
getting id from database 5 37
How can I build my own IDE using ASP.NET MVC? 2 33
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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