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've been trying to build a custom field type. It's basically a set of cascading dropdowns, where the values of the second dropdown are taken from another list based on the selected value in the first dropdown. I have followed this tutorial, and based my custom field type on that. The problem I'm having is that my first dropdown refuses to do a postback (at all).

My code is as follows:

using System;
using System.Runtime.InteropServices;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using System.Web.UI;
using System.Web.UI.WebControls;

 namespace CaesarDropDown
{   
[CLSCompliant(false)]
[Guid("0a876b22-e0c7-4a4e-a434-68c3232266dc")]
public class CeasarDropDownFieldControl : BaseFieldControl
{
    private DropDownList parentList;
    private DropDownList childList;
    private TextBox box;
    private SPDataSource dataSource;
    private SPSite site;

    /// <summary>
    /// Indicate whether our field should be shown as a new section or appended to the end of the preceding section
    /// </summary>
    public bool DisplayAsNewSection
    {
        get
        {
            return true;
        }
    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        site = SPContext.GetContext(this.Context).Site;
        dataSource = new SPDataSource();
        dataSource.List = site.RootWeb.Lists["Categorieën"];

        parentList = new DropDownList();

        this.parentList.SelectedIndexChanged += new EventHandler(parent_SelectedIndexChanged);
        this.parentList.AutoPostBack = true;

        this.parentList.DataSource = dataSource;
        this.parentList.DataTextField = "Title";
        this.parentList.DataValueField = "Categorie";
        this.parentList.DataBind();
        this.parentList.Items.Insert(0, new ListItem("Select", ""));

        this.childList = new DropDownList();
        this.box = new TextBox();
        this.box.Text = this.ControlMode.ToString();
    }

    void parent_SelectedIndexChanged(object sender, EventArgs e)
    {
        EnsureChildControls();

        SPDataSource ds = new SPDataSource();
        ds.List = site.RootWeb.Lists["Applicaties"]; // Temporary, get the correct list from the first dropdown here later
        this.childList.DataSource = ds;
        this.childList.DataBind();
        this.childList.Visible = false;
    }


    public override object Value
    {
        get
        {
            EnsureChildControls();
            return this.parentList.SelectedValue;
        }

        set
        {
            EnsureChildControls();
            this.parentList.SelectedValue = (string)this.ItemFieldValue;
            SPDataSource ds = new SPDataSource();
            ds.List = site.RootWeb.Lists["Applicaties"];                
            this.childList.DataSource = ds;
        }
    }

    protected override bool OnBubbleEvent(object sender, EventArgs e)
    {
        return base.OnBubbleEvent(sender, e);
    }

    protected override void Render(HtmlTextWriter output)
    {           
        this.parentList.RenderControl(output);
        this.childList.RenderControl(output);
        this.box.RenderControl(output);
    }

}

}

Whenever I select an option in the first list, there is no postback, the page doesn't refresh (like it should) and thus the SelectedIndexChanged method is not firing. I've been trying everything now, but I just cannot get my dropdown to postback... What am I missing?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

The reason that it does not work is that you are overriding the Render() method. Remove that override and add the following to your CreateChildControls:

this.Controls.Add(parentList);
this.Controls.Add(childList);
this.Controls.Add(box);

If you do this then your controls will be registered in the control tree and get a valid control name. This registration is required to get postbacks to work as you expect.

share|improve this answer
    
Thanks, That was the culprit! When I look at it, it's quite logical actually. I mixed up some different tutorials/code snippets... –  Anonymous Feb 2 '10 at 14:13
add comment

I havent looked at your code, but i implemented this functionality before so i know theres a working version available on codeplex that you might either use instead or look for inspiration in: http://cascddlistwithfilter.codeplex.com/

share|improve this answer
    
That is true, but that requires me to have ALL the data in the second dropdown in 1 list and filter that one on runtime. I would like to have it take the items in the second dropdown from a different list depending on the selected item in the first. I have checked the code and can't find any differences that could cause my postback not to work tho :( –  Anonymous Feb 2 '10 at 11:37
add comment

Your Answer

 
discard

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