Render and Image on a Button

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Render and Image on a Button

    Greetings, all...

    I'm having some issues with extending the
    System.Web.UI.WebControls.Button. I want to add an image to the button
    that will render on the button face itself. Think of the 'Back' button
    for the IE browser - and image to the left of the text.

    Unfortunately, with what I've coded, I'm getting exactly what I would
    expect: an image to the left of the text. The problem is, the image is
    rendering off the face of the button.

    What I need to find out is how to add the new image to part of the
    base, instead of as a new control that renders before the base.

    Here's the code I've got so far:

    namespace GoofingAround
    {
    [DefaultProperty("ImageUrl"),
    ToolboxData("<{0}:TextImageButton
    runat=server></{0}:TextImageButton>")]
    public class TextImageButton : System.Web.UI.WebControls.Button,
    INamingContainer
    {
    private Image _imageUrl = new Image();
    [Bindable(false),
    Category("Appearance"),
    Description("The image to be added to the button.")]

    public string ImageUrl
    {
    get{return _imageUrl.ImageUrl;}
    set{_imageUrl.ImageUrl = value;}
    }

    protected override void Render(HtmlTextWriter writer)
    {
    _imageUrl.RenderControl(writer);
    writer.Write("&nbsp;");
    base.Render(writer);
    }
    }
    }

    The goal is to programmatically change the text based on localization
    or custom strings, as well as being able to change the image.

    Any help would be greatly appreciated!

    thanks,
    Ric

    Ric_C Guest

  2. Similar Questions and Discussions

    1. Button Component with image instead of the button
      I want to use the button component which will trigger data from an XML file to populate a combo box with data from an xml file, but rather than the...
    2. About print datagrid with image render in Flex2.0
      Hi, all, Does the Flex2 support printing the datagrid with the imag renders? I try using flexdatagrid, but the after printing, I find the image...
    3. How to render an embedded image at design-time?
      Hi, I have a custom web server control, specifically a composite control, that includes an image button. The image button takes an ImageUrl...
    4. Render image of 3D View, and load save feature
      I'm wondering if anyone has experimented with exporting an image ( .jpg or other) of a 3d view. The idea here is that the visitor could create...
    5. Image won't preview - "could not render the database"
      I am on Mac OS X.2 using Fireworks MX, and get this error message fairly regularly: "Could not render the database. The file is already open in...
  3. #2

    Default RE: Render and Image on a Button

    You could make the button a composite control. Nest the image and text areas
    inside of a container tag (div, span, table) and create a click event for the
    entire group.

    The only other option I know of is to use GDI+ to render a new image with
    your text and image combined into one image. You could then use this image
    for your button.

    Neither solution is trivial, but both are very doable with some effort.

    Actually, ASPNETPRO magazine had a really cool article on this a couple of
    months ago.....
    --
    Staff Consultant II
    Enterprise Web Services
    Cardinal Solutions Group

    Future Business Model
    Loan Origination Services
    National City Mortgage


    "Ric_C" wrote:
    > Greetings, all...
    >
    > I'm having some issues with extending the
    > System.Web.UI.WebControls.Button. I want to add an image to the button
    > that will render on the button face itself. Think of the 'Back' button
    > for the IE browser - and image to the left of the text.
    >
    > Unfortunately, with what I've coded, I'm getting exactly what I would
    > expect: an image to the left of the text. The problem is, the image is
    > rendering off the face of the button.
    >
    > What I need to find out is how to add the new image to part of the
    > base, instead of as a new control that renders before the base.
    >
    > Here's the code I've got so far:
    >
    > namespace GoofingAround
    > {
    > [DefaultProperty("ImageUrl"),
    > ToolboxData("<{0}:TextImageButton
    > runat=server></{0}:TextImageButton>")]
    > public class TextImageButton : System.Web.UI.WebControls.Button,
    > INamingContainer
    > {
    > private Image _imageUrl = new Image();
    > [Bindable(false),
    > Category("Appearance"),
    > Description("The image to be added to the button.")]
    >
    > public string ImageUrl
    > {
    > get{return _imageUrl.ImageUrl;}
    > set{_imageUrl.ImageUrl = value;}
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > _imageUrl.RenderControl(writer);
    > writer.Write(" ");
    > base.Render(writer);
    > }
    > }
    > }
    >
    > The goal is to programmatically change the text based on localization
    > or custom strings, as well as being able to change the image.
    >
    > Any help would be greatly appreciated!
    >
    > thanks,
    > Ric
    >
    >
    Michael Baltic Guest

  4. #3

    Default Re: Render and Image on a Button

    Ric_C wrote:
    > Greetings, all...
    >
    > I'm having some issues with extending the
    > System.Web.UI.WebControls.Button. I want to add an image to the button
    > that will render on the button face itself. Think of the 'Back' button
    > for the IE browser - and image to the left of the text.
    >
    If buttons in .net were actual HTML buttons (<button>thelabel</button>)
    instead of the unflexible <input type=button> then you could easily put
    anything you want inside the button tags.

    Don't know wheter it would be possible to extend the Btton control to
    that extend?


    Jens
    Jens Ansorg Guest

  5. #4

    Default Re: Render and Image on a Button

    In message <#RxIr98gFHA.1948@TK2MSFTNGP12.phx.gbl>, Jens Ansorg
    <jens@ja-web.de> writes
    >Ric_C wrote:
    >> Greetings, all...
    >> I'm having some issues with extending the
    >> System.Web.UI.WebControls.Button. I want to add an image to the button
    >> that will render on the button face itself. Think of the 'Back' button
    >> for the IE browser - and image to the left of the text.
    >>
    >
    >If buttons in .net were actual HTML buttons (<button>thelabel</button>)
    >instead of the unflexible <input type=button> then you could easily put
    >anything you want inside the button tags.
    >
    >Don't know wheter it would be possible to extend the Btton control to
    >that extend?
    It doesn't look worth it compared to writing your own rendered button
    control derived from WebControl. It's hardly rocket science.

    --
    Steve Walker
    Steve Walker Guest

  6. #5

    Default Re: Render and Image on a Button

    Well, Steve...

    Unfortunately, I neither work for JPL, nor do I have a Chemistry
    degree, so I'm definitely not a rocket scientist.

    I've tried every what I know (in my somewhat limited scope of
    knowledge) to get the controls to render properly. I've tried adding
    the controls overriding OnPreRender and also overriding Render as well,
    but I'm still getting exactly the same result: image and label text
    rendering to the left of the button itself. Now, I could probably play
    with stupid CSS tricks to move the various elements onto the face, but
    that seems more like a kludge than a solution.

    So, do you have any suggestions that could point me down the road to
    solving this problem? Unfortunately, I don't have "big book stores"
    anywhere nearby, so I'm left with what I can dig up on Google.

    I appreciate any help you might want to offer.

    Take care,
    Ric

    Ric_C Guest

  7. #6

    Default Re: Render and Image on a Button

    Inherit from the button control and override the render method to use the
    button tag instead of the input tag. Thats the easiest fix.
    --
    Direct Email: [email]Michael.Baltic@RemoveCharactersUpTo#NCMC.Com[/email]

    Staff Consultant II
    Enterprise Web Services
    Cardinal Solutions Group


    "Ric_C" wrote:
    > Well, Steve...
    >
    > Unfortunately, I neither work for JPL, nor do I have a Chemistry
    > degree, so I'm definitely not a rocket scientist.
    >
    > I've tried every what I know (in my somewhat limited scope of
    > knowledge) to get the controls to render properly. I've tried adding
    > the controls overriding OnPreRender and also overriding Render as well,
    > but I'm still getting exactly the same result: image and label text
    > rendering to the left of the button itself. Now, I could probably play
    > with stupid CSS tricks to move the various elements onto the face, but
    > that seems more like a kludge than a solution.
    >
    > So, do you have any suggestions that could point me down the road to
    > solving this problem? Unfortunately, I don't have "big book stores"
    > anywhere nearby, so I'm left with what I can dig up on Google.
    >
    > I appreciate any help you might want to offer.
    >
    > Take care,
    > Ric
    >
    >
    Michael Baltic Guest

  8. #7

    Default Re: Render and Image on a Button


    The first thing I'd do would be to write some plain HTML to achieve the
    desired effect.

    You could then either, in order of decreasing control but increasing
    ease:

    * override Render to emit your HTML at runtime (so a completely rendered
    control) You'd obviously need to modify the HTML to reflect the text and
    image URL properties, and you'd need to create and wire up a click
    event. If you intended using the VS designer's absolute positioning,
    you'd have to support that.

    * (more simply) add a literal control to the WebControl's controls
    collection containing your HTML. Your HTML will end up inside a span
    which will provide absolute positioning support. You'll still have to
    wire up the click event manually, though.

    * (More simply still) Create the effect you want by adding controls to
    the webcontrol's control collection and positioning them appropriately


    I don't think subclassing button is going to get what you want, because
    it doesn't render as anything approaching what you are after. You could
    be really sneaky and intercept and modify the rendered output after the
    event: this is some code I use for debugging troublesome controls, I
    guess you can see the potential for fiddling things:

    protected override void Render(HtmlTextWriter writer)
    {
    System.IO.StringWriter target = new
    System.IO.StringWriter();
    HtmlTextWriter intercept = new HtmlTextWriter(target);
    base.Render (intercept);
    string html = target.ToString();
    writer.Write(html);
    }

    I really wouldn't advise getting into that though, it breaks the base
    class's encapsulation and makes you vulnerable to MS changing the
    implementation.

    Bottom line is that if you can do it in HTML, you can write a control to
    do it. Worst case scenario is that you might have to write a rendered
    control, which is not difficult but can be fiddly.


    --
    Steve Walker
    Steve Walker Guest

  9. #8

    Default Re: Render and Image on a Button

    I found a simple solution to this problem. I used an HtmlButton instead, and overwrote the inner HTML.

    HtmlButton cmdEdit = new HtmlButton();
    cmdEdit.InnerHtml = "<img src='images/edititem.gif' align='middle' />&nbsp;Edit";
    cmdEdit.Style.Add("vertical-align", "bottom");
    CigarDoug Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139