September 2007
Beginner
448 pages
10h 2m
English
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 users:
Declare the web controls to display in the web part.
Override the CreateChildControls event to set control properties.
Add each control to the controls collection.
Set the ChildControlsCreated property to render to controls,
or
Render the child controls in the RenderWebPart method.
The following code demonstrates these steps to create a Sum web part containing a text box to input a series of numbers, a command button to perform the calculation, and a label to display the result:
[Guid("6D9B7DBA-C365-4d18-A453-86102CCEA61A")] public class Sum : System.Web.UI.WebControls.WebParts.WebPart { // 1) Declare child controls. TextBox _txt = new TextBox( ); Literal _br = new Literal( ); Button _btn = new Button( ); Label _lbl = new Label( ); protected override void CreateChildControls( ) { // 2) Set child control properties. _txt.Width = Unit.Pixel(300); _txt.Height = Unit.Pixel(150); _txt.TextMode = TextBoxMode.MultiLine; _txt.ToolTip = "Enter a series of numbers to add."; _br.Text = "<br>"; _btn.Width = Unit.Pixel(50); _btn.Height = Unit.Pixel(25); _btn.Text = "Sum"; _btn.ToolTip = "Click here to get total."; _lbl.Width = Unit.Pixel(100); _lbl.Height = Unit.Pixel(30); _lbl.Text = "Total: "; // 3) Add the controls in the order to display them Controls.Add(_txt); Controls.Add(_br); Controls.Add(_btn); Controls.Add(_lbl); // ...