sps2003 使用 user control开发webpart

正文:
1.创建UCMine.ascx,并且加入到webpart项目中;
2.将UCMine.ascx文件放到 sps虚目录对应的物理路径下的wpresource/uc/UCMine.ascx;
3.在webpart代码中:
protected override void CreateChildControls()
{
    //实例化对象
    UCMine ucMine = (UCMine)this.Page.LoadControl("/wpresources/uc/ucmine.ascx");
    //将控件加入到容器中
    this.controls.add(ucMine);                  
}
4.注意,如果webpart里面用到了数据库操作,还需要配置web.config
<SecurityClass Name="System.data.sqlclient" Description="System.Security.Policy.ZoneMembershipCondition,System.data.sqlclient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<IPermission class="SqlClientPermission" version="1" Unrestricted="true" />
5.如果不想手动拷贝ascx,可以把ascx文件作为资源文件打包到webpart的dll文件中,然后在调用的时候动态释放,释放的代码就是标准的.net的方式了:
//添加一个统一方法
private System.Web.UI.Control loadResource(string resName)
{
  try
  {
      using (StreamReader reader = new StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resName + ".ascx")))
      {
          String resourceContent = reader.ReadToEnd();
          using (StreamWriter writer = new StreamWriter(this.Page.Server.MapPath("("/wpresources/uc/" + resName + ".ascx"), false))
          {
              writer.Write(resourceContent);
          }
      }
      return this.Page.LoadControl("("/wpresources/uc/" + resName + ".ascx");
  }
  catch(Exception exp)
  {
     return null;
  }
}
protected override void CreateChildControls()
{
    //实例化对象
    UCMine ucMine = (UCMine)loadResource("ucmine");
    //将控件加入到容器中
    this.controls.add(ucMine);                  
}
 
类别: SharePoint
发布日期: 2007-3-20 22:58
原文地址:https://www.cnblogs.com/yinpengxiang/p/1416146.html