1

I have an ASPX page with code behind I am deploying under the Site Settings on a SharePoint site. The purpose is for the user to be presented with a checkboxlist of items that apply to this site and they can check/uncheck multiple items to control what of these things show on the site. I add the items programmatically in the code behind, but the settings page shows with everything except the checkbox list. No errors, just no checkbox list on the page. Buttons, labels, etc, everything else is there correctly.

ASPX CODE:

<%@ Page MasterPageFile="~/_layouts/application.master" Debug="true" Inherits="Company.Project.SiteConfiguration.ItemConfiguration,Company.Project.SiteConfiguration,Version=1.0.0.0,Culture=neutral,PublicKeyToken=xxxxxxxxx" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="~/_controltemplates/InputFormSection.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="~/_controltemplates/InputFormControl.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="~/_controltemplates/ButtonSection.ascx" %>
<%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="spuc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <asp:Content ID="Content1" contentplaceholderid="PlaceHolderPageTitle" runat="server">
        <SharePoint:EncodedLiteral runat="server" text="Site Item Settings" />
    </asp:Content>
    <asp:Content ID="Content2" contentplaceholderid="PlaceHolderPageTitleInTitleArea" runat="server">
        <asp:literal ID="Literal1" text='Items To Show On This Site' runat="server"/>
    </asp:Content>
    <asp:Content ID="Content3" contentplaceholderid="PlaceHolderPageDescription" runat="server"/>
    <asp:Content ID="Content4" contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
    </asp:Content>
    <asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderMain" runat="server">
        <table class=propertysheet border="0" width="100%" cellspacing="0" cellpadding="0" id="diidProjectPageOverview">
            <wssuc:InputFormSection Title="Company Project Items Site Settings" Description="Check the items to show on this site." runat="server">
                <Template_InputFormControls>
                        <wssuc:InputFormControl runat="server">
                            <Template_Control>                  
                                <spuc:InputFormCheckBoxList id="TheItems" runat="server" class="ms-authoringcontrols" />                
                            </Template_Control>     
                       </wssuc:InputFormControl>       
                </Template_InputFormControls>  
            </wssuc:InputFormSection> 
            <wssuc:ButtonSection runat="server">       
                <Template_Buttons>        
                    <asp:Button UseSubmitBehavior="false" runat="server" class="ms-ButtonHeightWidth" OnClick="Save_Click" Text="Save" id="Save" />    
                </Template_Buttons> 
            </wssuc:ButtonSection> 
        </table>
    </asp:Content>

CODE BEHIND:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Portal.WebControls;

namespace Company.Project.SiteConfiguration
{
    public class ItemConfiguration : Page
    {
        InputFormCheckBoxList TheItems;
        public System.Data.DataSet ds;
        Button Save;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls();
            Save = new Button();
            Save.Click += new EventHandler(Save_Click);
            SPWeb web = SPContext.Current.Web;
            TheItems = new InputFormCheckBoxList();
            GetTheItems();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        } 

        protected void Save_Click(object sender, EventArgs e)
        {
            try
            {
                String saveTheItems = "";
                foreach (ListItem item in TheItems.Items)
                {
                    if (item.Selected == true)
                    {
                        saveTheItems += item.Value + ";";
                    }
                }
                using (Microsoft.SharePoint.SPSite mysite = new Microsoft.SharePoint.SPSite(SPContext.Current.Web.Url))
                {
                    using (Microsoft.SharePoint.SPWeb myweb = mysite.OpenWeb())
                    {
                        if (myweb.AllProperties.ContainsKey("Company.Project.TheItems"))
                        {
                            myweb.AllProperties.Remove("Company.Project.TheItems");
                            myweb.Update();
                            myweb.AllProperties.Add("Company.Project.ConnectionString", saveTheItems);
                            myweb.Update();
                        }
                        else
                        {
                            myweb.AllProperties.Add("Company.Project.ConnectionString", saveTheItems);
                            myweb.Update();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Company.Project.SiteConfiguration.ItemConfiguration", ex.Message + " || " + ex.StackTrace);
            }
        }

        private void GetTheItems()
        {
            try
            {
                ServiceConnectorDLL.Service svc = new ServiceConnectorDLL.Service(SPContext.Current.Web.Url);
                TheItems.Items.Clear();
                using (Microsoft.SharePoint.SPSite mysite = new Microsoft.SharePoint.SPSite(SPContext.Current.Web.Url))
                {
                    using (Microsoft.SharePoint.SPWeb myweb = mysite.OpenWeb())
                    {
                        if (myweb.AllProperties.ContainsKey("Company.Project.ConnectionString"))
                        {
                            String allTheItems = myweb.AllProperties["Company.Project.ConnectionString"].ToString();
                            ds = svc.RetrieveAllItemIDs();
                            foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                            {
                                ListItem it = new ListItem();
                                it.Text = dr["ItemName"].ToString();
                                it.Value = dr["ItemID"].ToString();
                                if (allTheItems.IndexOf(dr["ItemID"].ToString()) > 0)
                                {
                                    TheItems.Items.Add(it);
                                    TheItems.Items.FindByValue(it.Value).Selected = true;
                                }
                                else
                                {
                                    TheItems.Items.Add(it);
                                    TheItems.Items.FindByValue(it.Value).Selected = false;
                                }
                            }
                        }
                    }
                }
                svc = null;
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Company.Project.SiteConfiguration.HospitalConfiguration", ex.Message + " || " + ex.StackTrace);
            }
        }
    }
}

1 Answer 1

1
  • First: any control creation should be done in the CreateChildControls override.
  • Second: TheItems = new InputFormCheckBoxList(); is unnecessary, as asp.net creates the control based on the aspx file (in a on the fly partial class.
  • Third: Does GetTheItems databind the items to the checkboxlist? If not, everytime you postback, they get readded and any checked items are discarded. The list will keep growning. Bind the control to a collection if you haven;t done so already.
1
  • This is an aspx page not a web part. Can I still use the CreateChildControls? If I take the TheItems = new InputFormCheckBoxList() off my code behind, it says that the object doesn't exist;doesn't seem to see the object on the aspx page. In the GetTheItems(), you can see I am looping through a dataset and creating list items off of 2 of the columns and adding them to the checkboxlist items. Also, before I start this each time, I clear out the item list. This also reloads any saved checked items so any time it posts back it will reload all items checking ones that had been saved as checked
    – sah
    Commented Jun 24, 2011 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.