locked
Property value is not set in constructor RRS feed

  • Question

  • User-534664927 posted

    Hi All,

       
    I've created custom TemplateField and added new property ControlType which specifies if insert/edit item is textbox ,dropdownlist etc.

    However, during constructor call ControlType property is not set yet and always have default value. When ControlType property is already set? (Initialize method is one of them but that's too late)

    My code:

     

      <CustomGridView:CCustomGridView runat="server"  ID="customGridView"  AutoGenerateColumns="false" InsertRowActive="false" DataSourceID="dataSource" DataKeyNames="Id">
    <Columns>

    <CustomGridView:CustomTemplateField HeaderText="BuySell" ControlType="DropDownList">
    </CustomGridView:CustomTemplateField>
       
    // Custom TemplateField:
    
        public class CustomTemplateField : TemplateField
    {


    public CustomTemplateField() : base()
    {
    if (ControlType == ControlTypes.DropDownList)
    {
    EditItemTemplate = new DDLItemTemplate();
    InsertItemTemplate = new DDLItemTemplate();
    }
    ItemTemplate = new ViewItemTemplate();
    }

    public enum ControlTypes
    {
    DropDownList,
    CheckBox,
    TextBox
    }


    [Category("Behaviour")]
    [Description("Control used inside field")]
    public ControlTypes ControlType
    {
    get { if (this.ViewState["ControlType"] == null)
    return ControlTypes.TextBox;
    return (ControlTypes)this.ViewState["ControlType"];
    }
    set { this.ViewState["ControlType"] = value;
    }
    }

    }
     

     

     

    Wednesday, June 4, 2008 2:13 PM

All replies

  • User1191518856 posted

    ASP.NET will always use the constructor without parameters when creating (user) controls. What happens next is that the properties you've set in the aspx will be set. And then, the control is added to the page controls and it will follow the chain of page events.

    So I suggest you move your constructor code to OnInit method.

    Wednesday, June 4, 2008 6:11 PM
  • User-534664927 posted

    ASP.NET will always use the constructor without parameters when creating (user) controls. What happens next is that the properties you've set in the aspx will be set. And then, the control is added to the page controls and it will follow the chain of page events.

    So I suggest you move your constructor code to OnInit method.

    Thank you johran. Unfortunately, OnInit is not part of  the TemplateField class.

    Wednesday, June 4, 2008 9:31 PM