Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-10
8
Medium Priority
?
246 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

927 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