zevenseas


 

Re-using SharePoint controls

This post is going to be a summary of some of some the SharePoint controls I regularly use in my solutions. Cool thing about re-using the controls that are already out there is, that saves time in re-inventing the wheel and you don’t have to worry about keeping the look&feel consistent ;)

First, check out the following blog posts for more information on re-using the out-of-the-box controls of SharePoint

 

Now some controls are already covered by the links that are stated above, so this is really just a quick summary on the things I like to use most ;)

I’ve given each control a table, in the first row there is a screenshot (if applicable) how to control looks like when it’s rendered by SharePoint,
in the second row there is the declarative way of using the control,
in the third row there is (also, if applicable) some codebehind logic on how to  use the control and it’s value.

WebApplicationSelector 

webapplicationselector
<SharePoint:WebApplicationSelector ID="webApplicationSelector" runat="server" />
protected WebApplicationSelector webApplicationSelector;
..
SPWebApplication webApplication = webApplicationSelector.CurrentItem;

 

SiteAdministrationSelector

siteadministrationSelector
<SharePoint:SiteAdministrationSelector runat=server ID="siteadminselector" /> 
protected SiteAdministrationSelector siteAdministrationSelector;
..
SPSiteAdministration siteAdmin = siteAdministrationSelector.CurrentItem;

 

SchedulePicker , for more info on this one and when & why you should use this please read “Using the SchedulePicker” ;)

schedulepicker (2)

<%@ Register TagPrefix="wssuc" TagName="SchedulePicker"

src="~/_controltemplates/SchedulePicker.ascx" %>

<wssuc:SchedulePicker id="schedulePicker"Hourly="True"

Weekly="True" Monthly="True" Enabled="True" EnableStateView="True" runat="server"/>

protected SchedulePicker schedulePicker;
..
//Getting a schedule from the picker
SPSchedule schedule = schedulePicker.Schedule;
//Setting the schedule of the picker
schedulePicker.ScheduleString = schedule.ToString();

 

SPDatePickerControl 

spDatePickerControl
<script type="text/javascript" 
src="/_layouts/datepicker.js"></script>
<SharePoint:SPDatePickerControl id="datePickerControl" 
   runat="server"/>

 

ButtonSection

buttonsection
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" 
src="~/_controltemplates/ButtonSection.ascx" %>
<wssuc:ButtonSection runat="server">
    <Template_Buttons>
        <asp:Button runat="server" class="ms-ButtonHeightWidth" 
        OnClick="OnClickOK" Text="<%$Resources:wss,multipages_okbutton_text%>" 
        id="btnOk" accesskey="<%$Resources:wss,okbutton_accesskey%>"/>
    </Template_Buttons>
</wssuc:ButtonSection> 
When making use of the LayoutsPageBase in your codebehind, you can define the URL of the Cancel button by using this snippet:
public override string PageToRedirectOnCancel
{
    get
    {
        //return base.PageToRedirectOnCancel;
        return "/_layouts/settings.aspx";
    }
}

 

InputFormSection & InputFormControl

inputformsectionandcontrol
<%@ Register TagPrefix="wssuc" 
TagName="InputFormSection" 
src="~/_controltemplates/InputFormSection.ascx" %>
<%@ Register TagPrefix="wssuc" 
TagName="InputFormControl" 
src="~/_controltemplates/InputFormControl.ascx" %>
<wssuc:InputFormSection Title="InputFormSection"
    Description="InputFormSection Description" runat="server">		
    <template_inputformcontrols>            
        <wssuc:InputFormControl runat="server" LabelText="InputFormControl Text">
            <Template_Control>
                <%--Put a control here--%>
                </Template_Control>
        </wssuc:InputFormControl> 
    </template_inputformcontrols> 
</wssuc:InputFormSection> 

 

InputFormTextBox, notice there are two InputFormTextBoxes shown here. One with the RichTexT property set to true and the other set to false. So with a simple property change you can get the nicely and shiny FullHTML textbox.

inputformtextbox1

<SharePoint:InputFormTextBox ID="ApproveMailSubject" RichText="false"

runat="server" Width="100%"/>

<SharePoint:InputFormTextBox ID="ApproveMailContent" RichText="true" 
	RichTextMode="FullHtml" 
	runat="server" 
	TextMode="MultiLine" 
Rows="20"/>

 

InputFormRequiredFieldValidator

inputformrequiredvalidator
<wssawc:InputFormTextBox ID="uxFirstName" RichText="false" 
    TextMode="Singleline" runat="server" Width="100%"/>	
<wssawc:InputFormRequiredFieldValidator ID="uxFirstNameValidator"
    ControlToValidate="uxFirstName" Text="Error" runat="server"
    ErrorMessage ="First name is required" 
    EnableClientScript="true" Display="Dynamic">
</wssawc:InputFormRequiredFieldValidator>
 

InputFormRangeValidator

inputformrangevalidator
<wssawc:InputFormRangeValidator 
    ID="inputFormRangeVal" 
    Type="Integer" MinimumValue="1" MaximumValue="30"
    ControlToValidate="uxNumberOfDays"
    ErrorMessage="Value must between 1 and 30" 
    Runat="server" />

 

ToolBar & ToolBarButton

toolbar
<%@ Register TagPrefix="wssuc" TagName="ToolBar" 
src="~/_controltemplates/ToolBar.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBarButton" 
src="~/_controltemplates/ToolBarButton.ascx" %>
<wssuc:ToolBar id="Toolbar" runat="server">
	<template_buttons>
		<wssuc:ToolBarButton runat="server"
			id="ViewSites"
			Text="View Sites"
			ToolTip=""
			OnClick="ViewSites_Link"
			ImageUrl="/_layouts/images/newitem.gif"
			Padding="2px"
			AccessKey="V" />			
	</template_buttons>
	<template_rightbuttons>

<SharePoint:WebApplicationSelector id="Selector"

runat="server"/>

	</template_rightbuttons>
</wssuc:ToolBar>
To use the ToolBar in your WebPart you can load the UserControls and reference them using this:

protected ToolBarButton ToolBarButton;
protected ToolBar ToolBar;
..
//Adding a ToolBarButton
ToolBarButton = 
    (ToolBarButton)Page.LoadControl("/_controltemplates/ToolBarButton.ascx");
ToolBarButton.Click += new EventHandler(ToolBarButton_Click);
ToolBarButton.Text = "Complete selected tasks";
ToolBarButton.ImageUrl = "/_layouts/images/CheckNames.gif";
//Adding the ToolBar
ToolBar = 
    (ToolBar)Page.LoadControl("/_controltemplates/ToolBar.ascx");
ToolBar.Buttons.Controls.Add(ToolBarButton);
this.Controls.Add(ToolBar);

Links to this post

Comments

On 03 Jul 2009 02:55, Michaël Hompus

Great overview, I use them often as well.
I did miss the SiteAdministrationSelector in your presentation at the SDN, but I see you've included it in this post. I like this one even over the WebApplicationSelector. :)

On 03 Jul 2009 03:59, Robin

Thanks! And indeed.. awesome controls :) You got a nice blog as well mate! Added it my blogroll..

On 14 Jul 2009 08:15, Karine Bosch

Hi Robin,
Thanks for the reference to my pages! I always liked your blog content.
Greetz,
Karine

On 14 Jul 2009 09:24, Robin

Hi Karine,

same here! And no problem about the references, you deserve it ;)

On 17 Sep 2009 05:10, Thomas Resing

Hi Robin,
Followed the link from Kirk Evans' blog. Great content and layout here. Putting this on my top reference list for application pages.
Tom

On 18 Sep 2009 11:55, Robin

Hi Thomas,

thanks mate! :)

On 19 Oct 2009 10:03, Khushi

Thanks!! must appreciate it.. great article. I want to know can i have a Lost focus event of these controls?if yes then how???


Thanks * Regards;
Khushi

Name

Url

Email

Comments

CAPTCHA Image Validation



 
 
 

© 2009 Community Kit For SharePoint