+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / RadGrid InsertCommand Event Args From UserControl In Control Designer Being Lost

RadGrid InsertCommand Event Args From UserControl In Control Designer Being Lost

3 posts, 0 answered
  1. Francis
    Francis avatar
    5 posts
    Registered:
    06 May 2009
    23 Dec 2010
    Link to this post
    Greetings,
    I'm attempting to build a ControlDesigner that uses a RadGrid for the display and management of a list of links (essentially, they have a target and an image and a blurb of text).  I've constructed a UserControl for the radgrid to use as it's editor for the row data, and everything seems hunky-dorey.  Except, the PerformInsert and Update Commands, that the RadGrid documentation assures me preforms an callback to the rad grid, don't seem to ever actaully call the events, so the grid contents never get updated.  For right now, I don't have any of the persistence logic written, because honestly, I didn't figure on just wiring up the events to take this long.  Please, take a look at what I've done, and let me know if anyone has any ideas what I'm missing:

    LinkBoxesDesigner.cs:
    001.using System;
    002.using System.Collections.Generic;
    003.using System.Linq;
    004.using System.Web;
    005.using System.Web.UI;
    006.using System.ComponentModel;
    007.using Telerik.Cms.Web.UI;
    008.using System.Web.UI.WebControls;
    009.using Telerik.Framework.Web;
    010.using Telerik.Web.UI;
    011. 
    012.namespace lssCorp
    013.{
    014.    public class LinkBoxesDesigner : Telerik.Framework.Web.Design.ControlDesigner
    015.    {
    016.        private string _title;
    017.        private List<LinkBox> _linkBoxes;
    018. 
    019.        public override ITemplate LayoutTemplate
    020.        {
    021.            get
    022.            {
    023.                return this.layoutTemplate;
    024.            }
    025.            set
    026.            {
    027.                this.layoutTemplate = value;
    028.            }
    029.        }
    030. 
    031.        public override string LayoutTemplatePath
    032.        {
    033.            get
    034.            {
    035.                if ( this.ViewState[ "LayoutTemplatePath" ] == null )
    036.                    return "~/ControlTemplates/LinkBoxesDesigner.ascx";
    037.                return this.ViewState[ "LayoutTemplatePath" ] as string;
    038.            }
    039.            set
    040.            {
    041.                this.ViewState[ "LayoutTemplatePath" ] = value;
    042.            }
    043.        }
    044. 
    045.        protected override void LoadControlState( object savedState )
    046.        {
    047.            if ( savedState != null )
    048.            {
    049.                object[ ] state = (object[ ]) savedState;
    050.                base.LoadControlState( state[ 0 ] );
    051.                _title = (string) state[ 1 ];
    052.                _linkBoxes = (List<LinkBox>) state[ 2 ];
    053.            }
    054.        }
    055. 
    056.        protected override object SaveControlState( )
    057.        {
    058.            if ( this._title != null )
    059.            {
    060.                component.Title = this._title;
    061.            }
    062. 
    063.            if ( this._linkBoxes != null )
    064.            {
    065.                component.LinkBoxes = this._linkBoxes;
    066.            }
    067. 
    068.            return new object[ ] {
    069.                base.SaveControlState(),
    070.                this._title,
    071.                this._linkBoxes
    072.            };
    073.        }
    074. 
    075.        public override void RenderBeginTag( HtmlTextWriter writer )
    076.        {
    077.        }
    078. 
    079.        public override void RenderEndTag( HtmlTextWriter writer )
    080.        {
    081.        }
    082. 
    083.        protected override void CreateChildControls( )
    084.        {
    085.            this.Controls.Clear( );
    086. 
    087.            this.InitializeTemplate( );
    088.            this.InitializeComponent( );
    089. 
    090.            LinkBox test = new LinkBox( );
    091.            test.Blurb = "test";
    092.            test.ImageUrl = "test.jpg";
    093.            test.TargetUrl = "hi";
    094. 
    095.            component.LinkBoxes.Add( test );
    096. 
    097.            container.LinkBoxGrid.InsertCommand += new GridCommandEventHandler( LinkBoxGrid_InsertCommand );
    098.            container.LinkBoxGrid.ItemCommand += new GridCommandEventHandler( LinkBoxGrid_ItemCommand );
    099.            container.LinkBoxGrid.ItemInserted += new GridInsertedEventHandler( LinkBoxGrid_ItemInserted );
    100. 
    101.            this.container.LinkBoxGrid.DataSource = component.LinkBoxes;
    102.            this.container.LinkBoxGrid.DataBind( );
    103.             
    104.            this.Controls.Add( this.container );
    105. 
    106.            BindData( );
    107.        }
    108. 
    109.        void LinkBoxGrid_ItemInserted( object sender, GridInsertedEventArgs e )
    110.        {
    111.            throw new NotImplementedException( );
    112.        }
    113. 
    114.        void LinkBoxGrid_ItemCommand( object sender, GridCommandEventArgs e )
    115.        {
    116.            // throw new NotImplementedException( );
    117.        }
    118. 
    119.        void LinkBoxGrid_InsertCommand( object sender, GridCommandEventArgs e )
    120.        {
    121.            throw new NotImplementedException( );
    122.        }
    123. 
    124.        public override void OnSaving( )
    125.        {
    126.            this._title = this.container.TitleTextBox.Text;
    127.            this._linkBoxes = (List<LinkBox>) this.container.LinkBoxGrid.DataSource;
    128. 
    129.            component.Title = this._title;
    130.            component.LinkBoxes = this._linkBoxes;
    131.            base.OnPropertyChanged( EventArgs.Empty );
    132.        }
    133. 
    134. 
    135.        private void BindData( )
    136.        {
    137.            this.container.TitleTextBox.Text = component.Title;
    138.            this.container.LinkBoxGrid.DataSource = component.LinkBoxes;
    139.        }
    140. 
    141.        protected virtual void InitializeTemplate( )
    142.        {
    143.            this.container = new LinkBoxesDesignerContainer( this );
    144.            this.layoutTemplate = ControlUtils.GetTemplate<DefaultLinkBoxesDesignerTemplate>( this.LayoutTemplatePath );
    145.            this.layoutTemplate.InstantiateIn( this.container );
    146.        }
    147. 
    148.        protected virtual void InitializeComponent( )
    149.        {
    150.            if ( base.DesignedControl != null )
    151.            {
    152.                this.component = (ILinkBoxesControl) base.DesignedControl;
    153.                this.properties = TypeDescriptor.GetProperties( component );
    154.            }
    155.        }
    156. 
    157.        protected void linkBoxGrid_CancelCommand( object source, GridCommandEventArgs e )
    158.        {
    159.        }
    160. 
    161.        private ITemplate layoutTemplate;
    162.        private LinkBoxesDesignerContainer container;
    163.        private ILinkBoxesControl component;
    164.        private PropertyDescriptorCollection properties;
    165. 
    166.        protected class DefaultLinkBoxesDesignerTemplate : ITemplate
    167.        {
    168.            public void InstantiateIn( Control container )
    169.            {
    170.                throw new NotImplementedException( "Default control designer not implemented!" );
    171.            }
    172.        }
    173. 
    174.        protected class LinkBoxesDesignerContainer : GenericContainer<LinkBoxesDesigner>
    175.        {
    176.            public LinkBoxesDesignerContainer( LinkBoxesDesigner owner )
    177.                : base( owner, true )
    178.            {
    179.            }
    180. 
    181.            public TextBox TitleTextBox
    182.            {
    183.                get
    184.                {
    185.                    if ( this.titleTextBox == null )
    186.                    {
    187.                        this.titleTextBox = (TextBox) base.FindRequiredControl<Control>( "titleTextBox" );
    188.                    }
    189.                    return this.titleTextBox;
    190.                }
    191.            }
    192. 
    193.            public RadGrid LinkBoxGrid
    194.            {
    195.                get
    196.                {
    197.                    if ( this.linkBoxGrid == null )
    198.                    {
    199.                        this.linkBoxGrid = (RadGrid) base.FindRequiredControl<Control>( "linkBoxGrid" );
    200.                    }
    201.                    return this.linkBoxGrid;
    202.                }
    203.            }
    204. 
    205.            private TextBox titleTextBox;
    206.            private RadGrid linkBoxGrid;
    207. 
    208.        }
    209.    }
    210.}

    LinkBoxesDesigner.ascx:
    01.<%@ Control Language="C#" %>
    02.<%@ Register TagPrefix="sfLib" Namespace="Telerik.Libraries.WebControls" Assembly="Telerik.Libraries" %>
    03.<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" %>
    04.<div class="ctrlProps">
    05.    <table border="0">
    06.        <tr>
    07.            <td>
    08.                <asp:Label Width="100px" ID="titleLabel" runat="server" AssociatedControlID="titleTextBox" Text="Title Text:" />
    09.            </td>
    10.            <td>
    11.                <asp:TextBox ID="titleTextBox" runat="server" Width="250px" />
    12.            </td>
    13.        </tr>
    14.        <tr>
    15.            <td>
    16.                <asp:Label Width="100px" ID="linkBoxLabel" runat="server" AssociatedControlID="linkBoxGrid" Text="Link Boxes:" />
    17.            </td>
    18.            <td>
    19.                <telerik:RadGrid ID="linkBoxGrid" AutoGenerateColumns="false" runat="server" AllowSorting="false" AllowPaging="false" AllowMultiRowEdit="false">
    20.                    <MasterTableView AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="false" CommandItemDisplay="TopAndBottom">
    21.                        <EditFormSettings EditFormType="WebUserControl" UserControlName="~/ControlTemplates/EditLinkBox.ascx">
    22.                            <EditColumn UniqueName="EditCommandColumn">
    23.                            </EditColumn>
    24.                        </EditFormSettings>
    25.                        <Columns>
    26.                            <telerik:GridImageColumn UniqueName="ImageUrl" HeaderText="Image" DataImageUrlFields="ImageUrl">
    27.                            </telerik:GridImageColumn>
    28.                            <telerik:GridBoundColumn UniqueName="Blurb" HeaderText="Blurb" DataField="Blurb">
    29.                            </telerik:GridBoundColumn>
    30.                            <telerik:GridHyperLinkColumn UniqueName="TargetUrl" HeaderText="Target" DataNavigateUrlFields="TargetUrl">
    31.                            </telerik:GridHyperLinkColumn>
    32.                            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="LinkButton" Display="true" EditText="Edit" />
    33.                        </Columns>
    34.                    </MasterTableView>
    35.                </telerik:RadGrid>
    36.            </td>
    37.        </tr>
    38.    </table>
    39.</div>

    EditLinkBox.ascx:
    01.<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EditLinkBox.ascx.cs" Inherits="lssCorp.EditLinkBox" %>
    02.<%@ Register TagPrefix="sfLib" Namespace="Telerik.Libraries.WebControls" Assembly="Telerik.Libraries" %>
    03.<script runat="server">
    04. 
    05.</script>
    06.<table border="0">
    07.    <tr>
    08.        <td>
    09.            <asp:Label Width="100px" ID="imageUrlLabel" runat="server" AssociatedControlID="imageUrlTextBox" Text="Image Url:" />
    10.        </td>
    11.        <td>
    12.            <asp:TextBox ID="imageUrlTextBox" runat="server" Width="250px" Text='<%# DataBinder.Eval( Container, "DataItem.ImageUrl") %>' />
    13.            <sfLib:ButtonSelector ID="sfImageSelector" WindowNavigateUrl="~/Sitefinity/UserControls/Dialogs/ImageDialog.aspx" AssociatedControls="imageUrlTextBox"
    14.                runat="server" ItemTemplatePath="~/Sitefinity/Admin/ControlTemplates/Libraries/Dialogs/ButtonSelector.ascx" />
    15.        </td>
    16.    </tr>
    17.    <tr>
    18.        <td>
    19.            <asp:Label Width="100px" ID="blurbLabel" runat="server" AssociatedControlID="blurbTextBox" Text="Header Blurb:" />
    20.        </td>
    21.        <td>
    22.            <asp:TextBox ID="blurbTextBox" TextMode="MultiLine" runat="server" Width="250px" Rows="4" Text='<%# DataBinder.Eval( Container, "DataItem.Blurb") %>' />
    23.        </td>
    24.    </tr>
    25.    <tr>
    26.        <td>
    27.            <asp:Label Width="100px" ID="targetLabel" runat="server" AssociatedControlID="targetTextBox" Text="Target:" />
    28.        </td>
    29.        <td>
    30.            <asp:TextBox ID="targetTextBox" runat="server" Width="250px" Text='<%# DataBinder.Eval( Container, "DataItem.TargetUrl") %>' />
    31.        </td>
    32.    </tr>
    33.    <tr>
    34.        <td colspan="2">
    35.            <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>' />
    36.            <asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert" Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>' />
    37.              
    38.            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" />
    39.        </td>
    40.    </tr>
    41.</table>

    EditLinkBox.ascx.cs:
    01.using System;
    02.using System.Collections.Generic;
    03.using System.Linq;
    04.using System.Web;
    05.using System.Web.UI;
    06.using System.Web.UI.WebControls;
    07. 
    08.namespace lssCorp
    09.{
    10.    public partial class EditLinkBox : System.Web.UI.UserControl
    11.    {
    12.        private object _dataItem = null;
    13. 
    14.        public object DataItem
    15.        {
    16.            get
    17.            {
    18.                return this._dataItem;
    19.            }
    20.            set
    21.            {
    22.                this._dataItem = value;
    23.            }
    24.        }
    25. 
    26.        protected void Page_Load( object sender, EventArgs e )
    27.        {
    28. 
    29.        }
    30. 
    31.        private void InitializeComponent( )
    32.        {
    33.            this.DataBinding += new System.EventHandler( this.EditLinkBox_DataBinding );
    34.        }
    35. 
    36.        protected void EditLinkBox_DataBinding( object sender, EventArgs e )
    37.        {
    38. 
    39.        }
    40.    }
    41.}

    Thank you in advance for your time,
    Francis
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    24 Dec 2010
    Link to this post
    Hello Francis,

    You have to use NeedDataSource and subscribe for this event and InsertCommand inside the control designer code behind

    using System;
    using Telerik.Web.UI;
    using Telerik.Cms.Engine;
     
    public partial class Controls_SitefinityControlDesigher : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
     
            RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
            RadGrid1.InsertCommand += new GridCommandEventHandler(RadGrid1_InsertCommand);
     
        }
     
        void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            // sample datasource
            var manager = new ContentManager("Generic_Content");
            RadGrid1.DataSource = manager.GetContent();
        }
     
        void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
     
        }
     
    }


    Regards,
    Ivan Dimitrov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. Francis
    Francis avatar
    5 posts
    Registered:
    06 May 2009
    24 Dec 2010
    Link to this post
    Thank you so much, that's exactly what I needed to do.

    Thank you again,
    Francis
Register for webinar
3 posts, 0 answered