Web Part

A Web Part is a Microsoft ASP.NET server control that serves a particular purpose, such as displaying data from a spreadsheet or streaming stock quotations from an online Web service. Web Parts are inserted in Web Part zones on Web Part Pages. Web Part zones are containers for Web Parts that group and organize Web Parts and provide a set of properties that configure the Web Parts in that zone. Web Part Pages consolidate data and Web content through Web Part zones to create dynamic information portals.

A Web Part, like other ASP.NET controls, can be designed to be a composite control containing child controls. If you leverage familiar ASP.NET server-side controls when developing custom Web Parts, you can create detailed views and polished data entry screens without getting bogged down in the details of generating DHTML and JavaScript. For example, you can create sophisticated-looking Web Parts in a hurry using ASP.NET controls such as the DataGrid control, the Calendar control and the ASP.NET validation controls.

You must override the CreateChildControls method to create a custom Web Part with child controls. Your implementation of CreateChildControls should create and initialize child control objects and then add them to the Web Part object’s Controls collection. A simple example of creating a TextBox object and a Button object is shown in the class definition for FadiWebPart in this figure.

public class FadiWebPart : WebPart {

// fields to hold child controls

protected TextBox txtName;

protected Button btnSubmit;

// create child control

protected override void CreateChildControls() {

txtName = new TextBox();

this.Controls.Add(txtName);

btnSubmit = new Button();

btnSubmit.Text = “Submit Name”;

this.Controls.Add(btnSubmit);

}

// render HTML for Web Part

protected override

void RenderWebPart(ByVal output As HtmlTextWriter) {

txtName.RenderControl(output);

output.RenderBeginTag(HtmlTextWriterTag.Br);

btnSummit.RenderControl(output);

}

}

A custom Web Part class can create child ASP.NET controls.
Examine the implementation of the RenderWebPart method in the class FadiWebPart and see how it calls the RenderControl method on each child control. When you call RenderControl on a child control and pass it the HtmlTextWriter parameter, the child control responds by writing its HTML into the output stream that will be returned to the user. This example illustrates how a Web Part’s implementation of the RenderWebPart method is written to combine the HTML output of all the child controls into a single HTML element.

Advertisements

~ by UTS on May 10, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: