下拉列表的扩展方法导致错误

本文关键字:错误 方法 扩展 下拉列表 | 更新日期: 2023-09-27 18:36:20

我有一个扩展方法,用于DropDownList,它Extension method can only be declared in non-generic, non-nested static classes关键字this。我不太确定为什么,因为我已经看到了很多关于扩展方法的示例DropDownList

public static class DropDownListExtensions {
    public static void populateDropDownList(this DropDownList source, Action delegateAction) {
        source.DataValueField = "Key";
        source.DataTextField = "Value";
        source.DataSource = delegateAction;
        source.DataBind();
    }
}

下拉列表的扩展方法导致错误

可能是您的DropDownListExtensions类声明为其他类的嵌套。 将其移动到命名空间级别。