如何使用替代控制内部ASP.Net服务器控件? [英] How to use ASP.Net server controls inside of Substitution control?

查看:161
本文介绍了如何使用替代控制内部ASP.Net服务器控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我们在Substitution控件使用应该返回字符串,所以怎么可能使用的方法一<一href=\"http://weblogs.asp.net/scottgu/archive/2006/11/28/tip-trick-implement-donut-caching-with-the-asp-net-2-0-output-cache-substitution-feature.aspx\">donut在该组织应该呈现服务器端服务器控件Web窗体缓存?结果
例如LoginView控件?

while the method we use in Substitution control should return strings, so how is it possible to use a donut caching in web forms on a server control which should be rendered server side?
for example Loginview control?

推荐答案

更新
现在,这是一个完全工作的例子。有几件事情发生在这里:

UPDATE This is now a fully working example. There a few things happening here:


  1. 使用回拨替代控制来呈现你的需要。UserControl的输出

  2. 使用了覆盖VerifyRenderingInServerForm和EnableEventValidation被抛出时,用户控件包含需要一个表单标签或事件验证服务器控件以加载控件以prevent错误自定义页面类。

这里的标记:

<asp:Substitution runat="server" methodname="GetCustomersByCountry" />

这里的回调

public string GetCustomersByCountry(string country)
{
   CustomerCollection customers = DataContext.GetCustomersByCountry(country);

    if (customers.Count > 0)
        //RenderView returns the rendered HTML in the context of the callback
        return ViewManager.RenderView("customers.ascx", customers);
    else
        return ViewManager.RenderView("nocustomersfound.ascx");
}

这里的辅助类来呈现用户控件

public class ViewManager
{
    private class PageForRenderingUserControl : Page
    {
        public override void VerifyRenderingInServerForm(Control control)
        { /* Do nothing */ }

        public override bool EnableEventValidation
        {
            get { return false; }
            set { /* Do nothing */}
        }
    }

    public static string RenderView(string path, object data)
    {
        PageForRenderingUserControl pageHolder = new PageForUserControlRendering();
        UserControl viewControl = (UserControl) pageHolder.LoadControl(path);

        if (data != null)
        {
            Type viewControlType = viewControl.GetType();
            FieldInfo field = viewControlType.GetField("Data");
            if (field != null)
            {
                field.SetValue(viewControl, data);
            }
            else
            {
                throw new Exception("ViewFile: " + path + "has no data property");
            }
        }

        pageHolder.Controls.Add(viewControl);
        StringWriter result = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, result, false);
        return result.ToString();
    }
}

请参阅以下相关问题:

这篇关于如何使用替代控制内部ASP.Net服务器控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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