Listing 1: RenderWebPart method

/// <summary>
/// Render this Web Part to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
{
//just send "Hello World!" in Bold down to browser
    output.Write("Hello World!");
}

Listing 2: The CreateChildControls method instantiates all the controls

        /// <summary>
        /// Instantiate the control, set properties, and hook up any event handlers. Then,
		 add control to the pages Controls collection
        /// </summary>
        protected override void CreateChildControls()
        {
            // create the label control
            labelCurrentStandings = new Label();
            // set the text
            labelCurrentStandings.Text = "Current Standings: ";
            // create a new Grid
            dataGridStandings = new DataGrid();
            dataGridStandings.DataSource = GetStandings();
            // Bind data to Grid
            dataGridStandings.DataBind();
            //Add the Controls
            Page.Controls.Add(labelCurrentStandings);
            Page.Controls.Add(dataGridStandings);

        }

Listing 3: The GetStandings method retrieves data from an XML document

/// <summary>
        /// Get the Standings from a datasource
        /// </summary>
        /// <returns>DataView containing standings</returns>
        private DataView GetStandings()
        {

            // create a new Grid
            dataGridStandings = new DataGrid();
            // create a DataSet
            DataSet dataSetStandings = new DataSet();
            // Load Xml into DataSet
            dataSetStandings.ReadXml("standings.xml");
            //return the dafault view
            return dataSetStandings.Tables["team"].DefaultView;

        }


Listing 4: The RenderWebPart method outputs HTML to the browser

        /// <summary>
        /// Render this Web Part to the output parameter specified.
        /// </summary>
        /// <param name="output"> The HTML writer to write out to </param>
        protected override void RenderWebPart(HtmlTextWriter
        output)
        {
            //make sure we have created the child controls
            EnsureChildControls();

            this.labelCurrentStandings.RenderControl(output);
            output.Write(HtmlTextWriterTag.Br);
            this.dataGridStandings.RenderControl(output);
        }

Listing 5: SafeControls section in the Web.config file


<SafeControls>
      <SafeControl Assembly="Microsoft.SharePoint,
	  Version=11.0.0.0, Culture=neutral,
      PublicKeyToken=71e9bce111e9429c"
	   Namespace="Microsoft.SharePoint"
	   TypeName="*" Safe="True" />
      <SafeControl Assembly="HelloWorldWebPart"
	  Namespace="HelloWorldWebPart" TypeName="*" Safe="True"/>
      <SafeControl Assembly="StandingsWebPart"
	  Namespace="StandingsWebPart" TypeName="*" Safe="True"/>
</SafeControls>

Additional Code...for This Article (999~KB)