
Step 3: Override CreateChildControls and create a button event handler
After configuring the new class to act as a Web Part, you must override the CreateChildControls method to build the UI. You must also add a button handler to refresh the display data.
To override CreateChildControls and create a button event handler
-
In the SayHelloWebPart.cs file, copy and paste the following code to override the CreateChildControls method
protected override void CreateChildControls()
{
base.CreateChildControls();
//Fix for the UpdatePanel postback behaviour.
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(sayHello);
refreshName.ContentTemplateContainer.Controls.Add(this.displayName);
//The ScriptManager control must be added first.
this.Controls.Add(scriptHandler);
this.Controls.Add(refreshName);
}
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
'Fix for the UpdatePanel postback behaviour.
EnsurePanelFix()
Dim sayHello As New LinkButton
Dim refreshName As New UpdatePanel
Dim scriptHandler As New ScriptManager
displayName = New Label
inputName = New TextBox
'Set up control properties.
Me.displayName.ID = "displayName"
Me.displayName.Text = "Hello!"
Me.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.
AddHandler sayHello.Click, _
New EventHandler(AddressOf ClickHandler)
'Add the user interface (UI) controsl to the UpdatePanel
refreshName.ContentTemplateContainer.Controls.Add(Me.displayName)
refreshName.ContentTemplateContainer.Controls.Add(Me.inputName)
refreshName.ContentTemplateContainer.Controls.Add(sayHello)
'The ScriptManager must be added first.
Me.Controls.Add(scriptHandler)
Me.Controls.Add(refreshName)
End Sub
-
Then, in the SayHelloWebPart.cs file, copy and paste the following code.
private void ClickHandler(object sender, EventArgs args)
{
this.displayName.Text = "Hello, "
+ this.inputName.Text.ToString() + ".";
}
Private Sub ClickHandler(ByVal sender As Object, _
ByVal args As EventArgs)
Me.displayName.Text = "Hello, " & Me.inputName.Text & "!"
End Sub
-
Now you have created the basic UI and button handling event.
For ASP.NET controls that use the JavaScript _doPostBack() function to commit changes, a regular full-page postback event may occur even when the Web Part is inside an UpdatePanel control. Windows SharePoint Services 3.0 and ASP.NET AJAX cache certain form actions, which can cause a conflict between SharePoint and ASP.NET AJAX. To change this behavior, you must add code to scripts that are running in Windows SharePoint Services 3.0.