More in this section

Forums / Developing with Sitefinity / RadGrid in a TypeEditor Window not working

RadGrid in a TypeEditor Window not working

4 posts, 0 answered
  1. Mike
    Mike avatar
    9 posts
    Registered:
    15 Aug 2007
    20 Sep 2007
    Link to this post
    I already posted on this control, but seeing as this seems to be a radGrid problem I thought I'd create a new thread for it.

    I am creating a "Related Resources" control which basically allows the user to create a list of links.  (I tried to insert an image of this, I don't know if it worked)

    I created a typeEditor which contains a RadGrid bound to the database by a Guid [ParentKey] which is stored in a property of the object (I hardcoded it for testing).  The table:

    CREATE TABLE [dbo].[Links]( 
        [ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Links_ID]  DEFAULT (newid()), 
        [ParentKey] [uniqueidentifier] NULL
        [Text] [varchar](500) NULL
        [Url] [varchar](500) NULL
        [OpenNewWindow] [bitNOT NULL CONSTRAINT [DF_Links_OpenNewWindow]  DEFAULT ((0)), 
     CONSTRAINT [PK_Links] PRIMARY KEY CLUSTERED  
        [ID] ASC 
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY
    ON [PRIMARY

    The radgrid contains fields for edit/update/insert/delete, text, url, and a checkbox for OpenNewWindow (again, I tried to attach a screenshot).

    The problem is that the RadGrid fails to recognize any event other than delete.  Clicking on update, cancel or delete all result in a deleted record.  Inserting a new record inserts and then deletes the record.

    I created another radgrid outside of the editor window using the EXACT SAME CODE hardcoded to the same Guid.  This one works as expected.  I will attach both examples: Example 1 Editor Class:
    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 
    using System.Collections.Generic; 
    using Telerik.Cms.Web.UI; 
    using System.ComponentModel; 
    using System.Runtime.Serialization; 
    using Sovereign.BusinessLayer.Objects; 
    using System.Runtime.Serialization.Formatters.Binary; 
    using System.IO; 
    using Telerik.WebControls; 
    using System.Collections; 
     
     
    /// <summary> 
    /// Type editor control that let's user create / edit complex properties of controls. In this case 
    /// user can select category of quotes to be displayed and return the value to QuoteList control. 
    /// </summary> 
    /// 
    namespace Sovereign.BusinessLayer.Logic.Editors 
        public class LinkListEditor3 : WebUITypeEditor<string
        { 
            #region :::Constructors:::::::::::::::: 
     
            public LinkListEditor3() 
            { 
                if (ControlId == null) ControlId = new Guid(HttpContext.Current.Session[this.GetType().ToString()].ToString()); 
            } 
            #endregion 
            #region :::Properties::::::::::::::::: 
     
            private Guid ControlId; 
            private RadGrid radGrid; 
     
            /// <summary> 
            /// Overriden property of WebUITypeEditor. When user clicks "I'm done" this value is being sent 
            /// back to the property that opened this editor. 
            /// </summary> 
            public override string Value 
            { 
                get { return ControlId.ToString(); } 
                set { ControlId = new Guid(value); UpdateViewState(); } 
            } 
            #endregion 
            #region :::Methods::::::::::::::::::::: 
     
            /// <summary> 
            /// Called by ASP.NET. Creates child controls for this control. 
            /// </summary> 
            protected override void CreateChildControls() 
            { 
                this.Controls.Clear(); 
     
                // NOTE : this control can be much more complex in a real scenario. You could implement for example 
                // gridview and bind it to a database and then let user to select a value.  
     
                radGrid = GetMyGrid(); 
                this.Controls.Add(radGrid); 
                radGrid.DataBind(); 
            } 
     
            private void UpdateViewState() 
            { 
                HttpContext.Current.Session[this.GetType().ToString()] = ControlId; 
            } 
     
            private RadGrid GetMyGrid() 
            { 
                if (radGrid != nullreturn radGrid; 
     
                SqlDataSource DataSource = new SqlDataSource(Logging.ConnectionString, @"
                    SELECT ID, ParentKey, Text, Url, OpenNewWindow FROM Links WHERE (ParentKey = @ParentKey)"); 
                DataSource.SelectParameters.Add(new Parameter("ParentKey", TypeCode.String, "E32814F4-66F1-11DC-99EF-1C5656D89593")); 
                DataSource.ID = "SqlDataSource1"
     
                // Insert Parameters 
                DataSource.InsertCommand = @"
                    INSERT INTO [SitefinitySovereign].[dbo].[Links]
                        ([ParentKey], [Text], [Url], [OpenNewWindow])
                    VALUES (@ParentKey, @Text, @Url, @OpenNewWindow)"
                DataSource.InsertParameters.Add(new Parameter("ParentKey", TypeCode.String, "E32814F4-66F1-11DC-99EF-1C5656D89593"));//ControlId.ToString())); 
                DataSource.InsertParameters.Add(new Parameter("Text")); 
                DataSource.InsertParameters.Add(new Parameter("Url")); 
                DataSource.InsertParameters.Add(new Parameter("OpenNewWindow")); 
     
                // Update Parameters 
                DataSource.UpdateCommand = @"
                    UPDATE [SitefinitySovereign].[dbo].[Links]
                    SET [Text] = @Text,
                        [Url] = @Url,
                        [OpenNewWindow] = @OpenNewWindow
                    WHERE ID = @ID"
                DataSource.UpdateParameters.Add(new Parameter("Text")); 
                DataSource.UpdateParameters.Add(new Parameter("Url")); 
                DataSource.UpdateParameters.Add(new Parameter("OpenNewWindow")); 
                DataSource.UpdateParameters.Add(new Parameter("ID")); 
     
                // Delete Parameters 
                DataSource.DeleteCommand = @"
                    DELETE FROM [SitefinitySovereign].[dbo].[Links] WHERE ID = @ID"
                DataSource.DeleteParameters.Add(new Parameter("ID")); 
     
                // The RadGrid 
                radGrid = new RadGrid(); 
                radGrid.AllowAutomaticDeletes = true
                radGrid.AllowAutomaticInserts = true
                radGrid.AllowAutomaticUpdates = true
                radGrid.ID = "RadGrid1"
                radGrid.AllowPaging = true
                radGrid.DataSource = DataSource; 
                radGrid.EnableAJAX = true
                radGrid.GridLines = GridLines.None; 
     
                radGrid.MasterTableView.AutoGenerateColumns = false
                radGrid.MasterTableView.DataKeyNames = new string[] { "ID""ParentKey" }; 
                radGrid.MasterTableView.DataSource = DataSource; 
                radGrid.MasterTableView.PageSize = 20; 
                radGrid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Bottom; 
                radGrid.MasterTableView.EditMode = GridEditMode.InPlace; 
     
                radGrid.MasterTableView.Columns.Add(new GridEditCommandColumn()); 
     
                GridBoundColumn BoundColumn = new GridBoundColumn(); 
                BoundColumn.DataField = "Text"
                BoundColumn.HeaderText = "Display Text"
                BoundColumn.SortExpression="Text"
                BoundColumn.UniqueName="Text"
                radGrid.MasterTableView.Columns.Add(BoundColumn); 
     
                BoundColumn = new GridBoundColumn(); 
                BoundColumn.DataField = "Url"
                BoundColumn.HeaderText = "Url"
                BoundColumn.SortExpression="Url"
                BoundColumn.UniqueName="Url"
                radGrid.MasterTableView.Columns.Add(BoundColumn); 
     
                GridCheckBoxColumn CheckBoxColumn = new GridCheckBoxColumn(); 
                CheckBoxColumn.DataField = "OpenNewWindow"
                CheckBoxColumn.DataType = typeof(Boolean); 
                CheckBoxColumn.HeaderText = "Open New Window?"
                CheckBoxColumn.SortExpression="OpenNewWindow"
                CheckBoxColumn.UniqueName="OpenNewWindow"
                radGrid.MasterTableView.Columns.Add(CheckBoxColumn); 
     
                GridButtonColumn DeleteColumn = new GridButtonColumn(); 
                DeleteColumn.CommandName = "Delete"
                DeleteColumn.Text = "Delete"
                DeleteColumn.UniqueName = "column"
                radGrid.MasterTableView.Columns.Add(DeleteColumn); 
     
                radGrid.MasterTableView.ExpandCollapseColumn.Visible = false
                radGrid.MasterTableView.RowIndicatorColumn.Visible = false
     
                return radGrid; 
            } 
            #endregion 
        } 

    Example 1 Custom Control:
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Text; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using Telerik.Cms.Web.UI; 
    using Sovereign.BusinessLayer.Objects; 
    using Sovereign.BusinessLayer.Logic; 
     
    namespace Sovereign.BusinessLayer.WebControls 
        [DefaultProperty("Configuration")] 
        [ToolboxData("<{0}:NextSteps runat=server></{0}:NextSteps>")] 
        public class NextSteps : WebControl 
        { 
            #region :::Properties:::::::::::: 
     
            //[Bindable(true)] 
            //[Category("Appearance")] 
            //[DefaultValue("")] 
            //[Localizable(true)] 
            //public string Text 
            //{ 
            //    get 
            //    { 
            //        String s = (String)ViewState["Text"]; 
            //        return ((s == null) ? String.Empty : s); 
            //    } 
     
            //    set 
            //    { 
            //        ViewState["Text"] = value; 
            //    } 
            //} 
     
            private Guid _linkListGuid; 
     
            //[WebEditor("LinkListEditor")] 
            //public string ActionLink 
            //{ 
            //    get { return (_link == null)? null : _link.ToString(); } 
            //    set { _link = new LinkList(value); } 
            //} 
     
            //[Bindable(true)] 
            [Category("Appearance")] 
            [DefaultValue("")] 
            [Localizable(true)] 
            //[TypeConverter(typeof(ExtCollectionConverter)), WebEditor("Sovereign.BusinessLayer.Logic.Editors.LinkListEditor, Sovereign.BusinessLayer")] 
            [WebEditor("Sovereign.BusinessLayer.Logic.Editors.LinkListEditor3, Sovereign.BusinessLayer")] 
            public string Configuration 
            { 
                get { if (_linkListGuid == Guid.Empty) _linkListGuid = Guid.NewGuid(); return _linkListGuid.ToString(); } 
                set { try { _linkListGuid = new Guid(value); } catch (Exception) { _linkListGuid = Guid.NewGuid(); } } 
            } 
            //public string Configuration 
            //{ 
            //    get { if (_configuration == null) _configuration = new LinkList(); return _configuration.ToBase64String(); } 
            //    set { _configuration = new LinkList(value); } 
            //} 
     
            private string _title; 
     
            //[Bindable(true)] 
            [Category("Appearance")] 
            [DefaultValue("Next Steps")] 
            [Localizable(true)] 
            public string Title 
            { 
                get { return _title; } 
                set { _title = value; } 
            } 
            #endregion 
     
            protected override void RenderContents(HtmlTextWriter output) 
            { 
                if (_linkListGuid == Guid.Empty) return
     
                //Get Privacy Policy 
                string LinkTag; 
                StringBuilder builder = new StringBuilder(2048); 
                foreach (Link link in LinkList.GetListForControl(_linkListGuid)) 
                { 
                    if (String.IsNullOrEmpty(link.Url)) 
                    { 
                        LinkTag = String.Empty; 
                    } 
                    else if (link.OpenNewWindow) 
                    { 
                        LinkTag = "<a title=\"{1}\" href=\"#\" onclick=\"javascript:window.open('{0}','','width=790,height=530,left=0,top=0')\">"
                    } 
                    else 
                    { 
                        LinkTag = "<a title=\"{1}\" href=\"{0}\">"
                    } 
                    LinkTag = String.Format(LinkTag, link.Url, (link.DisplayDisclaimer) ? Utilities.PrivacyPolicy : link.Text); 
                    builder.AppendFormat("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"padding-top: 10px;\">" 
                        + "<tr><td valign=\"top\">{0}<img width=\"8\" vspace=\"2\" height=\"7\" border=\"0\" src=\"/img/double_arrow.gif\" " 
                        + "alt=\"\" title=\"\" />{2}</td><td style=\"padding-left: 4px;\">{0}{1}{2}</td></tr></table>"
                        LinkTag, link.Text, ((String.IsNullOrEmpty(LinkTag)) ? "" : "</a>")); 
                } 
     
                output.Write(String.Format(@"
                    <div id=""NextStepsContainer"" style=""padding: 10px 10px 15px; background-color: #e8e3da; width: 165px;"">
                        <span class=""red11"">{0}</span>
                        {1}
                    </div>", _title ?? "Next Steps", builder.ToString())); 
            } 
        } 
     

    Example 2 CodeBehind (renders to a div tag with id of 'divControl':
    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Collections; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 
    using Telerik.WebControls; 
    using Sovereign.BusinessLayer; 
     
    public partial class radgrid : System.Web.UI.Page 
        private RadGrid radGrid; 
     
     
         
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (radGrid != nullreturn
     
            SqlDataSource DataSource = new SqlDataSource(Logging.ConnectionString, @"
                    SELECT ID, ParentKey, Text, Url, OpenNewWindow FROM Links WHERE (ParentKey = @ParentKey)"); 
            DataSource.SelectParameters.Add(new Parameter("ParentKey", TypeCode.String, "E32814F4-66F1-11DC-99EF-1C5656D89593")); 
            DataSource.ID = "SqlDataSource1"
     
            // Insert Parameters 
            DataSource.InsertCommand = @"
                    INSERT INTO [SitefinitySovereign].[dbo].[Links]
                        ([ParentKey], [Text], [Url], [OpenNewWindow])
                    VALUES (@ParentKey, @Text, @Url, @OpenNewWindow)"
            DataSource.InsertParameters.Add(new Parameter("ParentKey", TypeCode.String, "E32814F4-66F1-11DC-99EF-1C5656D89593"));//ControlId.ToString())); 
            DataSource.InsertParameters.Add(new Parameter("Text")); 
            DataSource.InsertParameters.Add(new Parameter("Url")); 
            DataSource.InsertParameters.Add(new Parameter("OpenNewWindow")); 
     
            // Update Parameters 
            DataSource.UpdateCommand = @"
                    UPDATE [SitefinitySovereign].[dbo].[Links]
                    SET [Text] = @Text,
                        [Url] = @Url,
                        [OpenNewWindow] = @OpenNewWindow
                    WHERE ID = @ID"
            DataSource.UpdateParameters.Add(new Parameter("Text")); 
            DataSource.UpdateParameters.Add(new Parameter("Url")); 
            DataSource.UpdateParameters.Add(new Parameter("OpenNewWindow")); 
            DataSource.UpdateParameters.Add(new Parameter("ID")); 
     
            // Delete Parameters 
            DataSource.DeleteCommand = @"
                    DELETE FROM [SitefinitySovereign].[dbo].[Links] WHERE ID = @ID"
            DataSource.DeleteParameters.Add(new Parameter("ID")); 
     
            // The RadGrid 
            radGrid = new RadGrid(); 
            radGrid.AllowAutomaticDeletes = true
            radGrid.AllowAutomaticInserts = true
            radGrid.AllowAutomaticUpdates = true
            radGrid.ID = "RadGrid1"
            radGrid.AllowPaging = true
            radGrid.DataSource = DataSource; 
            radGrid.EnableAJAX = true
            radGrid.GridLines = GridLines.None; 
     
            radGrid.MasterTableView.AutoGenerateColumns = false
            radGrid.MasterTableView.DataKeyNames = new string[] { "ID""ParentKey" }; 
            radGrid.MasterTableView.DataSource = DataSource; 
            radGrid.MasterTableView.PageSize = 20; 
            radGrid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Bottom; 
            radGrid.MasterTableView.EditMode = GridEditMode.InPlace; 
     
            radGrid.MasterTableView.Columns.Add(new GridEditCommandColumn()); 
     
            GridBoundColumn BoundColumn = new GridBoundColumn(); 
            BoundColumn.DataField = "Text"
            BoundColumn.HeaderText = "Display Text"
            BoundColumn.SortExpression = "Text"
            BoundColumn.UniqueName = "Text"
            radGrid.MasterTableView.Columns.Add(BoundColumn); 
     
            BoundColumn = new GridBoundColumn(); 
            BoundColumn.DataField = "Url"
            BoundColumn.HeaderText = "Url"
            BoundColumn.SortExpression = "Url"
            BoundColumn.UniqueName = "Url"
            radGrid.MasterTableView.Columns.Add(BoundColumn); 
     
            GridCheckBoxColumn CheckBoxColumn = new GridCheckBoxColumn(); 
            CheckBoxColumn.DataField = "OpenNewWindow"
            CheckBoxColumn.DataType = typeof(Boolean); 
            CheckBoxColumn.HeaderText = "Open New Window?"
            CheckBoxColumn.SortExpression = "OpenNewWindow"
            CheckBoxColumn.UniqueName = "OpenNewWindow"
            radGrid.MasterTableView.Columns.Add(CheckBoxColumn); 
     
            GridButtonColumn DeleteColumn = new GridButtonColumn(); 
            DeleteColumn.CommandName = "Delete"
            DeleteColumn.Text = "Delete"
            DeleteColumn.UniqueName = "column"
            radGrid.MasterTableView.Columns.Add(DeleteColumn); 
     
            radGrid.MasterTableView.ExpandCollapseColumn.Visible = false
            radGrid.MasterTableView.RowIndicatorColumn.Visible = false
     
            //if (!divControl.HasControls()) 
                divControl.Controls.Add(radGrid); 
        } 
     

    I hate to ask alot of questions but it seems like some things that should be easy aren't.  At any rate I appreciate your assistance, and hope that you will continue to make Sitefinity a better product.

    Thanks,
    Mike
  2. Nitin
    Nitin avatar
    51 posts
    Registered:
    14 Aug 2007
    24 Sep 2007
    Link to this post
    Mike,

    I agree with you that certain things should be easy but are not. However, my experience has been that its my lack on understanding/knowledge of Sitefinity architecture that causes unnecessary delays and frustration(s) most of the times.

    I am currently working on a site for one of my clients and will need a user control exactly like you mentioned in this post.

    I would like to collaborate/contribute to the code and help make it work, if you are interested.

    Thanks and good luck!

    Nitin
  3. Yasen
    Yasen avatar
    121 posts
    Registered:
    18 May 2013
    25 Sep 2007
    Link to this post
    Hi Mike,

    You were right, this is a RadGrid problem. When you set radGrid.DataBind() on CreateChildControls, the event information for the RadGrid is reset, so the default event arguments (commandName = "delete") are used. To solve this, you have to DataBind after the events are fired , for example on preRender:

    protected override void OnPreRender(EventArgs e)  
    {  
      base.OnPreRender(e);  
      radGrid.DataBind();  
    I used this code in your first example in LinkListEditor3, don't forget to remove DataBind() from CreateChildControls.

    Hope this solves the problem, we are always willing to help and make it easier for you. Please accept our apologies for the delayed answers this week, we are concentrated on the 3.1 release.

    Greetings,
    Yasen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  4. Mike
    Mike avatar
    9 posts
    Registered:
    15 Aug 2007
    26 Sep 2007
    Link to this post
    Thank you for your assistance.  It now works as expected.

    Nitin if you take my code above, and make the changes suggested by Yasen, it works wonderfully.
Register for webinar
4 posts, 0 answered