Strongly Typed Controls and DynamicControl/Dynamic Field

2 posts, 0 answers
  1. David
    David avatar
    37 posts
    Member since:
    Aug 2014

    Posted 01 Feb Link to this post

    Using Entity Framework, Models, DataAnnotation and ASP WebForms, you can leverage the asp 'DynamicControl' and 'DynamicField' controls, which refer back to the model declaration and reflect the proper formatting and validation rules without having to duplicate those values in the markup or code behind.

     

    The article, http://www.asp.net/web-forms/overview/older-versions-getting-started/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-8, goes into detail on how to get this to work with the basic ASP gridview and dataform controls.

     

    Attempting to apply this technique to the RadDataForm, a control that can be strongly typed, results in the error: "The DynamicControl/DynamicField needs to exist inside a data control that is bound to a data source that supports Dynamic Data."

    Huh? The RadDataForm doesn't support Dynamic Data?

    Google search is frustrating due to the search returns dominated by the topic of "Dynamic Control" instead of "DynamicControl", and searched on this forum came up empty.

    How can I place a DynamicControl into the EditItemTemplate of a RadDataForm and benefit from the formatting and other annotations listed on the Model?

     

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1795 posts

    Posted 04 Feb Link to this post

    Hi David,

    Although that we have resolved the issue in the support ticket that you have opened, I will post the answer here as well:

    "Please note that (as with any other data bound control), you need to call the EnableDynamicData method within the Page's Init or Load events for the control with the correct model type:
    <telerik:RadAjaxPanel runat="server">
        <telerik:RadGrid runat="server" ID="RadGrid1" DataSourceID="LinqDataSource1" AllowPaging="true">
            <MasterTableView AutoGenerateColumns="false">
                <Columns>
                    <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <asp:DynamicControl ID="DynamicControl1" DataField="ID" runat="server" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DynamicControl ID="DynamicControl1" Mode="Edit" DataField="ID" runat="server" />
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
      
        <telerik:RadDataForm runat="server" ID="RadDataForm1"></telerik:RadDataForm>
      
        <asp:LinqDataSource
            ContextTypeName="ItemsCollection"
            TableName="AllItems"
            ID="LinqDataSource1"
            runat="server">
        </asp:LinqDataSource>
    </telerik:RadAjaxPanel>

    And the code-behind:
    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid1.EnableDynamicData(typeof(CustomType));
          
    }

    And the model:
    public class ItemsCollection
    {
        public ItemsCollection()
        {
        }
      
        public List<CustomType> AllItems
        {
            get
            {
                List<CustomType> _availableGenres = new List<CustomType>();
                for (int i = 0; i < 500; i++)
                {
                    _availableGenres.Add(new CustomType(i, "value" + i));
                }
                return _availableGenres;
            }
        }
    }
      
    public class CustomType
    {
        [Required(ErrorMessage = "Last name is required.")]
        [Range(0, 5)]
        public int ID { get; set; }
        public string Value { get; set; }
      
        public CustomType()
        {
      
        }
      
        public CustomType(int id, string value)
        {
            this.ID = id;
            this.Value = value;
        }
    }



    Regards,
    Konstantin Dikov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top