kamagra how much to take

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.

  1.         // Current Site
  2.         SPSite siteThis = null;
  3.         // Current Web
  4.         SPWeb webThis = null;
  5.         // Text Box Control
  6.         private TextBox txtTextBox;
  7.         // Button Control
  8.         private Button save;
  9.         // String Build In Order To Parse Relevant Errors
  10.         private StringBuilder msgError = new StringBuilder();
  11.         // SQL connection variable
  12.         SqlConnection sqlConn = null;
  13.         // Validator Control to Verify User Input
  14.         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().

  3. protected override void CreateChildControls()
  4.         {
  5.             base.CreateChildControls();
  6.             try
  7.             {
  8.                 // Call a new text box control
  9.                 txtTextBox = new TextBox();
  10.                 txtTextBox.ID = "txtTextBox";
  11.                 Controls.Add(txtTextBox);
  12.                 // Call a new button control and bind it to an event
  13.                 save = new Button();
  14.                 save.Text = "Save";
  15.                 save.Click += new EventHandler(save_Click);
  16.                 Controls.Add(save);
  17.             }
  18.             catch (Exception ex)
  19.             {
  20.                 msgError.Append("Error in CreateChildControls: " + ex.Message);
  21.             }
  22.         }

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.

  3. private ListBox lbListBox
  4.         // Under base.CreateChildControl() in the CreateChildControls Method
  5.         lbListBox = new ListBox();
  6.         lbListBox.Rows = 1;
  7.         lbListBox.Items.Add(new ListItem("my lbListBox Item"," my lbListBox Item "));
  8.         Controls.Add(lbListBox);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>