ControlAdapter中的渲染控件 [英] Render Control in ControlAdapter

查看:49
本文介绍了ControlAdapter中的渲染控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我发布的

适配器

添加我的控制适配器并且不进行任何更改,我得到了意外的行为:

我尝试添加调用以使基本控件呈现为

  RenderChildren(writer);RenderContents(writer); 

但是我似乎永远无法获得要控制的基础来渲染.

我最终想要的是这样的:

 受保护的重写void Render(HtmlTextWriter writer){writer.WriteBeginTag("div");writer.AddAttribute("class","wrapper");//渲染现有控件//Control.RenderControl();writer.WriteEndTag("div");} 

我的问题是,这是否有可能,如果我这样做显然是错误的话.任何建议或帮助,将不胜感激.

解决方案

您需要使用ControlAdapter类而不是WebControlAdapter,请参见下文.使用WebControlAdapter的问题在于,它通过调用RenderBeginTag,RenderContents和RenderEndTag来呈现控件. https://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.adapters.webcontroladapter%28v=vs.110%29.aspx

但是,对于此复选框,RenderContents不是生成内容的内容,而是Render方法.使用ControlAdapter类时,将执行控件的Render方法. https://msdn.microsoft.com/zh-CN/library/system.web.ui.adapters.controladapter%28v=vs.110%29.aspx

 公共类RadioButtonAdapter:System.Web.UI.Adapters.ControlAdapter {受保护的重写void BeginRender(HtmlTextWriter writer){writer.Write(< div class = \" wrapper \>");base.BeginRender(writer);}受保护的覆盖无效EndRender(HtmlTextWriter writer){base.EndRender(writer);writer.Write(</div>");}} 

This is some what related to my earlier question posted, but not exactly. I finally was able to get my control adapter to fire, however I cannot get it to work as I expected.

The whole reason I would like to use a control adapter is to add additional markup to system controls such as a RadioButton. I am using another framework that that expect this.

If we assume a RadioButton without any changes would render like

<input id="MainContent_RadioButton1" type="radio" name="ctl00$MainContent$RadioButton1" value="RadioButton1" /><label for="MainContent_RadioButton1">My Radio Button</label>

I would like to change the markup to look like

<div class="wrapper">
<input id="MainContent_RadioButton1" type="radio" name="ctl00$MainContent$RadioButton1" value="RadioButton1" /><label for="MainContent_RadioButton1">My Radio Button</label>
</div>

This is a really simplified example, so I am not looking for suggestions of sub classing my controls, or adding a panel to my page with a class.

In my Page-Init I have

protected void Page_Init(object sender, EventArgs e)
{
  HttpContext.Current.Request.Browser.Adapters["System.Web.UI.WebControls.RadioButton"] =
    "ControlAdapterTest.Controls.ControlAdaptors.RadioButtonAdapter";
}

My Control Adapter looks like

public class RadioButtonAdapter : WebControlAdapter {

    public new RadioButton Control => (RadioButton) base.Control;

    #region Overrides of WebControlAdapter

    protected override void Render(HtmlTextWriter writer)
    {
      base.Render(writer);
    }

    #endregion
  }
}

No adapter

Without using the control adapter my radio buttons obviously render fine.

Adapter

Adding my control adapter and changing nothing else, I get unexpected behavior:

I have tried adding calls to make the base control render such as

  RenderChildren(writer);
  RenderContents(writer);

but I can never seem to get the base to control to render.

Ultimately what I would like to have is something like:

protected override void Render(HtmlTextWriter writer)
{

  writer.WriteBeginTag("div");
  writer.AddAttribute("class", "wrapper");
  //Render existing control
  //Control.RenderControl();
  writer.WriteEndTag("div");

}

My questions is, is this even possible, if so am I doing something obviously wrong. Any suggestions or help would be appreciated.

解决方案

You need to use the ControlAdapter class instead of the WebControlAdapter, see further below. The problem with using the WebControlAdapter is that it renders the control by calling the RenderBeginTag, RenderContents and RenderEndTag. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.adapters.webcontroladapter%28v=vs.110%29.aspx

However in the case of the checkbox, the RenderContents isn't what generates the contents, instead it is the Render method. And the Render method of the control is executed when using the ControlAdapter class. https://msdn.microsoft.com/en-us/library/system.web.ui.adapters.controladapter%28v=vs.110%29.aspx

public class RadioButtonAdapter : System.Web.UI.Adapters.ControlAdapter {
    protected override void BeginRender(HtmlTextWriter writer) {
        writer.Write("<div class=\"wrapper\">");
        base.BeginRender(writer);
    }
    protected override void EndRender(HtmlTextWriter writer) {
        base.EndRender(writer);
        writer.Write("</div>");
    }
}

这篇关于ControlAdapter中的渲染控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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