Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在页面中异步加载用户控件_C#_Asp.net_Asynchronous_User Controls_Updatepanel - Fatal编程技术网

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

C# 在页面中异步加载用户控件,c#,asp.net,asynchronous,user-controls,updatepanel,C#,Asp.net,Asynchronous,User Controls,Updatepanel,我创建了一个页面,在更新面板中显示多个用户控件。某些用户控件的加载速度更快,而某些控件的加载时间可能更长。现在,当页面加载时,它会等待所有用户控件加载,然后才显示页面。但是我想异步加载用户控件,并为每个控件加载一个加载程序映像,这样轻量级用户控件就可以轻松加载,而无需等待较重的控件 请帮我找到解决办法 我已使用上述方法成功地将用户控件加载到我的页面中。但是现在我在加载包含ajax控件的usercontrols时遇到了困难,比如tab容器、日历扩展程序等 是否有解决此问题的方法您将遇到一系列问题

我创建了一个页面,在更新面板中显示多个用户控件。某些用户控件的加载速度更快,而某些控件的加载时间可能更长。现在,当页面加载时,它会等待所有用户控件加载,然后才显示页面。但是我想异步加载用户控件,并为每个控件加载一个加载程序映像,这样轻量级用户控件就可以轻松加载,而无需等待较重的控件

请帮我找到解决办法


我已使用上述方法成功地将用户控件加载到我的页面中。但是现在我在加载包含ajax控件的usercontrols时遇到了困难,比如tab容器、日历扩展程序等


是否有解决此问题的方法

您将遇到一系列问题:ViewState、需要表单标记的控件、回发将不起作用,但如果您使用纯视图的控件来执行此操作,则效果会很好

脚本:

//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();
    }
WebMethod:

//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();
    }

用户控件可能不是正确的答案,因为它们是用于服务器端合成的。您可能需要服务器上的一些独立组件(从独立服务的意义上说)来提供所需的html片段,以便您可以使用javascript请求它们,并将结果推到更大的页面中。

上面的代码给了我一个错误,如下所示。。非静态字段、方法或属性“System.Web.UI.TemplateControl.LoadControl(System.Type,object[])”需要对象引用。渲染方法应写入何处?在页面中还是在用户控件中?另外,将数据填入usercontrol的代码在哪里?我对用户控件非常陌生。因此,如果你能多解释一下上述方法,那就更好了。。非常感谢。如果你只是在学习如何编写用户控件,这是非常高级的。有两个查询。1.属性csclass和title的用途是什么。2.当我把整个内容放在.cs文件中时,我得到了一个错误“执行处理程序无格式页面的子请求时出错”。它发生在HttpContext.Current.Server.Execute行上(pageHolder,output,false);请告诉我我做错了什么嗨。。我已使用上述方法成功地将用户控件加载到我的页面中。但是现在我在加载包含ajax控件的usercontrols时遇到了困难,比如tab容器、日历扩展程序等。。是否有解决此问题的方法请查看这些声称可以解决此问题的有用链接:--