concept control in category sharePoint

appears as: control, controls, controls, A control, control, The controls, The control
SharePoint 2010 Web Parts in Action

This is an excerpt from Manning's book SharePoint 2010 Web Parts in Action.

Before we dig down deep into developing Web Parts for SharePoint 2010, you should know what a Web Part is. As the name implies, it means parts of a web. Web Parts are the building blocks for building web pages in SharePoint. Web Parts are essentially ASP.NET web controls that inherit the web control base class in ASP.NET but have special abilities compared with ordinary ASP.NET web controls. These special abilities focus on giving the end user or administrator, and not the developer, the possibility to customize, move, and edit the control. Normally, ASP.NET controls are configured and programmed on the server side with no or limited options for the end user to modify its properties. Web Parts, on the other hand, are building blocks that the end user can choose from to build their own web page. For example, the user may decide that a particular Web Part should show the contents of a document library with a specific set of columns, that another Web Part should display a graph generated from a data source, and so on. The most used Web Part in SharePoint is the one that shows a view of a SharePoint list, such as a task list, a calendar, or a document library.

The UI is built using controls. A control can be a text label, a button, or something more complex, such as a data grid. Controls can also be combined to create composite controls. These controls can be ASP.NET, SharePoint, or custom controls. Some controls produce the HTML that’s used by the browser, and some only act on the server side. This chapter will start by exploring the ASP.NET basics and showing how you can utilize the ASP.NET controls and features in your Web Part. Remember that Web Parts are defined in ASP.NET and aren’t specific to SharePoint.

When saving the properties after editing them using the tool pane, you invoke the ApplyChanges method after OnLoad, as figure 4.4 shows. A common mistake is using OnLoad to build your interface, which occurs before the changes are applied to the Web Part. The result? Your updated properties won’t be used by the controls immediately. You have to reload the page or, if you clicked Apply, click Apply once again to actually use the new values.

Figure 4.4. When the changes made in an Editor Part are applied to the Web Part, the properties are pushed back to the Web Part after the OnLoad event.

If you need to have your controls and control tree available earlier in the event flow, you can use the EnsureChildControls method, which invokes the CreateChildControls event. Don’t manually call CreateChildControls. This method ensures that any control adapters applied to the control are used. It also sets the ChildControlsCreated property of the control to true to avoid the multiple calling of the CreateChildControls method. As a best practice, you should always set this property in your own implementations of the CreateChildControls method:

protected override void CreateChildControls() {
    // create controls
    this.ChildControlsCreated = true;
}

You’ll eventually find that you need to add custom styling to your Web Part that isn’t available in any of the default CSS files. You have several approaches for adding this styling to the controls and Web Parts. To illustrate, let’s take a look at an ASP.NET Panel, which results in a HTML div element for which we’ll set a custom style. We’ll set height, width, background color, border, and a change in the font in this Panel object. It should look like figure 4.13.

Applying Styles Direct to the Controls

The easiest and fastest way is to add the styling directly to the control. You can do this by using the HTML style attribute or the specific properties of the control. When you’re applying the styles directly to the control, the code looks like this:

<asp:Panel ID="panel"
    runat="server"
    BorderColor="Black"
    BorderWidth="1px"
    BackColor="Silver"
    Font-Bold="true"
    ForeColor="Red"
    Width="200px"
    Height="100px">
Look and feel!
</asp:Panel>

As you can see, it takes a lot of code to apply styling to this control using the control’s inline properties. It’s easy for the developers to read and change, but the resulting HTML in the browser is bad. This sample uses a Visual Web Part; if you’d do the same for a traditional Web Part, you’d need a lot more code. All these attributes on the Panel element automatically create a style attribute on the resulting HTML div element, as shown here: