在页面的用户控件的异步加载 [英] Asynchronous loading of user controls in a page

查看:83
本文介绍了在页面的用户控件的异步加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个页面来显示更新面板内的多个用户控件。有些用户控件将被装载速度更快,有些可能需要较长时间来加载。现在,在加载页面时,它会等待所有的用户控件得到加载后,才显示该页面。但我想为他们每个人的形象装载机异步加载的用户控件,使得重量轻的用户控件将获得无需等待较重的轻松装入。

I have created a page to display multiple user controls inside update panels. Some user controls will get loaded faster and some may take a longer time to load. Now when the page loads, it waits for all the user controls to get loaded and display the page only after that. But I want to load the user controls asynchronously with a loader image for each of them so that the light weight user controls will get loaded easily without waiting for the heavier ones.

请帮我找到一个解决方案。

Please help me to find a solution.

我已成功加载使用上述方法用户控件到我的网页。但是现在我面临着包含AJAX控件,如标签的容器,压延机扩展等。

I have successfully loaded the user control into my page using the above method. However now i am facing difficulty in loading usercontrols containing ajax controls such as tab container, calender extender etc..

有任何工作围绕这个问题

Is there any work around for this problem

推荐答案

您会遇到一堆问题:ViewState中,控制需要表单标签,回发是行不通的,但如果你是一个控制这样做纯粹是一个查看它将很好地工作。

You will run into a bunch of issues: ViewState, controls needing form tags, postbacks will not work but if you are doing this with a control that is purely a View it will work fine.

脚本:

//use .ready() or pageLoad() and pass params etc if you need to
$.ajax({
    type: 'POST',
    url: 'Default.aspx/GetControlViaAjax',
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {  
      $('#yourdiv').html(data.d);
    }
 });

的WebMethod:

    [WebMethod]
    public static string GetControlViaAjax()
    {
        //example public properties, send null if you don't have any
        Dictionary<string, object> d = new Dictionary<string, object>();
        d.Add("CssClass", "YourCSSClass");
        d.Add("Title", "Your title");
        return RenderUserControl("/yourcontrol.ascx", true, d, null, null);
        //use this one if your controls are compiled into a .dll
        //return RenderUserControl(null, true, d, "Com.YourNameSpace.UI", "AwesomeControl");

    }  

Render方法:

    private static string RenderUserControl(string path, bool useFormLess,
         Dictionary<string, object> controlParams, string assemblyName, string controlName )
    {

        Page pageHolder = null;
        if (useFormLess)
        {
            pageHolder = new FormlessPage() { AppRelativeTemplateSourceDirectory = HttpRuntime.AppDomainAppVirtualPath }; //needed to resolve "~/"
        }
        else
        {
            pageHolder = new Page() { AppRelativeTemplateSourceDirectory = HttpRuntime.AppDomainAppVirtualPath };
        }

        UserControl viewControl = null;

        //use path by default
        if(String.IsNullOrEmpty(path))
        {    
            //load assembly and usercontrol when .ascx is compiled into a .dll        
            string controlAssemblyName = string.Format("{0}.{1},{0}", assemblyName, controlName );

            Type type = Type.GetType(controlAssemblyName);            
            viewControl = (UserControl)pageHolder.LoadControl(type, null);
        }
        else
        {
            viewControl = (UserControl)pageHolder.LoadControl(path);    

        }              

        viewControl.EnableViewState = false;

        if (controlParams != null && controlParams.Count > 0)
        {
            foreach (var pair in controlParams)
            {
                Type viewControlType = viewControl.GetType();
                PropertyInfo property =
                   viewControlType.GetProperty(pair.Key);

                if (property != null)
                {
                    property.SetValue(viewControl, pair.Value, null);
                }
                else
                {
                    throw new Exception(string.Format(
                       "UserControl: {0} does not have a public {1} property.",
                       path, pair.Key));
                }
            }
        }

        if (useFormLess)
        {                
            pageHolder.Controls.Add(viewControl);
        }
        else
        {
            HtmlForm form = new HtmlForm();
            form.Controls.Add(viewControl);
            pageHolder.Controls.Add(form);
        }
        StringWriter output = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, output, false);
        return output.ToString();
    }

FormlessPage类:

    public class FormlessPage : Page
    {
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    }

这篇关于在页面的用户控件的异步加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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