Sections
     
     
WWWCoder.com Resource Directory

Introducing ASP.NET 2.0 Web Parts
5/23/2006 9:48:22 AM

Web Parts is the brand new feature in ASP.NET 2.0. This feature supports personalized content and layout of a web page. This new feature or rather controls allows the user Change, Edit, Move the content of the web page as per their choices.

Web Parts is the brand new feature in ASP.NET 2.0. This feature supports personalized content and layout of a web page. This new feature or rather controls allows the user Change, Edit, Move the content of the web page as per their choices.

A Web Part is a modular unit of information that has a single purpose and that forms the basic building block of a Web Part page. Each instance of a specific Web Part may be similar or may be different in appearance and in behavior. However, the instance of a specific Web Part is based on the same Web Part assembly file that is installed on the site server. A Web Part assembly file can contain code for more than one Web Part. Microsoft Windows SharePoint Services contains many Web Parts that you can use. Web Parts are also available in the online Web Part gallery. Additionally, you can create your own Web Parts, or you can install Web Parts that are provided by Microsoft. 

Let’s get the hands on the subject to understand it better. 

First we will create and web site named DemoWebParts. I am not going through the process of creation of a new web site once again. If you are reading this article first then please refer back to the introduction article of ASP.NET 2.0. 

In the application we will create one theme first. The theme will contain one CSS file and one skin file. The CSS (MyTheme.css) file will look like the following: 

body
{
    font-family: Lucida Sans, Verdana, Arial, SunSans-Regular, Sans-Serif;
    font-size: 75%;
    margin:30px;
    background-color:White;
}

p
{
    font-size: 1.0em;
}

/* Three-pixel bug fix: http://www.positioniseverything.net/explorer/threepxtest.html */
* html p { height: 1%; }
#body-container

h1
{
    text-align: left;
    font-size: 2em;
    font-weight: bolder;
}

#wrapper
{
    width: 100%; clear: both;
}

#wz1
{
    width: 30%;
    background-color: #B8D7FF;
    float: left;
    height: 250px;
    text-align: center;
}

#wz2   
{
    height:250px;
    width:35%;
    float:left;
    background-color: silver;
    text-align: center;
}

#wz3
{
    width:30%;
    float:left;  
    height:250px;
    text-align: center;
}

#footer
{
    clear:both;
    width:100%;
    background-color:Orange;
    height:30px;
}

h2
{
    font-size: 1.1em;
}

h3
{
   font-size: 1em;
}

h4
{
    font-size: .9em;
}

/* LINK VISTED HOVER ACTIVE selectors must remain in this order to work */
a:link
{
    color: #006699;
}

a:visited
{
    color: #330099;

a:hover
{
    text-decoration: underline;

a:active
{
    color: green;
}

The skin file (Skin.skin) will have the following entries:

<%--
Default skin template. The following skins are provided as examples only. 

1. Named control skin. The SkinId should be uniquely defined because
   duplicate SkinId's per control type are not allowed in the same theme. 

<asp:GridView runat="server" SkinId="gridviewSkin" BackColor="White" >
   <AlternatingRowStyle BackColor="Blue" />
</asp:GridView>

2. Default skin. The SkinId is not defined. Only one default
   control skin per control type is allowed in the same theme.

<asp:Image runat="server" ImageUrl="~/images/image1.jpg" />
--
%> 

<asp:GridView runat="server" SkinId="KylieSkin" >
           <RowStyle Font-Names="tahoma,arial" Font-Size=".85em" />
            <HeaderStyle BackColor="#4D4D4D" ForeColor="White" />
            <AlternatingRowStyle BackColor="#FFECB8" />
</asp:GridView>

<asp:Image SkinId="photo" runat="server" ImageUrl="hilo.jpg" /> 

<asp:Menu runat="server" Orientation="Vertical" />

 <asp:Login title="Login" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4"
   BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"
    ForeColor="#333333">
    <TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
    <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
    <TextBoxStyle Font-Size="0.8em" />
    <LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px"
        Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />

</
asp:Login>                       

<asp:Calendar title="Calendar" runat="server" BackColor="White" BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="190px" NextPrevFormat="FullMonth" Width="350px">
    <TodayDayStyle BackColor="#CCCCCC" />
    <SelectedDayStyle BackColor="#333399" ForeColor="White" />
    <OtherMonthDayStyle ForeColor="#999999" />
    <TitleStyle BackColor="White" BorderColor="Black" BorderWidth="4px" Font-Bold="True"
        Font-Size="12pt" ForeColor="#333399" />
    <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" VerticalAlign="Bottom" />
    <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />

</
asp:Calendar>           

You have to create the files as specified and then copy and paste the code there. 

Now we are ready for the main part, the creation and using web parts. Add one web page named Default.aspx (see Figure 1).  

Adding a web page called Default.aspx

Figure 1 - Adding a web page called Default.aspx

Now open the aspx file in source view mode and modify the same as shown below: 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title>Untitled Page</title>

</
head>
<
body>
    <form id="form1" runat="server">
        <h1>

            A Demo On Web Parts
        </h1>
        <div id="wrapper">
            <div id="wz1">
            </div>
            <div id="wz2">
            </div>
            <div id="wz3">
            </div>
        </div>

    </form>

</
body>
</
html> 

This will create 3 column named wz1, wz2, and wz3. Now apply the stylesheet, in which the styles of these columns are already defined. If you switch to design view, you will not be able to view anything. So we need to apply the stylesheet. 

To apply the stylesheet we change the page directive in the following way: 

<%@ Page theme ="MyTheme" StylesheetTheme ="MyTheme" Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

Now if you switch to design view, you will be able to see the screen that looks similar to Figure 2. 

Design view of the Web page

Figure 2 - Design view of the Web page

Now expand the toolbox. There you will  find one tab named WebParts. From there drag and drop WebPartManager on the web form. Now web parts as explained earlier are the collection of user controls that you make or the server controls that is coming with the ASP.NET 2.0 and they live inside the zone. To start of drag and drop two WebPartZone from the toolbox and place on the first two columns (see Figure 3). Now within these two zones you can drag and drop normal ASP.NET server controls. For this demo I will be using one Calendar control and one Login Control. The screen should look some what similar to Figure 4 (as formatting options provided by the controls have been used.)

 Selecting WebPartZone from the toolbox in Visual Studio.

Figure 3 - Selecting WebPartZone from the toolbox in Visual Studio.

Web page with web part controls added.

Figure 4 - Web page with web part controls added.

You can run the page in the browser to see how it looks like during the run time. See the small arrow in the upper right corner of each control and you can use them to minimize or hide the same. No code required. 

This is not over yet. Here comes the interesting part. 

Now we will use one combo box. We will use this to go with the web part manager and do something different what have been offered till now by ASP.NET 2.0. In the dropdownlistbox, add two items, “Browse” and “Design”. Keep the EnableAutoPostback check box checked. Then double-click the dropdownlistbox to open up the code editor. In the SelectedIndexChanged add the following code: 

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal
e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged 

        Select Case DropDownList1.SelectedValue
            Case "Browse"
                WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode
            Case "Design"
                WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode
            Case Else

 
        End Select
    End Sub

Now run the page in the browser. Here choose the Design mode from the dropdownlist. Now you can literally drag and drop both the controls (see Figure 5).  

Dragging and dropping a web part on a page.

Figure 5 - Dragging and dropping a web part on a page.

Now lets go a little further. Let us a one usercontrol and type some text on the page. With the help of the CatalogZone control we will allow the user to add or remove this usercontrol from the page. To do this Go back to the Default.aspx and drag and drop CatalogZone control. The CatalogZone will have the list of all the webpartzone in the page. Now Drag and drop DeclarativeCatalogPart within the CatalogZone. Now from the smart tag select edit template. This will create an empty box. Drag and drop the user control that you have created earlier.

In the DropdownListBox add another list item named “View” and also change the code a little bit. 

        Select Case DropDownList1.SelectedValue
            Case "Browse"
                WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode
            Case "Design"
                WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode
            Case "View"

                WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode

            Case Else
 

Now run the page in the browser. Select the View mode from the list. And here you will see that user have the choice to view the control and can place the control in any of the webpart zone.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 2/23/2010 10:23:35 PM by Anonymous
Comments: not clear , why not provide the whole source code in the end.
Left on 11/16/2009 11:44:41 PM by Anonymous
Comments: this is help ful
Left on 10/7/2009 8:31:45 AM by Anonymous
Comments: I got good knowledge thanking you
Left on 5/29/2009 7:10:56 AM by Anonymous
Comments: sadsad
No ratings available.
Left on 5/7/2009 5:35:43 AM by Anonymous
Comments: good.if u could provider even more that'll be better
Left on 3/4/2009 1:21:16 PM by Anonymous
Comments: Pure BS
Left on 2/10/2009 12:29:38 AM by Anonymous
Comments: It Nice For Beginner only
Left on 1/5/2009 11:37:31 PM by Anonymous
Comments: not clear
No ratings available.
Left on 12/4/2008 4:54:05 AM by Anonymous
Comments: Gand teri
No ratings available.
Left on 10/24/2008 5:17:20 AM by Anonymous
Comments: its only OK not good enough/helpful,actually its bit confusing.n e wayse good for beginners
Left on 9/20/2008 4:22:20 AM by Anonymous
Comments: So thank it very help ful me
No ratings available.
Left on 8/27/2008 11:56:53 PM by Anonymous
Comments: Nice for starting work on web parts
No ratings available.
Left on 8/22/2008 6:26:55 AM by Anonymous
Comments: nice article... helped me a lot..
Left on 7/24/2008 1:08:47 AM by Anonymous
Comments: poor
Left on 5/9/2008 8:44:49 AM by Anonymous
Comments: It is often useful to show a working example at the start of the tutorial. It would have made the explaination clearer.
Left on 1/2/2008 3:35:45 PM by Anonymous
Comments: i cant pick controls on design mode
plz ans
No ratings available.
Left on 12/28/2007 12:42:50 AM by Anonymous
Comments: good one
Left on 9/1/2007 11:04:28 AM by Anonymous
Comments: nice
Left on 8/29/2007 2:49:35 AM by Anonymous
Comments: useless
Left on 6/21/2007 3:53:30 AM by Anonymous
Comments: excellant work
Left on 2/27/2007 5:29:07 AM by Anonymous
Comments: non sense
Left on 12/26/2006 3:49:20 PM by Anonymous
Comments:
No ratings available.
Left on 8/1/2006 4:40:56 PM by Anonymous
Comments: Is not going far enough but is good for starting
No ratings available.
Left on 7/10/2006 12:13:39 AM by Anonymous
Comments: Excellent article. I found it extremely useful. It is definitely enough to get anyone going with WebParts!
No ratings available.
Left on 6/26/2006 6:53:49 AM by Anonymous
Comments: ok
Left on 6/14/2006 1:57:39 AM by Anonymous
Comments: wonderful
     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use