Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top