locked
Initialisation of ButtonField RRS feed

  • Question

  • User-511826081 posted

    Hello folks,

    Initialisation of ButtonField

    I have a code which perfectly working:

    ButtonField ORDER_NO = new ButtonField();
    ORDER_NO.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    ORDER_NO.ButtonType = ButtonType.Link;
    ORDER_NO.Visible = true;

    A need to make it a bit shorter: 

    ButtonField ORDER_NO = new ButtonField() {
        ItemStyle.HorizontalAlign = HorizontalAlign.Center,
           ButtonType = ButtonType.Link,
        Visible = true;
    };

    this code are failed on compilation of 

       ItemStyle.HorizontalAlign = HorizontalAlign.Center,

    with Error CS0747 Invalid initializer member declarator

    Did anybody have an idea what is wrong and how to get it compilable?

    Control already inherited and in constructor initialisation are done.
    But I still like to known why direct approach didn't work.

    Wednesday, September 25, 2019 10:59 AM

All replies

  • User288213138 posted

    Hi Andrey Belyakov,

    ItemStyle.HorizontalAlign = HorizontalAlign.Center,

    ItemStyle Property only get the style of any text-based content displayed by a data control field, but cannot set style.

    This is ItemStyle property doc: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.datacontrolfield.itemstyle?view=netframework-4.8

    And others can get or set them property. So that's itemStyle why fail to compile.

    If you need to make it a bit shorter, you can set the ItemStyle property separately outside.

    Best regards,

    Sam

    Thursday, September 26, 2019 5:47 AM
  • User-511826081 posted

    ItemStyle Property only get the style of any text-based content displayed by a data control field, but cannot set style.

    ------

    Good explanation.

    But, sorry, not a proper one.

    In the provided code not used a property setter. Accessed only getter, which supposed to be available... after instance initialized.

    However, this is not a regular code, this is an instance initialization and property, I assume, not ready for use.

    So, to solve the problem, require to define property itself (not a value) and for defined property set required values.

    Solution is:

    new BoundField() { ItemStyle = { HorizontalAlign = HorizontalAlign.Center } }

    Wednesday, October 2, 2019 8:05 AM