【问题标题】:No extension method attribute How can I solve it没有扩展方法属性如何解决
【发布时间】:2016-01-18 06:10:29
【问题描述】:

当我使用时

protected void GenGridView()
    {
        var data = project.ObtainDataDescJSON();
        Title = "show";
        for (int rowCtr = 0; row < data.Num.Count; row++)
        {
            var buttonField = new ButtonField
            {
                ButtonType = ButtonType.Button,
                Text = "Show",
                CommandName = "Display"

            };

            buttonField.Attributes.Add("data-toggle", "modal");
            buttonField.Attributes.Add("data-target", "#myModal");
            buttonField.CssClass = "btn btn-info";             

            ModelNumFieldsGrid.Columns.Add(buttonField);
            break;
        }
      }

在 C# 中定义按钮时出现错误说没有扩展属性和没有扩展 cssClass。

我试过了

[AttributeUsageAttribute(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute 

using System.Runtime.CompilerServices.ExtensionAttribute;

但不起作用。我该如何解决它

我的确切错误

Error	2	'System.Web.UI.WebControls.ButtonField' does not contain a definition for 'Attributes' and no extension method 'Attributes' accepting a first argument of type 'System.Web.UI.WebControls.ButtonField' could be found (are you missing a using directive or an assembly reference?)	C:\Users\s0\Documents\Visual Studio 2013\WebSites\Model.aspx.cs	55	25	Pred

Error	7	'System.Web.UI.WebControls.ButtonField' does not contain a definition for 'CssClass' and no extension method 'CssClass' accepting a first argument of type 'System.Web.UI.WebControls.ButtonField' could be found (are you missing a using directive or an assembly reference?)	C:\Users\s06\Documents\Visual Studio 2013\WebSites\Model.aspx.cs	74	25	Pred

【问题讨论】:

  • 你能告诉我们buttonField的定义吗?
  • 酷!让我更新我的帖子
  • 实际的错误信息也会有所帮助 - 我确定这不是“没有扩展属性和没有扩展 cssClass”
  • 我已经编辑了我的帖子你是说这部分吗?
  • 您使用的是什么版本的 .net?这在 4.0 和 4.5 之间移动

标签: c# extension-methods


【解决方案1】:

C# 中的扩展方法允许您声明可以调用的方法就好像它们是类的方法:

public class Button { } 

public static class ButtonExtensions 
{
    public static int GetArea(this Button button)
    {
        return button.Width * button.Height;
    }
}

使用此扩展方法,您可以调用:

Button b = new Button();
int area = b.GetArea();

当您删除扩展方法时,您会收到错误消息

“按钮”不包含GetArea 的定义,并且找不到接受Button 类型的第一个参数的扩展方法GetArea

现在可能从来没有扩展方法 AttributesCssClass 但编译器猜测它曾经存在过,可能是因为大多数程序员甚至不会在 IntelliSense 不存在时尝试编译t拿起成员的名字。

简而言之:编译器告诉您ButtonField 没有这些成员。

【讨论】:

  • 那么,使用扩展方法属性的唯一方法是自己编写Attribute方法吗?其实这个问题是基于我的另一个问题。你能看看吗? stackoverflow.com/questions/33217284/…
  • 错误很简单——你试图访问不存在的属性。但是现在您正在抛出“扩展”和“属性”这两个术语,就像您正在尝试做一些非常复杂的事情一样。老实说,我不知道你想要达到什么目的。
  • 对不起。我对 Web Develope 完全陌生。我想在前面的 bootsrtap 模式中使用 C# 中定义的按钮。通过单击一个按钮,我想要一个弹出窗口。但是这个按钮是在 C# 的后面定义的。而且我不知道如何使用前面的那个按钮。
【解决方案2】:

确实 System.Web.UI.WebControls.ButtonField 不包含名为 CssClass 的属性,可以通过以下方式设置类:

buttonField.ControlStyle.CssClass

此外,如果您需要向 ButtonField 添加自定义属性,则需要在网格的事件 RowDataBound 中执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多