Adding Child Controls

The UserInfoTable web part isn't interactive: the information flows only from the server to the browser. To make a web part that can interact with members:

  1. Declare the web controls to display in the web part.

  2. Override the CreateChildControls event to set control properties.

  3. Add each control to the controls collection.

  4. Render the child controls in the RenderWebPart event.

The following code demonstrates the steps to create a Sum web part containing a textbox to receive a series of numbers input, a command button to perform the calculation, and a label to display the result:

 ' 1) Declare child controls. Dim _txt As New TextBox Dim _br As New Literal Dim WithEvents _btn As New Button Dim _lbl As New Label Dim _total As String   Protected Overrides Sub CreateChildControls() ' Create utility object for dimensions. Dim u As Unit ' 2) Set child control properties. With _txt .Width = u.Pixel(400) .Height = u.Pixel(200) .TextMode = TextBoxMode.MultiLine End With With _br .Text = "<br>" End With With _btn .Width = u.Pixel(60) .Height = u.Pixel(30) .Text = "Sum" .ToolTip = "Click here to get total." End With With _lbl .Width = u.Pixel(100) .Height = u.Pixel(30) End With ' 3) Add the controls in the order to display them Controls.Add(_txt) Controls.Add(_br) Controls.Add(_btn) Controls.Add(_lbl) End Sub   ' Display web part. Protected Overrides Sub RenderWebPart _ (ByVal output As System.Web.UI.HtmlTextWriter) ' 4) Write controls to output stream. RenderChildren(output) End ...

Get Essential SharePoint now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.