This documentation is archived and is not being maintained.

Sample: Say Hello Web Part Code

Windows SharePoint Services 3

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Following is the complete sample code for the SayHelloWebPart sample described in Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part.

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

namespace Sample.SayHello
{
    public class SayHelloWebPart : WebPart
    {
      private Label displayName;
      private TextBox inputName;
      protected override void CreateChildControls()
      {
        base.CreateChildControls();

        //Fix for the UpdatePanel postback behavior.
        EnsurePanelFix();

        LinkButton sayHello = new LinkButton();
        UpdatePanel refreshName = new UpdatePanel();
        ScriptManager scriptHandler = new ScriptManager();
        displayName = new Label();
        inputName = new TextBox();

        //Set up control properties.
        this.displayName.ID = "displayName";
        this.displayName.Text = "Hello!";
        this.inputName.ID = "inputName";
        sayHello.ID = "sayHello";
        sayHello.Text = "Say Hello";
        scriptHandler.ID = "scriptHandler";
        refreshName.ID = "refreshName";
        refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;
        refreshName.ChildrenAsTriggers = true;

        //Add the EventHandler to the Button.
        sayHello.Click += new EventHandler(ClickHandler);

        //Add the user interface (UI) controls to the UpdatePanel.
        refreshName.ContentTemplateContainer.Controls.Add(this.inputName);
             refreshName.ContentTemplateContainer.Controls.Add(this.displayName);
        refreshName.ContentTemplateContainer.Controls.Add(sayHello);

        //The ScriptManager control must be added first.
        this.Controls.Add(scriptHandler);
        this.Controls.Add(refreshName);
      }
      private void ClickHandler(object sender, EventArgs args)
      {
        this.displayName.Text = "Hello, " + this.inputName.Text.ToString() + "!";
      }
      private void EnsurePanelFix()
      {
        if (this.Page.Form != null)
        {
          String fixupScript = @"
          _spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
          function _initFormActionAjax()
          {
          if (_spEscapedFormAction == document.forms[0].action)
          {
            document.forms[0]._initialAction =
              document.forms[0].action;
          }
        }
        var RestoreToOriginalFormActionCore = 
          RestoreToOriginalFormAction;
        RestoreToOriginalFormAction = function()
        {
        if (_spOriginalFormAction != null)
        {
          RestoreToOriginalFormActionCore();
          document.forms[0]._initialAction = document.forms[0].action;
        }
        }";
        ScriptManager.RegisterStartupScript(this, 
          typeof(SayHelloWebPart), "UpdatePanelFixup", 
          fixupScript, true);
        }
      }
    }
}
Show: