ascx用户控件。

解决方案 »

  1.   

      当你激发一个服务器控件的事件时,它动态加载,可以先在放一个 placeholder 
      usercontrol  col=(usercontrol )this.page.loadcontrol(yourcontrol.ascx)
      palceholder.control.add(col);
    这样就加载了  
     
      

  2.   

    to:ab83328523不是说这个。而是问Page到底在做什么。为什么要LoadControl,直接Controls.Add为什么不行。
      

  3.   

    说起来就是通过制定的路径,创建控件的实例反编译下,两个相关函数:
    private BuildResult GetVPathBuildResultInternal(VirtualPath virtualPath, bool noBuild, bool allowCrossApp, bool allowBuildInPrecompile)
    {
        if (this._compilationStage == CompilationStage.TopLevelFiles)
        {
            throw new HttpException(SR.GetString("Too_early_for_webfile", new object[] { virtualPath }));
        }
        BuildResult vPathBuildResultFromCacheInternal = this.GetVPathBuildResultFromCacheInternal(virtualPath);
        if (vPathBuildResultFromCacheInternal == null)
        {
            if (noBuild)
            {
                return null;
            }
            this.ValidateVirtualPathInternal(virtualPath, allowCrossApp, false);
            Util.CheckVirtualFileExists(virtualPath);
            if (this.IsNonUpdatablePrecompiledApp && !allowBuildInPrecompile)
            {
                throw new HttpException(SR.GetString("Cant_update_precompiled_app", new object[] { virtualPath }));
            }
            bool gotLock = false;
            try
            {
                CompilationLock.GetLock(ref gotLock);
                vPathBuildResultFromCacheInternal = this.GetVPathBuildResultFromCacheInternal(virtualPath);
                if (vPathBuildResultFromCacheInternal != null)
                {
                    return vPathBuildResultFromCacheInternal;
                }
                VirtualPathSet data = CallContext.GetData("CircRefChk") as VirtualPathSet;
                if (data == null)
                {
                    data = new VirtualPathSet();
                    CallContext.SetData("CircRefChk", data);
                }
                if (data.Contains(virtualPath))
                {
                    throw new HttpException(SR.GetString("Circular_include"));
                }
                data.Add(virtualPath);
                try
                {
                    this.EnsureTopLevelFilesCompiled();
                    return this.CompileWebFile(virtualPath);
                }
                finally
                {
                    data.Remove(virtualPath);
                }
            }
            finally
            {
                if (gotLock)
                {
                    CompilationLock.ReleaseLock();
                }
            }
        }
        return vPathBuildResultFromCacheInternal;

    private Control LoadControl(IWebObjectFactory objectFactory, VirtualPath virtualPath, Type t, object[] parameters)
    {
        BuildResultCompiledType type = null;
        BuildResultNoCompileUserControl control = null;
        PartialCachingAttribute cachingAttribute;
        if (objectFactory != null)
        {
            type = objectFactory as BuildResultCompiledType;
            if (type != null)
            {
                t = type.ResultType;
                Util.CheckAssignableType(typeof(UserControl), t);
            }
            else
            {
                control = (BuildResultNoCompileUserControl) objectFactory;
            }
        }
        else if (t != null)
        {
            Util.CheckAssignableType(typeof(Control), t);
        }
        if (t != null)
        {
            cachingAttribute = (PartialCachingAttribute) TypeDescriptor.GetAttributes(t)[typeof(PartialCachingAttribute)];
        }
        else
        {
            cachingAttribute = control.CachingAttribute;
        }
        if (cachingAttribute == null)
        {
            Control control2;
            if (objectFactory != null)
            {
                control2 = (Control) objectFactory.CreateInstance();
            }
            else
            {
                control2 = (Control) HttpRuntime.CreatePublicInstance(t, parameters);
            }
            UserControl control3 = control2 as UserControl;
            if (control3 != null)
            {
                if (virtualPath != null)
                {
                    control3.TemplateControlVirtualPath = virtualPath;
                }
                control3.InitializeAsUserControl(this.Page);
            }
            return control2;
        }
        HashCodeCombiner combinedHashCode = new HashCodeCombiner();
        if (objectFactory != null)
        {
            combinedHashCode.AddObject(objectFactory);
        }
        else
        {
            combinedHashCode.AddObject(t);
        }
        if (!cachingAttribute.Shared)
        {
            this.AddStackContextToHashCode(combinedHashCode);
        }
        string combinedHashString = combinedHashCode.CombinedHashString;
        return new PartialCachingControl(objectFactory, t, cachingAttribute, "_" + combinedHashString, parameters);
    }