Custom control: How to render an embedded image at design-time?

Discussion in 'ASP .Net Web Controls' started by Justin, Jul 7, 2005.

  1. Justin

    Justin Guest

    Hi,

    I have a custom web server control, specifically a composite control,
    that includes an image button. The image button takes an ImageUrl
    property that renders at _runtime_, but at design-time the image does
    not render unless the user chooses an image.

    I would like to display a default image at design-time (or _at_least_
    reduce the size of the "missing image" icon that Visual Studio
    displays--it is too big**[see footnote]**).

    The ImageUrl property can only point to a path, obviously, which means
    I must depend on the user to have an image at a certain path. Also, I
    cannot access the Request object (thus neither the
    Request.ApplicationPath) at design-time, so I can't even get a valid
    path at design-time anyways.

    Therefore, I would like to use an embedded resource (an image included
    in the Visual Studio project, with 'Build Action' marked as "Embedded
    Resource") at runtime.

    But from what I can tell, System.Web.UI.Design.ControlDesigner only
    allows me to really affect the HTML at design-time via
    GetDesignTimeHtml(). But I cannot display an embedded resource with
    HTML...

    Here are relevant parts of my code:

    ---------------------------------------------

    public class PopupCalendar : System.Web.UI.WebControls.WebControl,
    INamingContainer {
    private TextBox textBox;
    private Image image;

    ....

    protected override void CreateChildControls() {
    this.Controls.Clear();
    textBox = new TextBox();
    this.Controls.Add(textBox);
    textBox.ID = "TextBox";

    image = new Image();
    this.Controls.Add(image);
    image.ID = "Image";

    }

    ....

    }

    ---------------------------------------------

    public class PopupCalendarDesigner : WebDesign.ControlDesigner {
    public override string GetDesignTimeHtml () {
    PopupCalendar popcal = (PopupCalendar)this.Component;

    return "<input type=\"text\" style=\"width: " +
    popcal.Width + "px\"
    /><img style=\"width: 16px; height: 16px;\">";
    }

    }

    ---------------------------------------------

    **[footnote]**
    I have tried putting "image.Width = Unit.Pixel(16); image.Height =
    Unit.Pixel(16);" in CreateChildControls(), but that means that only
    16x16 images can be used, because this affects _both_ design-time and
    run-time.

    Thank you very much for your help!

    - Justin
     
    Justin, Jul 7, 2005
    #1
    1. Advertising

  2. Justin

    Mythran Guest

    "Justin" <> wrote in message
    news:...
    > Hi,
    >
    > I have a custom web server control, specifically a composite control,
    > that includes an image button. The image button takes an ImageUrl
    > property that renders at _runtime_, but at design-time the image does
    > not render unless the user chooses an image.
    >
    > I would like to display a default image at design-time (or _at_least_
    > reduce the size of the "missing image" icon that Visual Studio
    > displays--it is too big**[see footnote]**).
    >
    > The ImageUrl property can only point to a path, obviously, which means
    > I must depend on the user to have an image at a certain path. Also, I
    > cannot access the Request object (thus neither the
    > Request.ApplicationPath) at design-time, so I can't even get a valid
    > path at design-time anyways.
    >
    > Therefore, I would like to use an embedded resource (an image included
    > in the Visual Studio project, with 'Build Action' marked as "Embedded
    > Resource") at runtime.
    >
    > But from what I can tell, System.Web.UI.Design.ControlDesigner only
    > allows me to really affect the HTML at design-time via
    > GetDesignTimeHtml(). But I cannot display an embedded resource with
    > HTML...
    >


    You can check to see if you are in design-time or not and set the
    width/height in CreateChildControls :)

    Mythran
     
    Mythran, Jul 8, 2005
    #2
    1. Advertising

  3. Justin

    Justin Guest

    As I said in the original message, " I have tried putting "image.Width
    = Unit.Pixel(16); image.Height =
    Unit.Pixel(16);" in CreateChildControls(), but that means that only
    16x16 images can be used, because this affects _both_ design-time and
    run-time. "

    Unfortunately, that is not too good because that means the image will
    always be forced to 16x16.

    Thank you.
     
    Justin, Jul 8, 2005
    #3
  4. Justin

    Mythran Guest

    "Justin" <> wrote in message
    news:...
    > As I said in the original message, " I have tried putting "image.Width
    > = Unit.Pixel(16); image.Height =
    > Unit.Pixel(16);" in CreateChildControls(), but that means that only
    > 16x16 images can be used, because this affects _both_ design-time and
    > run-time. "
    >
    > Unfortunately, that is not too good because that means the image will
    > always be forced to 16x16.
    >
    > Thank you.
    >


    Yes, you did...and I said to check for design time in CreateChildControls
    and wrap the code for changing the size with the check. That way, when it
    renders during run-time, the code for changing the size is not ran.

    Hope this helps,

    Mythran
     
    Mythran, Jul 8, 2005
    #4
  5. Justin

    Justin Guest

    I mis-read, I apologize.

    The only way I can think of to check for design-time is inelegant,
    i.e., checking for the existence of a Context object. Do you know a
    different way?

    Also, I assume you have no idea how to render an image at design-time?

    Thanks.
     
    Justin, Jul 8, 2005
    #5
  6. Justin

    Mythran Guest

    "Justin" <> wrote in message
    news:...
    >I mis-read, I apologize.
    >
    > The only way I can think of to check for design-time is inelegant,
    > i.e., checking for the existence of a Context object. Do you know a
    > different way?
    >
    > Also, I assume you have no idea how to render an image at design-time?
    >
    > Thanks.
    >


    You render an image the same way you render one at run-time (anchor
    tag)...but the path to the image has to be valid in design-time as well.

    VB.Net:

    Private ReadOnly Property IsDesignTime() As Boolean
    Get
    Return (Not Me.Site Is Nothing) AndAlso Me.Site.DesignMode
    End Get
    End Property

    C#:

    private bool IsDesignTime
    {
    get {
    return this.Site != null && this.Site.DesignMode;
    }
    }

    hth :)

    Mythran
     
    Mythran, Jul 8, 2005
    #6
  7. Re: Custom control: How to render an embedded image at design-time

    I posted an answer in the other forum.

    direct email:
    userid = Michael.Baltic
    server = NCMC.com
    --
    Staff Consultant II
    Enterprise Web Services
    Cardinal Solutions Group

    Future Business Model
    Loan Origination Services
    National City Mortgage


    "Justin" wrote:

    > I mis-read, I apologize.
    >
    > The only way I can think of to check for design-time is inelegant,
    > i.e., checking for the existence of a Context object. Do you know a
    > different way?
    >
    > Also, I assume you have no idea how to render an image at design-time?
    >
    > Thanks.
    >
    >
     
    Michael Baltic, Jul 8, 2005
    #7
  8. Justin

    Justin Guest

    > You render an image the same way you render one at run-time (anchor
    tag)...but the path to the image has to be valid in design-time as
    well.

    Yes, but I know of no way to get the project path at design-time**, and
    I do not want to bundle an image with my webcontrol DLL.

    > this.Site.DesignMode


    Thanks, that is the magical property I was hoping for.

    Justin

    **Actually, I know of a convoluted way to get the project path at
    design time: use the EnvDTE library (this is my last resort). See this
    link:
    http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1960&lngWId=10
     
    Justin, Jul 8, 2005
    #8
  9. Justin

    Justin Guest

    Re: Custom control: How to render an embedded image at design-time

    Thank you, I replied to it. I'm not exactly sure what your suggestion
    was.

    Justin
     
    Justin, Jul 8, 2005
    #9
  10. Justin

    Justin Guest

    >> You render an image the same way you render one at run-time (anchor
    tag)...but the path to the image has to be valid in design-time as
    well

    > Yes, but I know of no way to get the project path at design-time**, and

    I do not want to bundle an image with my webcontrol DLL.

    My original question was how to render an image in an ASP.NET
    webcontrol from an embedded resource, which means, I infer, a stream.
    But System.Web.UI.WebControls.Image has no way of consuming a stream.
    This is my main question -- how to display an embedded image in a
    webcontrol at design-time? Do I *have* to write the embedded image to
    the filesystem, then pass that path to the Image.ImageUrl ?

    Thank you,
    Justin
     
    Justin, Jul 8, 2005
    #10
  11. Re: Custom control: How to render an embedded image at design-time

    Check the other forum for my latest response. Basically, web controls
    shouldn't have the image embedded into the dll. As a general rule all of the
    resources associated with the control should be able to be modified by the
    site adminstrator.

    A truly extensible control, should have the ability for the user to replace
    the associated images and scripts with their own resources without
    recompiling the control.
    --
    Direct Email: Michael.Baltic@RemoveCharactersUpTo#NCMC.Com

    Staff Consultant II
    Enterprise Web Services
    Cardinal Solutions Group


    "Justin" wrote:

    > >> You render an image the same way you render one at run-time (anchor

    > tag)...but the path to the image has to be valid in design-time as
    > well
    >
    > > Yes, but I know of no way to get the project path at design-time**, and

    > I do not want to bundle an image with my webcontrol DLL.
    >
    > My original question was how to render an image in an ASP.NET
    > webcontrol from an embedded resource, which means, I infer, a stream.
    > But System.Web.UI.WebControls.Image has no way of consuming a stream.
    > This is my main question -- how to display an embedded image in a
    > webcontrol at design-time? Do I *have* to write the embedded image to
    > the filesystem, then pass that path to the Image.ImageUrl ?
    >
    > Thank you,
    > Justin
    >
    >
     
    Michael Baltic, Jul 8, 2005
    #11
  12. Justin

    Mythran Guest

    "Justin" <> wrote in message
    news:...
    >>> You render an image the same way you render one at run-time (anchor

    > tag)...but the path to the image has to be valid in design-time as
    > well
    >
    >> Yes, but I know of no way to get the project path at design-time**, and

    > I do not want to bundle an image with my webcontrol DLL.
    >
    > My original question was how to render an image in an ASP.NET
    > webcontrol from an embedded resource, which means, I infer, a stream.
    > But System.Web.UI.WebControls.Image has no way of consuming a stream.
    > This is my main question -- how to display an embedded image in a
    > webcontrol at design-time? Do I *have* to write the embedded image to
    > the filesystem, then pass that path to the Image.ImageUrl ?
    >
    > Thank you,
    > Justin
    >


    Yes, you can embed an image without writing the image to the filesystem (in
    order for the http handler to receive it). What you would do is create a
    class that inherits from HttpModule to handle a specific url sent by the
    client, then place the specific url as the img tag's src property. There
    are examples on the net on how to stream an image. Pull the image from the
    assembly manifest as a Stream using the System.Reflection namespace.
    (GetAssemblyResourceManifest or something, can't remember the exact name of
    the method of the Assembly class to use).

    HTH!

    Mythran
     
    Mythran, Jul 8, 2005
    #12
  13. Justin

    Justin Guest

    Yes, that helps a lot. Thank you very much.

    Justin
     
    Justin, Jul 8, 2005
    #13
    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. Replies:
    0
    Views:
    725
  2. Replies:
    1
    Views:
    385
    Teemu Keiski
    Jul 2, 2003
  3. Replies:
    1
    Views:
    139
  4. Justin M. Keyes

    How to render an embedded image at design-time?

    Justin M. Keyes, Jul 7, 2005, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    242
    Justin M. Keyes
    Jul 8, 2005
  5. Replies:
    1
    Views:
    348
    justncase80
    Sep 27, 2005
Loading...

Share This Page