如何构建ImageButton控件适配器(或更一般的,如何构建一个简单的控件适配器)? [英] How to build ImageButton Control Adapter (or more general, how to build a simple control adapter)?

查看:102
本文介绍了如何构建ImageButton控件适配器(或更一般的,如何构建一个简单的控件适配器)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题的灵感是我发现了ImageButton网页控件上非常烦人的默认风格( border-width:0px; )。简单的解决方案是通过向控件添加您自己的样式来覆盖它。 Style =border-width:2px;



一个简单的控制适配器,它只是在正确的地方,只是告诉控件不呈现默认的样式。$ / b
$ b

从CSSFriendly ControlAdapter项目,似乎他们正在重新创建大部分的渲染,这是对我想要做的过分 - 即只是更改渲染的默认样式。





这是可能吗?

这是一个问题,如何通过控制适配器修改默认样式的渲染,

感谢,Egil。

解决方案

有两种方法。两者都需要编写自定义控制适配器。您可以在代码中设置实际值,或者您可以根本不包括该值,然后使用CSS设置您的值。以下是您需要执行此操作的代码。

 命名空间TestApp 
{
使用System.IO ;
使用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)
{
//更改值
// value =2;

// -or-

//不包括值
// return;
}

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

public override void AddStyleAttribute(HtmlTextWriterStyle键,字符串值)
{
if(key == HtmlTextWriterStyle.BorderWidth)
{
//更改值
// value =2px;

// -or-

//不包括值
// return;
}

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

您需要向其中一个浏览器文件添加条目,例如

 < browsers& 
< browser refID =Default>
< controlAdapters>
< adapter controlType =System.Web.UI.WebControls.ImageadapterType =TestApp.ImageAdapter,TestApp/>
< / controlAdapters>
< / browser>
< / browsers>


My inspiration for this question was my discovery of the very annoying default style (border-width: 0px;) on the ImageButton web control. The simple solution is to override it by adding your own style to the control e.g. Style="border-width: 2px;".

How every, it would have been nice to just make a simple control adapter that would just step in at the right place and just tell the control not to render the default styling.

After looking a bit at the code from the CSSFriendly ControlAdapter project, it seems like they are recreating much of the rendering, which is overkill for what I want to do -- i.e. just change the default styling that is rendered out.

So the question, how to just modify the rendering of the default styles through control adapters, and leave the rest as is?

Is it even possible?

Thanks, Egil.

解决方案

There are two ways to do this. Both will require writing up a custom Control Adapter. Either you can set the actual value in code, or you can just not include the value at all and then use CSS to set your value. Here's the code you'll need to do this.

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'll need to add an entry into one of your browser files like this

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

这篇关于如何构建ImageButton控件适配器(或更一般的,如何构建一个简单的控件适配器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆