How do I build an ImageButton Control Adapter (or more generally, how do I build a simple control adapter)?

My inspiration for this question was my discovery of the very annoying default ( border-width: 0px;

) style in the ImageButton web control. An easy solution is to override it by adding your own style to the control eg. Style="border-width: 2px;"

...

However, it would be nice to just create a simple control adapter that just goes into the right place and just tells the control not to display the default style.

After looking a bit at the code from the CSSFriendly ControlAdapter project, it seems that they recreate most of the rendering, which is overkill for what I want to do, i.e. just change the style that is being exposed.

So the question is, how can you simply change the default rendering of styles using the control adapters and leave the rest as they are?

Is it possible?

Thanks, Egil.

0


source to share


1 answer


There are two ways to do this. Both will require you to write your own management adapter. Either you can set the actual value in your code, or you just can't include the value at all and then use CSS to set your value. Here is the code you will need to do.

namespace TestApp
{
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.Adapters;

    public class ImageAdapter : ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(new RewriteImageHtmlTextWriter(writer));
        }

        public class RewriteImageHtmlTextWriter : HtmlTextWriter
        {
            public RewriteImageHtmlTextWriter(TextWriter writer)
                : base(writer)
            {
                InnerWriter = writer;
            }

            public RewriteImageHtmlTextWriter(HtmlTextWriter writer)
                : base(writer)
            {
                InnerWriter = writer.InnerWriter;
            }

            public override void AddAttribute(HtmlTextWriterAttribute key, string value, bool fEncode)
            {
                if (key == HtmlTextWriterAttribute.Border)
                {
                    // change the value
                    //value = "2";

                    // -or-

                    // don't include the value
                    //return;
                }

                base.AddAttribute(key, value, fEncode);
            }

            public override void AddStyleAttribute(HtmlTextWriterStyle key, string value)
            {
                if (key == HtmlTextWriterStyle.BorderWidth)
                {
                    // change the value
                    //value = "2px";

                    // -or-

                    // don't include the value
                    //return;
                }

                base.AddStyleAttribute(key, value);
            }
        }
    }
}

      



Then you need to add an entry to one of your browser files, for example

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.Image" adapterType="TestApp.ImageAdapter, TestApp" />
    </controlAdapters>
  </browser>
</browsers>

      

+4


source







All Articles