[CLOSED] Creating Custom Control From Ext.Net.Label in MVC 4.0

  1. #1

    [CLOSED] Creating Custom Control From Ext.Net.Label in MVC 4.0

    Hi,
    I am trying to create custom properties in a custom control derived from Ext.Net.Label. I am able to create a custom control and able to access the inherent properties of Ext.NET, But when I try to add a new property(LabelText) in the derived class like @Html.Y().ExtLabel().LabelText("Hello")
    , it's not showing up on the extension method. Here is the code.

    Derived Class:

    namespace Ext.Net.Extn.Extensions
    {
        public partial class ExtLabel : Label
        {
            public ExtLabel() : base() { }
            
            private string _LabelText;
    
            //Custom Property
            public string LabelText
            {
                get { return _LabelText; }
                set { _LabelText = value; }
            }
        }
    
        public partial class ExtLabel 
        {
            
            //public Builder ToBuilder()
            //{
            //    return new ControlFactory().ExtLabel();
            //}
    
            public class Builder : Builder<ExtLabel, Builder>
            {
                public Builder(ExtLabel label) : base(label) { }
                //public Builder() : base(new ExtLabel()) { }
            }
        }
    }
    Extension: For accessing custom control under Y() extention
    public class Y { }
            public static class YExtensions 
            { 
                public static ControlFactory Y(this HtmlHelper helper) 
                { 
                    return new ControlFactory { HtmlHelper = helper }; 
                } 
            }
    ControlFactory: For creating Builder class of tghe custom control
    public ExtLabel.Builder ExtLabel()
            {
                return BuildControl<ExtLabel, ExtLabel.Builder>(new ExtLabel
                {
                    ID = Guid.NewGuid().ToString("N"),
                    ViewContext = HtmlHelper != null ? HtmlHelper.ViewContext : null
                });
            }
    Factory: Generic method for actually creating the builder class.
    public HtmlHelper _HtmlHelper { get; set; }
    
            public TBuilder BuildControl<TControl, TBuilder>(TControl control)
            {
                return (TBuilder)Activator.CreateInstance(typeof(TBuilder), control);
            }
    The above aproach is working fine for
    Last edited by Baidaly; Jun 12, 2013 at 6:06 PM. Reason: [CLOSED]
  2. #2

    Any help would be good

    Hi, Any help would be good.
  3. #3
    Hello, Any progress on this ?????? It's going to be one month time, since I posted this question.
  4. #4
    Moving to the Premium forum
    Please post any questions in the Premium forum otherwise we can miss it
    Vladimir Shcheglov
    Sr. Developer
  5. #5
    Hello!

    We are investigating.
  6. #6
    Hi @randhir,

    Hope the following helps.

    ControlFactory.cs (analog of BuilderFactory.cs in Ext.NET)
    using System.Web.Mvc;
    
    namespace Ext.Net.MyExtensions
    {
        public partial class ControlFactory
        {
            public HtmlHelper HtmlHelper
            {
                get;
                set;
            }
        }
    
        public partial class ControlFactory<TModel> : ControlFactory
        {
            HtmlHelper<TModel> htmlHelper;
            new public HtmlHelper<TModel> HtmlHelper
            {
                get
                {
                    return this.htmlHelper;
                }
                set
                {
                    this.htmlHelper = value;
                    base.HtmlHelper = value;
                }
            }
        }
    
        public class Y
        {
            private static ControlFactory builder = null;
    
            public static ControlFactory Builder
            {
                get
                {
                    if (Y.builder == null)
                    {
                        Y.builder = new ControlFactory();
                    }
    
                    return Y.builder;
                }
            }
    
            internal static ControlFactory BuilderHelper(HtmlHelper htmlHelper)
            {
                return new ControlFactory { HtmlHelper = htmlHelper };
            }
    
            internal static ControlFactory<TModel> BuilderHelper<TModel>(HtmlHelper<TModel> htmlHelper)
            {
                return new ControlFactory<TModel> { HtmlHelper = htmlHelper };
            }
        }  
    }
    YExtensions.cs (analog of GeneralExtensions.cs in Ext.NET)
    using System.Web.Mvc; 
    
    namespace Ext.Net.MyExtensions
    {
        public static class YExtensions
        {
            public static ControlFactory Y(this HtmlHelper htmlHelper)
            {
                Ext.Net.MyExtensions.Y.Builder.HtmlHelper = htmlHelper;
                return Ext.Net.MyExtensions.Y.BuilderHelper(htmlHelper);
            }
    
            public static ControlFactory<TModel> Y<TModel>(this HtmlHelper<TModel> htmlHelper)
            {
                Ext.Net.MyExtensions.Y.Builder.HtmlHelper = htmlHelper;
                return Ext.Net.MyExtensions.Y.BuilderHelper<TModel>(htmlHelper);
            }
        }
    }
    ExtLabel.cs
    namespace Ext.Net.MyExtensions
    {
        public partial class ExtLabel : Label
        {
            public ExtLabel() : base() { }
    
            private string _LabelText;
    
            public string LabelText
            {
                get 
                { 
                    return _LabelText; 
                }
                set 
                { 
                    _LabelText = value;
                    this.Text = value; // for sake of testing
                }
            }
        }
    }
    ExtLabelBuilder.cs
    namespace Ext.Net.MyExtensions
    {
        public partial class ExtLabel
        {
            new public partial class Builder<TExtLabel, TBuilder> : Label.Builder<TExtLabel, TBuilder>
                where TExtLabel : ExtLabel
                where TBuilder : Builder<TExtLabel, TBuilder>
            {
                public Builder(TExtLabel component) : base(component) { }
    
                public virtual TBuilder LabelText(string labelText)
                {
                    this.ToComponent().LabelText = labelText;
                    return this as TBuilder;
                }
            }
    
            public partial class Builder : ExtLabel.Builder<ExtLabel, ExtLabel.Builder>
            {
                public Builder() : base(new ExtLabel()) { }
    
                public Builder(ExtLabel component) : base(component) { }
            }
        }
    
        public partial class ControlFactory
        {
            public ExtLabel.Builder ExtLabel()
            {
                return this.ExtLabel(new ExtLabel { ViewContext = this.HtmlHelper != null ? this.HtmlHelper.ViewContext : null });
            }
    
            public ExtLabel.Builder ExtLabel(ExtLabel component)
            {
                component.ViewContext = this.HtmlHelper != null ? this.HtmlHelper.ViewContext : null;
    
                return new ExtLabel.Builder(component);
            }
        }
    }
    Usage
    @using Ext.Net.MyExtensions;
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>  
    </head>
    <body>
        @Html.X().ResourceManager()
    
        @Html.Y().ExtLabel().LabelText("Hello!")
    </body>
    </html>
  7. #7
    Thanks Daniil, That helped.
  8. #8
    It is also possible to get helper methods for your custom controls directly in Html.X(). Please see the "Creating Razor helper methods for custom controls" tutorial:
    http://forums.ext.net/showthread.php?27672

Similar Threads

  1. [CLOSED] Ext.net.Label: creating with xtype
    By supera in forum 2.x Premium Help
    Replies: 2
    Last Post: Oct 16, 2012, 5:53 PM
  2. [CLOSED] Creating custom control on server side
    By AnulekhaK in forum 1.x Premium Help
    Replies: 11
    Last Post: Feb 21, 2012, 10:15 AM
  3. [CLOSED] Creating custom control
    By jchau in forum 1.x Premium Help
    Replies: 3
    Last Post: Dec 29, 2010, 6:03 PM
  4. [CLOSED] creating a custom class?
    By smmille1 in forum 1.x Premium Help
    Replies: 10
    Last Post: Jul 09, 2010, 9:31 PM
  5. [CLOSED] First steps creating ext:Label derived control
    By dlouwers in forum 1.x Premium Help
    Replies: 9
    Last Post: Jan 20, 2009, 12:35 PM

Tags for this Thread

Posting Permissions