Tipo di campo personalizzato non postback
-
16-10-2019 - |
Domanda
ho cercato di costruire un tipo di campo personalizzato. Si tratta fondamentalmente di una serie di menu di scorrimento a cascata, in cui vengono adottati i valori della seconda discesa da un'altra lista in base al valore selezionato nella prima discesa. Ho seguito questo tutorial, e si basa il mio tipo di campo personalizzato su questo. Il problema che sto avendo è che la mia prima discesa si rifiuta di fare un postback (a tutti).
Il mio codice è il seguente:
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);
}
}
}
Ogni volta che si seleziona un'opzione nel primo elenco, v'è non postback, la pagina non si aggiorna (come dovrebbe), e quindi il metodo SelectedIndexChanged non sta sparando. Ho provato tutto ora, ma io proprio non riesco a ottenere la mia discesa per postback ... Che cosa mi manca?
Soluzione
La ragione che non funziona è che si sta eseguendo l'override del metodo Render (). Rimuovere che override e aggiungere la seguente alle vostre CreateChildControls:
this.Controls.Add(parentList);
this.Controls.Add(childList);
this.Controls.Add(box);
In questo caso quindi i controlli verrà registrato nella struttura di controllo e ottenere un nome di controllo valido. La registrazione è necessaria per ottenere il postback al lavoro come previsto.
Altri suggerimenti
I havent guardato il codice, ma ho implementato questa funzionalità prima quindi so theres una versione funzionante disponibili su CodePlex che si potrebbe usare sia al posto o cercare ispirazione in: http://cascddlistwithfilter.codeplex.com/