ASP.NET
  Home arrow ASP.NET arrow Page 2 - Using Web Parts in ASP.Net 2.0
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Using Web Parts in ASP.Net 2.0
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2005-11-28

    Table of Contents:
  • Using Web Parts in ASP.Net 2.0
  • Web Parts Explained
  • Properties
  • Changing the Layout

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Web Parts in ASP.Net 2.0 - Web Parts Explained


    (Page 2 of 4 )

    Web parts are basically beefed up panel controls. No, in fact, that’s exactly what they are, as the WebPart abstract class is actually derived from the Panel class! But instead of just the basic content holding block that is a panel, the Web Part has loads of additional interface elements as well as functionality, such as the ability to minimize and restore, as well as drag-and-drop.

    Before I get into the code, I have to explain one more thing. You’re not actually going to implement a WebPart control on your page. As I mentioned, it’s an abstract class. So how do you get them on your page? Well, the simplest way is by creating user controls. These are contained in a collection of WebParts, by a control called a WebPartZone. The WebPartZone is also responsible for the layout of the WebPart(s) that it contains. You can have as many WebPartZones as you want on a page, containing many web parts. Of course, you can go through all the work of creating your own class by inheriting from the WebPart class, but it still needs to be wrapped in a WebPartZone. So, for the sake of simplicity, just use user controls!

    You will also need to use a control called WebPartManager to manage all the WebParts on the page. Though it has no actual user interface, it is necessary to handle such things as remembering the visual state of the users’ WebParts.

    So let’s assume that you’ve created two user controls, one that retrieves the weather, and one that retrieves stock quotes. I’m not going to detail the creation of these controls; you should be able to handle that yourself, or at least download some pre-built controls on the web to do this. I’m just going to show you how to register them on your page, and implement them as web parts. Here’s the code:

    <%@ register tagprefix=”jc” tagname=”Weather” src=”weather.ascx” %>
    <%@ register tagprefix=”jc” tagname=”Stocks” src=”stocks.ascx” %>
    <%@ page language=”C#” %>

    <html>
    <head>
    <title>webparts tutorial</title>
    </head>
    <body>
     <form runat=”server” id=”Form1”>
      <asp:WebPartManager runat=”server” id=”myWebMgr” />
      <asp:WebPartZone runat=”server” id=”zone1”>
      <ZoneTemplate>
            <jc:Weather runat=”server” id=”myWeather” />
            <jc:Stocks  runat=”server” id=”myWeather” />
           </ZoneTemplate>
      </ asp:WebPartZone>
     </form>
    </body>
    </html>

    By looking as this code, it’s not readily apparent why I even bother throwing in all the extra code for the WebParts, when we could just toss the user controls right onto the page. However, by wrapping them in WebParts, we achieve some extra "stuff." By default, the WebPartZone will now throw a border and title on your WebParts (user controls) as defined in your site defaults. But very importantly, now you’re also given runtime rendering options. As in: each control displays two options: "minimize" and "close."

    More ASP.NET Articles
    More By Justin Cook


       · hey,Justin Cook here. Hope you enjoy the article!
       · Hi justin,I'm just a little confused with the development of web parts....
     

    ASP.NET ARTICLES

    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT