ASP.NET Custom Control Adapter not firing

痴心易碎 提交于 2020-01-05 03:57:09

问题


I am trying to write a simple ControlAdapter below is a very simplified case, but I still can't seem to get it to work. In my simplified case I just want to write something out before my control is rendered.

I defined my control adapter as such:

using System.Web.UI;
using System.Web.UI.WebControls.Adapters;

namespace Test.Web.Common.Controls.ControlAdapters {

  public class RadioBtnStyleAdapter : WebControlAdapter {


    protected override void BeginRender(HtmlTextWriter writer) {

      writer.WriteLine("<!--- CONTROL ADAPTER -->");
      writer.WriteLine("<div><b>TEST</b></div");

      base.BeginRender(writer);
    }
  }
}
  • I then added an App_Browsers folder under my Test.Web project.
  • I then added a default.browser file under App_Browsers

And my default.browsers looks like

<browsers>
  <browser id="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.RadioButton" adapterType="Test.Web.Common.Controls.ControlAdapters.RadioBtnStyleAdapter"></adapter>
    </controlAdapters>
  </browser>
</browsers>

It is my understanding that should be enough, and the framework should pick up the the adapter, however it never actually seems to fire.

Any suggestions about what I could be missing?

Update

I am able get it to work if I programmaticaly add the adapter on Page_Init like so

HttpContext.Current.Request.Browser.Adapters["System.Web.UI.WebControls.RadioButton"] = "Test.Web.Common.Controls.ControlAdapters.RadioBtnStyleAdapter";

But nothing I have read show this as a required step, so I am not convinced this is correct.


回答1:


You need to change the id attribute of the browser element to refID as follows. The id attribute is used if you would like to give it a unique name, when mapping/identifying a particular browser, and refID is used when associating new capabilities (such as Control Adapters) with an existing browser definition. And that's exactly what you want to do.

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.RadioButton" adapterType="Test.Web.Common.Controls.ControlAdapters.RadioBtnStyleAdapter"></adapter>
    </controlAdapters>
  </browser>
</browsers>


来源:https://stackoverflow.com/questions/33551179/asp-net-custom-control-adapter-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!