简体   繁体   中英

No extension method attribute How can I solve it

When I use

 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; } }

to define buttons in C# I got error said no extension attributes and no extension cssClass.

I tried

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

and

using System.Runtime.CompilerServices.ExtensionAttribute;

but doesn't work. How can I solve it

My exact errors

 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

Extension methods in C# allow you to declare methods that you can call as if they were methods of a class:

public class Button { } 

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

With this extension method, you can call:

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

When you remove the extension method, you get the error message

'Button' does not contain a definition for GetArea and no extension method GetArea accepting a first argument of type Button could be found

Now there probably never was an extension method Attributes or CssClass but the compiler is guessing that it was there once, probably since most programmers won't even try compile when IntelliSense isn't picking up on the member names.

In short: the compiler is telling you that ButtonField does not have these members.

Indeed System.Web.UI.WebControls.ButtonField does not contain a property called CssClass, you can set up the class through:

buttonField.ControlStyle.CssClass

Also if you need to add custom attributes to the ButtonField you need to do that in the event RowDataBound of your grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM