Wednesday, August 20, 2008 Blogs RegisterLogin
Rock Star Sponsors

Compuware

Everest Logo images

redgatesmall.gif

TEK Systems

VacoTechnology_logo.jpg

 

Resource Sponsors

Apress
c1logo_150x150.gif
wrox_4c.gif

CodeSmith Tools
 

O'Reilly Discount Logo 

PerpetuumSoftwareLogo.gif

 

Quest Software

 

Decision Source

 

Murach Logo

 

JetBrains Logo

SourceGear Logo

ASPNetPRO

Ineta

  For information about sponsorship click here.

Blog_List
SharePoint 2007 Development ROCKS!
Location: BlogsRob Foster's Blog    
Posted by: Rob Foster 9/6/2006

Ok, so I'm more than a couple weeks into writing the SharePoint 2007 book and I have to say writing code against SharePoint 2007 with the 2.0 Framework ROCKS!

There are a lot of things that are really cool about developing web parts that will make life easier in both development and maintainence.  SharePoint 2007 Web Parts are actually built on top of the ASP.NET 2.0 Web Part Framework, which makes development a lot easier that it was with SharePoint 2003.  There are two classes that you can use as bases for your custom web parts: System.Web.UI.WebControls.WebParts.WebPart (ASP.NET 2.0) and Microsoft.SharePoint.WebPartPages.WebPart (SharePoint).  The Microsoft.SharePoint.WebPartPages.WebPart class actually inherits from the ASP.NET 2.0 web part and ensures seamless uplevel support between ASP.NET 2.0 web parts and SharePoint web parts. Now you might be asking when should you use on e over the other?  Actually, you should use the ASP.NET web part, unless you are trying to something specific to SharePoint, such as create a set of connected web parts.  Note that this post will only cover the ASP.NET WebPart class, because it is mainly what you will use when you develop your custom web parts.

Let's take a look at an example of a very simple web part that utilizes ViewState and accounts for a PostBack (note that this is a very simple example, but you will be able to see what to do and expand on it later). 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace WebParts
{
    public class MyWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        public MyWebPart() { }

        protected TextBox _txtName;
        protected Button _btnSubmit;
        protected Label _lblName;

        protected override void CreateChildControls()
        {
            //initialize controls

            this._txtName = new TextBox();
            this.Controls.Add(this._txtName);

            this._btnSubmit = new Button();
            this._btnSubmit.Text = "Submit";
            this._btnSubmit.Click += new EventHandler(_btnSubmit_Click);
            this.Controls.Add(this._btnSubmit);

            this._lblName = new Label();
            this.Controls.Add(this._lblName);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write("<table width=100%>");
            writer.Write("<tr>");
            writer.Write("<td>");
            writer.Write("Enter your name:");
            writer.Write("</td>");
            writer.Write("<td>");
            this._txtName.RenderControl(writer);
            writer.Write("</td>");
            writer.Write("</tr>");
            writer.Write("<tr>");
            writer.Write("<td colspan=2>");
            writer.Write("Your name is ");
            this._lblName.RenderControl(writer);
            writer.Write("</td>");
            writer.Write("</tr>");
            writer.Write("<tr>");
            writer.Write("<td>");
            writer.Write("</td>");
            writer.Write("<td>");
            this._btnSubmit.RenderControl(writer);
            writer.Write("</td>");
            writer.Write("</tr>");
            writer.Write("</table>");
        }

        void _btnSubmit_Click(object sender, EventArgs e)
        {
            this._lblName.Text = this._txtName.Text;
        }
    }
}
//end of code

In the class, first there are three controls defined: a TextBox, Button, and Label control.  Next, these controls are initialized by using the WebPart's CreateChildControls event.  This event should be used to initialize any web control that is contained inside of your web part and make sure to ADD YOUR CONTROL TO THE WEB PART'S CONTROLS COLLECTION!  Next, the Render method is used to create the user interface to the web part.  (Yes, there is a more elegant way to do this, but this gets the point across very easily).  In this example, a simple table is rendered with the web controls rendered in the cells of the table. 

When you compile and run the web part inside of SharePoint or your web part framework, it will display the table and allow you to click the button for the PostBack.  No, there still isn't a formal “designer”, but I hear that one is on the way and will be available for Visual Studio .NET 2005. 

You see?  I told you that web part development rocks!  Enjoy and let me know how your development goes!

Copyright ©2006 Rob Foster
Permalink |  Trackback

Your name:
Title:
Comment:
Add Comment   Cancel 
Search_Blog
Blog_Archive
Copyright 2006 by Nashville Visual Studio .NET User Group Terms Of UsePrivacy Statement