Developing A SharePoint Form WebPart – Part 2 – Declare Variables, Assign Controls, And Set up Exception Management
Once the class is setup for the Form WebPart, you need to declare relevant variables and instantiate the required controls by overriding the CreateChildControls() method. For the Forms WebPart, which only accepts an ID input, the only required controls are a TextBox control and a new button that is wired to a submission event.
- // Current Site
- SPSite siteThis = null;
- // Current Web
- SPWeb webThis = null;
- // Text Box Control
- private TextBox txtTextBox;
- // Button Control
- private Button save;
- // String Build In Order To Parse Relevant Errors
- private StringBuilder msgError = new StringBuilder();
- // SQL connection variable
- SqlConnection sqlConn = null;
- // Validator Control to Verify User Input
- private RequiredFieldValidator fieldVal;
There are two best practices to consider when starting to script a piece of functionality such as the Forms WebPart. Firstly, it is advantageous to build a reference to the current site and web since this can be passed on the OnInit method in order to harvest the current site and web context by using SPControl.GetContextSite(Context). In terms of exception management, organizations typically vary in whether there are enterprise standards such as using the trapping logic out of the Microsoft Exception Handling Enterprise Library or a custom common exception library. If managing individual loose exceptions, use a class available StringBuilder() in order to append the string exception text by using (stringbuildervariable).Append within the relevant try/catch blocks.
Next, the controls have to be declared by overriding the CreateChildControls() method. The two controls that need to be instantiated are the TextBox control to accept the ID input from the user and the button control that is wired to an event handler to manage the data submission. This is accomplished by initializing each of the controls, and then call the add method out of the Controls property to add it to the WebPart output.
Wrapping this in a try/catch block, it is noticeable that the StringBuilder() being used to manage the exceptions is being called in the catch block if there is an error that occurs in the CreateChildControls().
- protected override void CreateChildControls()
- // Call a new text box control
- txtTextBox = new TextBox();
- txtTextBox.ID = "txtTextBox";
- // Call a new button control and bind it to an event
- save = new Button();
- save.Text = "Save";
- save.Click += new EventHandler(save_Click);
- catch (Exception ex)
- msgError.Append("Error in CreateChildControls: " + ex.Message);
If more controls were required for data entry, such as using a ListBox control with relevant bound list items, the relevant variables need only be declared and the control added in the same fashion.
- private ListBox lbListBox
- // Under base.CreateChildControl() in the CreateChildControls Method
- lbListBox = new ListBox();
- lbListBox.Rows = 1;
- lbListBox.Items.Add(new ListItem("my lbListBox Item"," my lbListBox Item "));