-
16-10-2019 - |
题
我一直在尝试构建自定义字段类型。基本上,这是一组级联下拉列表,其中第二个下拉列表的值是根据第一个下拉列表中所选值从另一个列表中获取的。我跟随了 这个 教程,并基于我的自定义字段类型。我遇到的问题是,我的第一个下拉列表拒绝(完全)进行后卫。
我的代码如下:
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);
}
}
}
每当我在第一个列表中选择一个选项时, 不
解决方案
它不起作用的原因是您正在覆盖Render()方法。删除该覆盖物,并将以下内容添加到您的CreateChildControls:
this.Controls.Add(parentList);
this.Controls.Add(childList);
this.Controls.Add(box);
如果执行此操作,则您的控件将在控制树中注册并获取有效的控件名称。需要此注册才能使后卫按照您的预期工作。