服务器标记不能包含<%...%GT;结构体 [英] Server tags cannot contain <% ... %> constructs

查看:153
本文介绍了服务器标记不能包含<%...%GT;结构体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CDN我的网站上的图像。
问题是,有时我必须如的ImageButton服务器控件,我想,以充分提取CDN的路径使用的类。为此我试图做:

I am trying to use CDN for my images on the website. Problem is, sometimes I have server controls such as ImageButton, and I would like to use a class in order to fully extract the path of the CDN. for that purpose I tried doing:

<asp:ImageButton runat="server" OnClick="Agree" ImageUrl="<%=ResourceManager.GetImageCDN("iagree.png")%>" />

和我得到的标题错误。

只有当我使用&LT;%#将工作(且仅当我的数据绑定)。
我怎样才能做到这一点很容易?我怎么可以把CDN code在我的标记code?

Only if I'm using <%# it will work (and only if I an databinding). How can I do this easily? how can I place CDN code on my markup code?

谢谢!

推荐答案

有四个选项(除了&LT;%#%&GT; 风格绑定,我不推荐):

There are four options (in addition to "<%# %>" style databinding, which I don't recommend):


  1. 设置在code背后的价值。这种膨胀的ViewState,当然需要控制的每一个实例code的变化。

  2. 使用自定义的前pressionBuilder。这不膨胀ViewState的,但它确实需要改变所有的标记。

  3. 使用一个控件适配改变无处不在应用控制的行为;例如,通过修改ImageUrl属性之前,控件呈现。可以在不影响ViewState中进行。

  4. 使用一个类,从的ImageButton类继承,与标签映射结合使用该类代替了原来无处不在您的应用程序,并消除改变你的标记需求。可以在不影响ViewState中进行。

最好的选择取决于你的应用程序的要求,但如果你想改变站点范围内的,我通常preFER控制适配器。

The best option depends on your app's requirements, but I usually prefer a control adapter if you want to make the changes site-wide.

下面是一个例子,如果它可以帮助:

Here's an example, in case it helps:

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

namespace Sample
{
    public class ImageButtonControlAdapter : WebControlAdapter
    {
        protected override void BeginRender(HtmlTextWriter writer)
        {
            ImageButton image = this.Control as ImageButton;
            if ((image != null) && !String.IsNullOrEmpty(image.ImageUrl))
            {
                //
                // Decide here which objects you want to change
                //
                if (!image.ImageUrl.StartsWith("http") && 
                    !image.ImageUrl.StartsWith("data:"))
                {
                    image.ImageUrl = ResourceManager.GetImageCDN(image.ImageUrl);
                }
            }
            base.BeginRender(writer);
        }
    }
}

在App_Browers以下项配置到你的应用程序/ adapter.browser:

Configure it into your app with the following entry in App_Browers/adapter.browser:

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.ImageButton"
               adapterType="Sample.ImageButtonControlAdapter" />
    </controlAdapters>
  </browser>
</browsers>

您的标记是:

<asp:ImageButton runat="server" OnClick="Agree" ImageUrl="iagree.png" />

酷吧?

这篇关于服务器标记不能包含&LT;%...%GT;结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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