JG Vimalan's Blog

It is all about coding!

Create a custom toolbar webpart in MOSS 2007

Hi, 

The following code snippet will give you an idea on creating a custom toolbar webpart in MOSS 2007. The code snippet also contains enabling support for Ajax. 

public class CustomCalendar : System.Web.UI.WebControls.WebParts.WebPart
{

        #region Global Variables 

        private SPCalendarView _TeamCalendarUI = null;
        private UpdatePanel _ControlsHolder = null;
        private ToolBar _TeamCalendarToolBar = null;
        private LinkButton _NewMeetingButton = null;
        private LinkButton _NewAppointmentButton = null; 

        #endregion 

        /// <summary>
        /// Register the ScriptManager during page init.
        /// </summary>
        /// <param name=”e”></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ScriptManager ajaxManager = ScriptManager.GetCurrent(this.Page);
            if (ajaxManager == null)
            {
                ajaxManager = new ScriptManager();
                ajaxManager.EnablePartialRendering = true;

                Page.ClientScript.RegisterStartupScript(this.GetType(), this.ID, “_spOriginalFormAction = document.forms[0].action;”, true);

                if ((this.Page.Form != null))
                {
                    string formOnSubmitAtt = this.Page.Form.Attributes[“onsubmit”];
                    if (!string.IsNullOrEmpty(formOnSubmitAtt) & formOnSubmitAtt == “return _spFormOnSubmitWrapper();”)
                    {
                        this.Page.Form.Attributes[“onsubmit”] = “_spFormOnSubmitWrapper();”;
                    }
                    //add the ScriptManager as the first control in the Page.Form
                    this.Page.Form.Controls.AddAt(0, ajaxManager);
                }
            }
        }

        /// <summary>
        /// Create the controls to be rendered.
        /// </summary>
        protected override void CreateChildControls()
        {
            //Clear the existing controls
            this.Controls.Clear();
           
            _ControlsHolder = new UpdatePanel();
            {
                _ControlsHolder.ID = “updatePanel”;
                _ControlsHolder.ChildrenAsTriggers = true;
                _ControlsHolder.UpdateMode = UpdatePanelUpdateMode.Conditional;
            }

            this.Controls.Add(_ControlsHolder); 

            #region New Meeting Button

            _NewMeetingButton = new LinkButton();
            _NewMeetingButton.ToolTip = “Schedule a new meeting.”;
            _NewMeetingButton.Text = “New Meeting”;
            _NewMeetingButton.ID = “newMeetingBtn”; 

            #endregion 

            #region New Appointment Button

            _NewAppointmentButton = new LinkButton();
            _NewAppointmentButton.ToolTip = “Schedule a new appointment.”;           
            _NewAppointmentButton.Text = “New Appointment”;
            _NewAppointmentButton.ID = “newAppointmentBtn”;
            _NewAppointmentButton.Attributes.Add(“onclick”, “window.alert(‘Not yet implemented.’)”);

            #endregion 

            #region Custom ToolBar

            _TeamCalendarToolBar = (ToolBar)Page.LoadControl(“~/_controltemplates/ToolBar.ascx”);
            _TeamCalendarToolBar.RightButtonSeparator = “|”;  
            _TeamCalendarToolBar.RightButtons.Controls.Add(_NewMeetingButton);
            _TeamCalendarToolBar.RightButtons.Controls.Add(_NewAppointmentButton);           
            #endregion      

               _ControlsHolder.ContentTemplateContainer.Controls.Add(_TeamCalendarToolBar);

            _TeamCalendarUI = new SPCalendarView();
            _TeamCalendarUI.EnableViewState = true;
            _TeamCalendarUI.Width = System.Web.UI.WebControls.Unit.Percentage(100);

            . . .
            . . .

        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            _ControlsHolder.RenderControl(writer);
        }

OutPut:

CustomToolBar

Regards,

JG Vimalan

Advertisements

August 10, 2009 - Posted by | SharePoint

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: