zoukankan      html  css  js  c++  java
  • Creating a generic Web Parts for hosting ASP.NET User Controls

    In my previous post, I share a Calendar Web Part solution in C# for SharePoint Portal Server 2003, which demonstrates  how to mix web parts and ASP.Net User Controls(.ASCX). Over there, you are forced to hardcode the name and path to a .ASCX file into the web part implementation. The following listing shows a part of codes.

    // field to hold reference to user control object
    private System.Web.UI.Control _innerUserControl;
    // instantiate user control in CreateChildControls
    protected override void CreateChildControls()
    {
    _innerUserControl = this.Page.LoadControl("/wpresources/WebUserControlMonth.ascx");
                this.Controls.Add(_innerUserControl);
    }
    // render user control
    protected override void RenderWebPart(HtmlTextWriter output)
    {
                // output.Write(SPEncode.HtmlEncode(Text));
                this.EnsureChildControls();
                _innerUserControl.RenderControl(output);
    }


    There is another solution by Barry Kouda regarding how to create a generic Web Parts for hosting ASP.Net User Control. It's really usefully to design web parts. The following contents show the main method of loading an ASP.NET user control into a Web Part and making it generic.

    The approach involves using a shared Web Part property to track the name and path of the .ASCX file. Examine the following code:

    // field to hold path to .ASCX file
    private string _path;
        // Web part property to make path configurable
        [ WebPartStorage(Storage.Personal), Category("Miscellaneous"),
          FriendlyName("User Control Path"),
          Description("Path to .ascx file"),
          Browsable(true), DefaultValue("")
        ]
        public string ControlFile  {
          get { return _path; }
          set { _path = value; }
        }
        // field to hold reference to user control object
        Control uc;
     
        // instantiate user control in CreateChildControls
        protected override void CreateChildControls() {
          this.Controls.Clear();
          uc = this.Page.LoadControl(_path);   
          this.Controls.Add(uc);
        }
         // render user control
        protected override void RenderWebPart(HtmlTextWriter output) {
          uc.RenderControl(output);
        }

    Now, the file name and path to the .ASCX file are configurable. It’s possible for any SharePoint developer, Web designer or content manager to the UsercontrolHost web part to a web part page and configure the User Control File property through configuration functionality.

  • 相关阅读:
    利用IIS应用请求转发ARR实现IIS和tomcat整合共用80端口
    Application Request Route实现IIS Server Farms集群负载详解
    jQuery插件之ajaxFileUpload
    百度上传组件
    jQuery选择器总结 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法
    如何使 WebAPI 自动生成漂亮又实用在线API文档
    Swagger+AutoRest 生成web api客户端(.Net)
    小程序开发的40个技术窍门,纯干货!
    为你下一个项目准备的 50 个 Bootstrap 插件
    In-Memory:内存数据库
  • 原文地址:https://www.cnblogs.com/rickie/p/27013.html
Copyright © 2011-2022 走看看