[CLOSED] Adding events to custom control

  1. #1

    [CLOSED] Adding events to custom control

    I'm trying to add a new event to my custom control here:

    http://forums.ext.net/showthread.php...ding-GridPanel

    In the init of this control, I create a CheckboxSelectionModel. First, is there a way that I can set the selectionchange event handler for this existing selection model? If not, is there a way to expose that event through my custom class as a new event?
    Last edited by Daniil; Apr 06, 2012 at 10:00 AM. Reason: [CLOSED]
  2. #2
    Hi,

    Quote Originally Posted by jmcantrell View Post
    First, is there a way that I can set the selectionchange event handler for this existing selection model?
    Do you mean DirectEvent?

    Example C#
    selModel.DirectEvents.SelectionChange.Event += SelectionChangeHandler;

    Quote Originally Posted by jmcantrell View Post
    If not, is there a way to expose that event through my custom class as a new event?
    Please follow:
    http://forums.ext.net/showthread.php...ll=1#post50813


    Also you may be interested how to use DirectMethods within a custom control:
    http://forums.ext.net/showthread.php...ll=1#post63709
  3. #3
    Thanks. I'll look at that, b/c i do want to add a corresponding directevent. What I'm trying to do is something like this:

    <xxx:CheckboxPanel runat="server">
        <Listeners>
            <SelectionChange Handler="..." />
        </Listeners>
    </xxx:CheckboxPanel>
  4. #4
    Then the first link should help.
  5. #5
    I'm working through it. Is there a simple example in the ext.net source that might illustrate it?
  6. #6
    I've just prepared an example.

    1. Create the JavaScript class.


    MyGridPanel JavaScript class

    Ext.ux.MyGridPanel = Ext.extend(Ext.net.GridPanel, {
        initEvents : function(){
            Ext.ux.MyGridPanel.superclass.initEvents.call(this);
    
            this.addEvents("selectionchange");
    
            this.relayEvents(this.getSelectionModel(), ["selectionchange"]);
        }
    });
    
    Ext.ComponentMgr.registerType("mygridpanel", Ext.ux.MyGridPanel);
    The JavaScript extend method
    http://docs.sencha.com/ext-js/3-4/#!/api/Ext-method-extend

    Useful JavaScript methods regarding how to adding new events for the JavaSctipt class.

    http://docs.sencha.com/ext-js/3-4/#!/api/Ext.util.Observable-method-addEvents
    http://docs.sencha.com/ext-js/3-4/#!/api/Ext.util.Observable-method-fireEvent
    http://docs.sencha.com/ext-js/3-4/#!/api/Ext.util.Observable-method-relayEvents

    2. Mark this JavaScript file as Embedded Resources.

    3. Add the following to the Assembly.cs.

    [assembly: WebResource("Work.resources.js.MyGridPanel.js", "text/javascript")]
    4. Implement the MyGridPanelListeners class inheriting from the GridPanelListeners one.

    4.1. Add your custom event.

    4.2 Override the ConfigOptions property.

    MyGridPanelListeners class
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Xml.Serialization;
    using Newtonsoft.Json;
    using Ext.Net;
    
    namespace Work
    {
        public class MyGridPanelListeners : GridPanelListeners
        {
            private ComponentListener selectionChange;
    
            /// <summary>
            /// Fires when the selection changes
            /// </summary>
            
    [ListenerArgument(0, "item", typeof(MyGridPanel), "MyGridPanel")]
            [TypeConverter(typeof(ExpandableObjectConverter))]
            [PersistenceMode(PersistenceMode.InnerProperty)]
            [NotifyParentProperty(true)]
            [Description("Fires when the selection changes.")]
            public virtual ComponentListener SelectionChange
            {
                get
                {
                    if (this.selectionChange == null)
                    {
                        this.selectionChange = new ComponentListener();
                    }
    
                    return this.selectionChange;
                }
            }
    
            [Browsable(false)]
            [EditorBrowsable(EditorBrowsableState.Never)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            [XmlIgnore]
            [JsonIgnore]
            public override ConfigOptionsCollection ConfigOptions
            {
                get
                {
                    ConfigOptionsCollection list = base.ConfigOptions;
                    list.Add("selectionChange", new ConfigOption("selectionChange", new SerializationOptions("selectionchange", typeof(ListenerJsonConverter)), null, this.SelectionChange));
    
                    return list;
                }
            }
        }
    }

    5. Implement the MyGridPanel class.


    5.1. Override the XType property.
    http://docs.sencha.com/ext-js/3-4/#!/api/Ext.Component-cfg-xtype

    XType property
    public override string XType
    {
        get
        {
            return "mygridpanel";
        }
    }
    The related code within the JavaScript class:
    Ext.ComponentMgr.registerType("mygridpanel", Ext.ux.MyGridPanel);
    5.2. Override the InstanceOf property.

    The constructor name to instantiate the JavaScript class.

    InstanceOf property
    public override string InstanceOf
    {
        get
        {
            return "Ext.ux.MyGridPanel";
        }
    }
    5.3. Override the Resources properties.

    To load the control resources.

    Resources property
    protected override List<ResourceItem> Resources
    {
        get
        {
            List<ResourceItem> resources = base.Resources;
            resources.Capacity += 1;
            resources.Add(new ClientScriptItem(typeof(MyGridPanel), "Work.resources.js.MyGridPanel.js", ""));
    
            return resources;
        }
    }
    5.4. Override the ConfigOptions property.
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [XmlIgnore]
    [JsonIgnore]
    public override ConfigOptionsCollection ConfigOptions
    {
        get
        {
            ConfigOptionsCollection list = base.ConfigOptions;
    
            list.Add("listeners", new ConfigOption("listeners", new SerializationOptions("listeners", JsonMode.Object), null, this.Listeners));
    
            return list;
        }
    }
    5.5. Implement some logic.
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.SelectionModel.Add(new RowSelectionModel());
    }
    Here is the resulting code.

    MyGridPanel class
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Xml.Serialization;
    using Newtonsoft.Json;
    using Ext.Net;
    
    namespace Work
    {
        public class MyGridPanel : GridPanelBase
        {
            public override string XType
            {
                get
                {
                    return "mygridpanel";
                }
            }
    
            public override string InstanceOf
            {
                get
                {
                    return "Ext.ux.MyGridPanel";
                }
            }
    
            protected override List<ResourceItem> Resources
            {
                get
                {
                    List<ResourceItem> resources = base.Resources;
                    resources.Capacity += 1;
                    resources.Add(new ClientScriptItem(typeof(MyGridPanel), "Work.resources.js.MyGridPanel.js", ""));
    
                    return resources;
                }
            }
    
            private MyGridPanelListeners listeners;
    
            /// <summary>
            /// Client-side JavaScript Event Handlers
            /// </summary>
            [NotifyParentProperty(true)]
            [PersistenceMode(PersistenceMode.InnerProperty)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            [ViewStateMember]
            [Description("Client-side JavaScript Event Handlers")]
            public MyGridPanelListeners Listeners
            {
                get
                {
                    if (this.listeners == null)
                    {
                        this.listeners = new MyGridPanelListeners();
                    }
    
                    return this.listeners;
                }
            }
    
            [Browsable(false)]
            [EditorBrowsable(EditorBrowsableState.Never)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            [XmlIgnore]
            [JsonIgnore]
            public override ConfigOptionsCollection ConfigOptions
            {
                get
                {
                    ConfigOptionsCollection list = base.ConfigOptions;
    
                    list.Add("listeners", new ConfigOption("listeners", new SerializationOptions("listeners", JsonMode.Object), null, this.Listeners));
    
                    return list;
                }
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                this.SelectionModel.Add(new RowSelectionModel());
            }
        }
    }
    6. How to use.

    Test page
    <%@ Page Language="C#" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <%@ Register Assembly="Work" Namespace="Work" TagPrefix="cc" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.MyGridPanel1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "test1", "test2", "test3" },
                    new object[] { "test4", "test5", "test6" },
                    new object[] { "test7", "test8", "test9" },
                };
                store.DataBind();
            }
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.NET Example</title>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
        <cc:MyGridPanel ID="MyGridPanel1" runat="server" AutoHeight="true">
            <Store>
                <ext:Store runat="server">
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="test1" />
                                <ext:RecordField Name="test2" />
                                <ext:RecordField Name="test3" />
                            </Fields>
                        </ext:ArrayReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column Header="Test1" DataIndex="test1" />
                    <ext:Column Header="Test2" DataIndex="test2" />
                    <ext:Column Header="Test3" DataIndex="test3" />
                </Columns>
            </ColumnModel>
            <Listeners>
                <SelectionChange Handler="console.log('MyGridPanel SelectionChange');" />
            </Listeners>
        </cc:MyGridPanel>
    </body>
    </html>
  7. #7
    Wow. Thanks Daniil. I'll look through this. Bookmarked!

Similar Threads

  1. Replies: 2
    Last Post: Jul 29, 2012, 12:19 PM
  2. Replies: 3
    Last Post: May 03, 2012, 3:33 PM
  3. [CLOSED] Getting error while adding custom control to container
    By AnulekhaK in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Feb 20, 2012, 12:59 PM
  4. Replies: 2
    Last Post: Jan 09, 2012, 7:18 AM
  5. [CLOSED] Adding Custom Events
    By conman in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Sep 04, 2009, 8:50 PM

Tags for this Thread

Posting Permissions