WebPart Framework basics
CoderSource.net
WebPart Framework basics - Article by deepak.gupta
Level: MediumType: Article
Rating: Page: 2 of 3

Date: 8/17/2007 12:00:00 AM

Environment: IIS, ASP .NET, C# 2.0

Building Web Part Page

Here we will create a webpart page using components of WebPart Framework.

Prerequisite

  • Internet Information Services (IIS) installed and configured on the computer that will host the site.

    For details about installing and configuring IIS, see the IIS Help documentation included with the installation, or see the online IIS documentation on the Microsoft TechNet site (Internet Information Services 6.0 Technical Resources).


  • VS .Net 2005

    Launch Visual Studio 2005 and create a class library project named “MyWebPart”.


  • Add following code in MyWebpart.cs

    using System;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;

    namespace MyWebpart
    {
        [Guid("0d722531-f735-43f4-af13-51b4dc8f4ecd")]

        public class MyWebpart : System.Web.UI.WebControls.WebParts.WebPart
       {
             private string displayText = "Hello World!";
             [WebBrowsable(true), Personalizable(true)]
             public string DisplayText
             {
               get { return displayText; }
               set { displayText = value; }
             }
            public MyWebpart()
           {
              this.ExportMode = WebPartExportMode.All;
           }
           protected override void Render(HtmlTextWriter writer)
          {
            // TODO: add custom rendering code here.
           // writer.Write("Output HTML");

          writer.Write(displayText);
          }
        }
    }




     

    Using WebPart

    Create Web Site

    Launch Visual Studio 2005 and create a new website project. A default.aspx page will also get created.

    Configure Web.config


    By default ASP.NET 2.0 Web Parts uses SQL Express. Go to next section if SQL Express is installed on your machine.

    To use this stuff with SQL Server 2000,2005 follow following steps to configure the aspnetdb database on your SQL Server 2000 database server

    Follow these steps to setup the aspnetdb database:

    1) run the utility - C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\aspnet_regsql.exe

    2) Select the “Configure SQL Server for application services” option and click “Next”

    3) Enter your server name, login credentials and the name of the database to create/edit with the aspnetdb stuff.(The database will hold your login and personalization data, so if you don't have complete control over the database server

    4) Add the following stuff to your web sites Web.config file (this stuff tells ASP.NET to use your SQL Server 2000 or 2005 database instead of SQL Express):

     

    <connectionStrings>
    <add name="SQLConnString"
    connectionString="Data Source=SQL_SERVER_NAME;Initial Catalog=aspnetdb;Integrated Security=True"
    providerName="System.Data.SqlClient" /> </connectionStrings>

    <system.web> <webParts> <personalization defaultProvider="SqlPersonalizationProvider">
    <providers>
    <add name="SqlPersonalizationProvider"
    type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
    connectionStringName="SQLConnString"
    applicationName="/" />
    </providers> <authorization>
    <deny users="*" verbs="enterSharedScope" />
    <allow users="*" verbs="modifyState" />
    </authorization>
    </personalization>
    </webParts>
    </system.web>

     

    Add WebPartManager Control

    Next, drag and drop a WebPartMenuManager control from the Toolbox (under the WebParts tab) onto the default Web Form. The WebPartManager control manages all of the Web Parts on a Web Form and must be the first control that you add to the page. A Web Part Page require exactly one instance of WebPartManager control. This control must be placed in an .aspx file before the tags for any of the other control associated with the Web Part infrastructure.

    You will find following in your code

    <asp:WebPartManager ID="WebPartManager1" runat="server">
    </asp:WebPartManager>

    Add WebPartZone

    Insert a 3-by-1 table onto the form (Layout->Table) and drag and drop a WebPartZone control from the Toolbox (under the WebParts tab) into first cell. This WebPartZone control will serve as the docking area for one or more Web Parts.

    <body>
    <form id="form1" runat="server">
    <div>
    <asp:WebPartManager ID="WebPartManager1" runat="server" >
    </asp:WebPartManager>
    <table style="width: 186px">
    <tr>
    <td style="background-color: #ffcccc">
    <asp:WebPartZone ID="WebPartZone1" runat="server">
    </asp:WebPartZone>
    </td>
    <td style="width: 49px; background-color: #99ff66;" >
    </td>
    <td style="width: 62px">
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>

    Add a Web Control

    Drag drop a lable control in WebPartZone

    <asp:WebPartZone ID="WebPartZone1" runat="server">

    <ZoneTemplate>

    <asp:Label ID="Label1" runat="server" Text="My web part"></asp:Label>

    </ZoneTemplate>

    </asp:WebPartZone>



    Run the web application.


    Add WebPart

    Here will add the webpart created

    Add a reference to the class library that holds the MyWebPart.

    Open code default.aspx page . Register mywebpart in page you want to display webpart

    <%@ Register Assembly="MyWebpart" Namespace="MyWebpart" TagPrefix="mywebpart" %>

    Add following line

    <mywebpart:SimpleWebpart runat="server" id="HelloWorld" Title="WebPart" />

    Code will look like
     

     <asp:WebPartZone ID="WebPartZone1" runat="server" >

    <ZoneTemplate>

    <asp:Label ID="Label2" runat="server" title = "My Area" Text="My Web Part"></asp:Label>

    <mywebpart:SimpleWebpart runat="server" id="HelloWorld" Title="WebPart" />

    </ZoneTemplate>

    Run the application

    Till now we are able to create and display web part. Now we will see how we can edit properties and layout of a webpart.

    1 2 3

    You Can Rate this Article, if you are Logged In      
     

    More Links from CoderSource.net:

     
    Refer to a Friend:

    Your Details:

    Name:     e-mail:

    Friend Details:

    Name:    e-mail:    


    MENU
    Home
    MFC 
    C++
    .Net
    WIN32
    Programming
    Forum
    My Articles
    Add to Google
    Add to My Yahoo!
    Welcome to Codersource.Net Login | Register | Faq  

    SEARCH
    Google
     

    NOTES:


    Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

    © Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us