Click here to Skip to main content
Licence CPOL
First Posted 13 Dec 2006
Views 28,491
Bookmarked 36 times

Export and import web parts and personalized using membership in ASP.NET 2.0

By | 13 Dec 2006 | Article
Rich and dynamic web contains using webpart and asp.net membership controls

Sample screenshot

Introduction

In ASP.NET 2.0 there are several new web controls are added; one of them is web parts by taking advantage of the new Web Parts framework, you can build applications that can be easily customized by users at runtime. Users can rearrange the elements of a page built with Web Parts by using a drag-and-drop interface. Users can also add and remove elements in a page by working with one or more Web Part catalogs.
This article introduces you to the Web Part framework. It also goes beyond the basics and discusses several advanced features of Web Parts. For example, you learn how to share Web Part settings across applications by importing and exporting Web parts. More to this this is simple application by which you can see the use of membership control and personalization experience for user level.

Background

The basic idea behind this page to get introduced the new control in ASP.NET 2.0 like login control, Master pages and web parts, and main intention is import and export web parts.
Using the code.

Using the code

This code is basically a simple application where user can register and customize its news page.
New pages contain the web pats controls and that web part can be personalize and customize.

In this article we will focus on the Export and import of the web parts.
By this feature you donot have to put every web part on each page if your page is have web part zone then end user can import the perviously saved web parts and he can place it on any page he want by just importing the web part using import web part catalog.
 
For this reason you will be requiring one CatalogZone on your page.
After adding catlog zone add "Import web part catalog" to inside it.
You can refer the NewsPage.aspx page in the demo site.
For enabling the export option on your web parts you have to add one key in web.config file.

<system.web>
    <webParts enableExport="true"></webParts>
 </system.web>

After adding this now your all web parts are enable to the exports.
More to this you have to enable ExportMode mode of the all web part on your page to WebPartExportMode.All

Here I am enabling the export mode of all webparts like this.

foreach (WebPartZone wz in WebPartManager1.Zones)
           {
               foreach (WebPart wp in wz.WebParts)
               {
                   wp.ExportMode = WebPartExportMode.All;
               }
           }
If you want to export any specific web part then you can enable mode of that webpart only.

After doing this you will find the one menu (verb) is added to the webpart that is export
On clicking to this menu browser will ask user to save the *.WebPart file.

Sample screenshot


*.WebPart is just an xml file to stores the description of web part it look like :

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="<A href="http://schemas.microsoft.com/WebPart/v3">http://schemas.microsoft.com/WebPart/v3</A>">
    <metaData>
      <type src="~/UserControls/Links.ascx" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties />
      <genericWebPartProperties>
        <property name="AllowClose" type="bool">True</property>
        <property name="Width" type="unit" />
        <property name="AllowMinimize" type="bool">True</property>
        <property name="AllowConnect" type="bool">True</property>
        <property name="ChromeType" type="chrometype">Default</property>
        <property name="TitleIconImageUrl" type="string" />
        <property name="Description" type="string" />
        <property name="Hidden" type="bool">False</property>
        <property name="TitleUrl" type="string" />
        <property name="AllowEdit" type="bool">True</property>
        <property name="Height" type="unit" />
        <property name="HelpUrl" type="string" />
        <property name="Title" type="string">Links </property>
        <property name="CatalogIconImageUrl" type="string" />
        <property name="Direction" type="direction">NotSet</property>
        <property name="ChromeState" type="chromestate">Normal</property>
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="HelpMode" type="helpmode">Navigate</property>
        <property name="ExportMode" type="exportmode">All</property>
      </genericWebPartProperties>
    </data>
  </webPart>
</webParts>

Once you have save that file then you set the mode of webpartmanager as design mode

WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode; 

Once mode is set in the Catalog Zone user will have import web part catalog where he can browser the saved file i.e. (*.WebPart) file and add that web part to the page in any webpart zone by selecting webpart and clicking add.

Sample screenshot

Happy Exploring.. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sachin Pisal

Team Leader

United States United States

Member

I am Software professional and MCTS, Working for last 6 years in .Net technologies.
I loved to work on Microsoft technologies Specially the Web Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWeb part location inside og a webpartzone Pinmemberyaniv_azulay23:05 8 Jul '08  
Hello,
Hi, i found your demo very helpfull.
But i still have a problam. I don't understand where all the properties been saved.
For example, if I have a webpartzone and i drag a webpart inside it, where this action been saved ? why next time I'll entered the page I still can see what i did ?
Another thing, I want the changes I made, will be shared with every user who enter into my web application.
It's very urgant to me.
 
Yaniv.
GeneralTo add webpart to particular webzone without using dropdown of catalog zone Pinmember_manish0:28 7 Mar '08  
GeneralPlease Clearify Ur Sample PinmemberLalit Gandhi20:03 26 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 14 Dec 2006
Article Copyright 2006 by Sachin Pisal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid