Add programmatically DataViewWebPart to your site.

  SPWeb WssSite = SPControl.GetContextWeb(Context); 
  SPList list = WssSite.Lists["--List Name--"];
  WssSite.AllowUnsafeUpdates=true;
  DataViewWebPart DVWebPart = new DataViewWebPart();
  
  DVWebPart.ZoneID = "ZoneForMenu";
  DVWebPart.Width="130";
  DVWebPart.Title="DataView Web Part";
  DVWebPart.FrameType= FrameType.None;
  DVWebPart.ListName = list.ID.ToString("B").ToUpper();
  DVWebPart.Description = list.ID.ToString("B").ToUpper();

  //Optional:
  //DVWebPart.Xsl="xsl code here";
  //DVWebPart.DataQuery="DataQuery Code Here";

  SPWebPartCollection webPartCollection = WssSite.GetWebPartCollection(Request.ServerVariables["URL"] , Storage.Shared);
  int WebPartCount=0;
  
  if (webPartCollection != null)
  {
    webPartCollection.Web.AllowUnsafeUpdates=true;
    webPartCollection.Add(DVWebPart);
    
  }
  else
  {
   //Response.Write("webPartCollection is null ");
  }
  
   
  WssSite.AllowUnsafeUpdates=false;
   
     //Cleanup:
  
  WssSite.Dispose();
  WssSite  = null;
  webPartCollection=null;
  DVWebPart.Dispose();
  DVWebPart=null;

  //refresh the page to see the new DataViewWebPart.
  Response.Redirect(Request.ServerVariables["URL"]);

posted @ 2006-01-18 17:58  海潮的博客  阅读(313)  评论(0编辑  收藏  举报