How should control images should be handled?

Discussion in 'ASP .Net Building Controls' started by ~~~ .NET Ed ~~~, Oct 31, 2004.

  1. This is something that has been nagging me over and over again and I think
    there must be a better solution out there.

    Some times (usually?) user controls and custom web controls need images such
    as icons and other decorations in the form of images. The problem is... how
    do you handle that in a way that everything is self-contained or at least
    free form file system dependencies?

    For example, the control needs a few icons and some background image for
    part of the control. Is there a way to make it self-contained in the
    Assembly? and if so, how can I extract/handle such image for use in an HTML
    tag such as <img src= "icon.png"> ?

    I typically put all my images in the img directory at the root of my web
    host account. Now, in a production system this would simply translate to
    http://www.mydomain.com/img/icon.png. But then on my development machine my
    root directory is actually a virtual directory that is accessed as
    http://localhost/mydomain/img/icon.png. That means that in production my
    root is actually the domain root, while in the development system the root
    is actually an application directory, a subdirectory of the IIS root.

    In this case I am always stuck with the problem that I need to make the
    control able to find its images easily (same goes for any stylesheet it may
    need). Having to enter a root path in every instance of the control does not
    sound very practical. Having to specify a web.config key for every instance
    of the control also seems impractical, and hardcoding the key also sounds a
    bit crappy.

    I suppose there are much better ways to handle this so any input is
    appreciated.

    Regards,
    Em.
    ~~~ .NET Ed ~~~, Oct 31, 2004
    #1
    1. Advertising

  2. "~~~ .NET Ed ~~~" <> wrote in message
    news:Os%...
    > This is something that has been nagging me over and over again and I think
    > there must be a better solution out there.
    >
    > Some times (usually?) user controls and custom web controls need images
    > such
    > as icons and other decorations in the form of images. The problem is...
    > how
    > do you handle that in a way that everything is self-contained or at least
    > free form file system dependencies?
    >
    > For example, the control needs a few icons and some background image for
    > part of the control. Is there a way to make it self-contained in the
    > Assembly? and if so, how can I extract/handle such image for use in an
    > HTML
    > tag such as <img src= "icon.png"> ?


    You don't.

    When the browser sees that <img> tag, it issues an HTTP request for
    icon.png. The web server will either find that file and return it, or will
    return an error. Having the file sitting around in your assembly just won't
    do it, because the web server isn't going to go looking in your assembly.

    Of course, the alternative would be to get the web browser to look in your
    assembly. :)

    You can do that by writing a custom HttpHandler and configuring the web site
    to call it whenever there is a request for a particular resource. For
    instance, you could put "icon.png" for a control called "MyControl" which
    resides in a web application called "/app" in "/app/icon.png.MyControl". If
    you configure the HttpHandler to process all requests for *.MyControl, then
    it will be able to look in the assembly which MyControl is in for the
    appropriate resources.

    > I typically put all my images in the img directory at the root of my web
    > host account. Now, in a production system this would simply translate to
    > http://www.mydomain.com/img/icon.png. But then on my development machine
    > my
    > root directory is actually a virtual directory that is accessed as
    > http://localhost/mydomain/img/icon.png. That means that in production my
    > root is actually the domain root, while in the development system the root
    > is actually an application directory, a subdirectory of the IIS root.


    Try using "~/img/icon.png" in any server-side control. In particular:

    <img runat="server" src="~/img/icon.png">

    That will look in the appropriate place depending on where your application
    is deployed.

    John Saunders
    John Saunders, Nov 3, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    0
    Views:
    16,911
    Peter
    Jul 11, 2003
  2. Ajith Nair
    Replies:
    3
    Views:
    6,882
    Ajith Nair
    Aug 25, 2005
  3. =?Utf-8?B?Y2NnYXR2b2x2bw==?=

    Why are labels handled differently than an input control with clie

    =?Utf-8?B?Y2NnYXR2b2x2bw==?=, Aug 1, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    2,742
    =?Utf-8?B?Y2NnYXR2b2x2bw==?=
    Aug 1, 2006
  4. SaikiranPuppala
    Replies:
    0
    Views:
    453
    SaikiranPuppala
    Jul 11, 2008
  5. vj
    Replies:
    3
    Views:
    226
Loading...

Share This Page