我正在为 sharepoint 2010 开发一个 Web 部件。我有一个包含事件的下拉列表。在更改时,它应该显示一个或另一个标签,具体取决于值,但这不起作用。我确实将 autopostback 设置为 true,但我不希望每次单击下拉菜单时都加载整个页面。
protected override void CreateChildControls()
{
toolPartPanel = new Panel();
ddl = new DropDownList();
ddl.ID = "ddl";
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
ddl.EnableViewState = true;
ddl.Items.Add("a");
ddl.Items.Add("b");
label1 = new Label();
label1.Text = "a selected";
label1.Visible = false;
label2 = new Label();
label2.Text = "b selected";
label2.Visible = false;
this.Controls.Add(label1); this.Controls.Add(label2);
this.Controls.Add(ddl);
this.Controls.Add(toolPartPanel);
base.CreateChildControls();
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
Selected = ddl.SelectedValue.ToString();
if (Selected == "a")
{
label1.Visible = true;
}
else
{
if (Selected == "b")
{
label2.Visible = true;
}
}
}