Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm having a problem that's really blowing my mind right now, because it seems like some basic .net functionality isn't working properly. No doubt I'm missing something really obvious. I could really use your help!

Basically, I've got a SharePoint Documents Library and I'm making a webpart to customize it's edit form. It's a rather basic form, it looks like this.

For example, I've selected an item in listbox A and click add. The code will give a crash with the error "Object reference not set to an instance of an object", so a NullPointer. When going into debug mode, I noticed that the ListBox.SelectedItem is always null, even when I've obviously selected an item. Furthermore, for debugging reasons, I thought I'd add a SelectedIndexChanged event to the listbox, but that doesn't even fire when making a selection.

Does anyone have a clue why this is happening? It seems weird that basic functionality would bug up like this. Here's my underlying C# code, the refresh method fills the listboxes with Labels according to the currently selected item in SharePoint. Listbox A are the available labels, B are the ones that have been added to the document.

    const string LABELFIELDNAME = "Global Labels";
    const string SPLABELLIST = "Global Label List";
    const string SPDOCLIBRARY = "Documents";

    int[] currentSelection;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeControl();
        EnsureChildControls();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Refresh();
    }

    protected void Refresh()
    {
        lstAllLabels.Items.Clear();
        lstSelectedLabels.Items.Clear();

        //Haal de label lijst in SharePoint op
        if (SPContext.Current.Web.Lists.TryGetList(SPLABELLIST) != null)
        {
            SPList splstLabels = SPContext.Current.Web.Lists[SPLABELLIST];

            //Vul de kieslijst op het form met de labels uit de SharePoint lijst
            foreach (SPListItem item in splstLabels.Items)
            {
                lstAllLabels.Items.Add(new ListItem(item.Title));
            }

            //Zoek de kolom met geselecteerde labels op van het item dat op het moment in SharePoint is geselecteerd
            if (SPContext.Current.ListItem[LABELFIELDNAME] != null)
            {
                SPFieldLookupValueCollection colCurrentItemLabels = SPContext.Current.ListItem[LABELFIELDNAME] as SPFieldLookupValueCollection;

                if (colCurrentItemLabels != null)
                {
                    if (colCurrentItemLabels.Count > 0)
                    {
                        //Voeg de geselecteerde labels toe aan de kieslijst op het form
                        foreach (SPFieldLookupValue item in colCurrentItemLabels)
                        {

                            if (lstAllLabels.Items.FindByText(item.LookupValue) != null)
                            {
                                lstSelectedLabels.Items.Add(item.LookupValue);
                                lstAllLabels.Items.Remove(item.LookupValue);
                            }
                        }
                    }
                }

            }
            //ELSE ERROR AFHANDELING

        }
        //ELSE ERROR AFHANDELING
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        //Label item aanmaken en ingevoerde naam geven
        SPListItem newItem = SPContext.Current.Web.Lists[SPLABELLIST].Items.Add();                
        newItem["Title"] = txtNew.Text;
        newItem.Update();

        Refresh();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ListItem temp = lstAllLabels.SelectedItem;
        lstSelectedLabels.Items.Add(temp);
        lstAllLabels.Items.Remove(temp);
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
    }

    protected void lstAllLabels_SelectedIndexChanged(object sender, EventArgs e)
    {
        currentSelection = lstAllLabels.GetSelectedIndices();
        lblSelected.Text = currentSelection.Length.ToString();
    }
share|improve this question
add comment

1 Answer

Could be many things... If you are using a visual webpart, I'm assuming this is the code for the usercontrol:

Remove the event handler declarations from your ascx file. Ensure your '<%@ control %>' directive is inheriting from the correct class. Ensure you've remembered to copy the ascx file to the layouts folder and that its referencing the correct control

Try declaring the controls as protected members in your class.

 protected ListBox lstAllLabels;

In your page load event

this.lstAllLabels.SelectedIndexChanged += new EventHandler(lstAllLabels_SelectedIndexChanged);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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