我有一个实用方法,可以从下拉列表中选择一个项目,我想添加触发所选索引更改事件的功能(如果需要)。
如何验证此事件是否存在然后执行?
示例页面代码:
protected void Page_Load(object sender, EventArgs e)
{
dropDownListEditMyControl.SelectedIndexChanged += new EventHandler(dropDownListEditMyControl_SelectedIndexChanged);
Utilities.SelectedItemInDropdown(dropDownListMyControl);
}
protected void dropDownListEditMyControl_SelectedIndexChanged(object sender, EventArgs e)
{
// Do something special
}
实用程序代码:
public static void SelectItemInDropdown(DropDownList dropDown, int? value, bool triggerChangeEvent)
{
if (value.HasValue)
{
ListItem li = dropDown.Items.FindByValue(value.Value.ToString());
if (li != null)
{
dropDown.ClearSelection();
li.Selected = true;
if (triggerChangeEvent)
{
if (dropDown.SelectedIndexChanged != null)
{
dropDown.SelectedIndexChanged(dropDown, new EventArgs());
}
}
}
}
}
问题在于检查下拉菜单是否具有选定的索引更改事件然后调用它(它不编译)