c# - 在页面中异步加载用户控件

标签 c# asp.net asynchronous user-controls updatepanel

我创建了一个页面来显示更新面板内的多个用户控件。一些用户控件将加载得更快,而一些可能需要更长的时间来加载。现在,当页面加载时,它会等待所有用户控件加载完毕,然后才显示页面。但是我想用每个用户控件的加载器图像异步加载用户控件,这样轻量级的用户控件就可以轻松加载,而无需等待较重的用户控件。

请帮我找到解决办法。


我已经使用上述方法成功地将用户控件加载到我的页面中。但是现在我在加载包含 ajax 控件(例如选项卡容器、日历扩展器等)的用户控件时遇到困难。

这个问题有解决办法吗

最佳答案

您会遇到很多问题:ViewState、需要表单标签的控件、回发将不起作用,但如果您使用纯 View 的控件执行此操作,它将正常工作。

脚本:

//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]
    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");

    }  

渲染方法:

    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)
        {
        }
    }

关于c# - 在页面中异步加载用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295995/

相关文章:

c# - 检查两个集合是否相等

ASP.NET、IIS 和 COM

asp.net - 如何创建基于 .NET Core 而不是完整框架的 ASP.NET 项目?

asp.net - 如何理解 Visual Studio 2008 中的动态 HTML?

python - 有没有支持异步请求的Python ElasticSearch客户端?

c# - .NET 中的不可知连接处理程序

C# - 更改 DataGridView 中每行单元格值的最有效方法

asynchronous - 如何快速停止 Camel 中的 seda

c# - 3D 图形 - 矩阵数学不起作用

ios - findObjectsInBackgroundWithBlock : gets data from Parse, 但数据只存在于 block 内