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

Forums / Developing with Sitefinity / Getting radgrid items from control designer

Getting radgrid items from control designer

4 posts, 0 answered
  1. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    30 Sep 2010
    Link to this post
    Hi,

    Im developing a control designer and the strangest thing is happening. Throughout my sitefinity project I have used the following code to loop through the rows in a radgrid and establish whether that rows was selected.

    public override void OnSaving() {
     
        // Set the underlying control properties to the new values.
        ((CaseStudySelectorControlDesignerBase)DesignedControl).strControlTitle = oControlTitle.Text;
     
        //setup local variables for use
        Guid guContentID = Guid.Empty;
        List<Guid> guContentIDs = new List<Guid>();
     
        //loop through each row in grid
        foreach (GridDataItem item in this.oSelectedItemsGrid.MasterTableView.Items) {
     
            if (item.Selected == true) {
     
                //add to local variable
                guContentID = new Guid(item["ContentID"].Text);
                guContentIDs.Add(guContentID);
     
            }
     
        }
        ((CaseStudySelectorControlDesignerBase)DesignedControl).ContentIDs = guContentIDs.ToArray();
     
    }

    The Grid has been found, as the loop executes three times which is how many records are within it. But item.Selected is always false even when i have clearly selected that row.

    Any obvious season why this behaviour is different within a control designer?

    Thanks
    higgsy
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 Sep 2010
    Link to this post
    Hello Alastair,

    Sitefinity can save properties in string format. Sitefinity will automatically convert primitive types for you, such as integer or boolean, but what if you need to persist property that is an array or a list you need to create a type converter

    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. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    01 Oct 2010
    Link to this post
    Hi Ivan,

    Thanks for your response. I understand how sitefinity stores data, however the code hasnt got to the point of saving the data yet.

    The markup for my control designer is:
    01.<%@ Control Language="C#" %>
    02.<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    03.<div class="ctrlProps">
    04.    <div class="ctrlContent">
    05.        <h3>Case Study Selection</h3>
    06.        <p>Please choose the Case Studies you wish to appear from the list below.</p>
    07.        <asp:Label ID="lblControlTitle" runat="server" Text="Title of widget: " AssociatedControlID="txtControlTitle" />
    08.        <asp:TextBox ID="txtControlTitle" runat="server" />
    09.        <asp:RequiredFieldValidator Display="Dynamic" runat="server" ControlToValidate="txtControlTitle" Text=" * required!" />
    10.        <br />
    11.        <telerik:RadGrid ID="grdSelectedCaseStudies" runat="server" AllowPaging="false" AutoGenerateColumns="false" Width="750px" AllowMultiRowSelection="true">
    12.            <PagerStyle Mode="NextPrevAndNumeric" />
    13.            <MasterTableView AutoGenerateColumns="false">
    14.                <NoRecordsTemplate>
    15.                    <p>No documents in this library.</p>
    16.                </NoRecordsTemplate>
    17.                <Columns>
    18.                    <telerik:GridClientSelectColumn UniqueName="SelectColumn">
    19.                    </telerik:GridClientSelectColumn>
    20.                    <telerik:GridBoundColumn Display="false" HeaderText="ContentID" UniqueName="ContentID" HeaderStyle-Width="20px" />
    21.                    <telerik:GridBoundColumn HeaderText="Client name" UniqueName="ClientName" HeaderStyle-Width="300px" />
    22.                    <telerik:GridBoundColumn HeaderText="Title" UniqueName="Title" HeaderStyle-Width="430px" />
    23.                </Columns>
    24.            </MasterTableView>
    25.            <ClientSettings EnablePostBackOnRowClick="false">
    26.                <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true" />
    27.                <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True" ScrollHeight="240px" />
    28.            </ClientSettings>
    29.        </telerik:RadGrid>
    30.    </div>
    31.</div>

    And then the code is:
    01.protected override void InitializeControls(System.Web.UI.Control viewContainer) {
    02. 
    03.    oControlTitle.Text = ((CaseStudySelectorControlDesignerBase)DesignedControl).strControlTitle;
    04. 
    05.    #region Register_Events
    06. 
    07.    oSelectedItemsGrid.ItemDataBound += new GridItemEventHandler(oSelectedItemsGrid_ItemDataBound);
    08. 
    09.    #endregion
    10. 
    11.    if (!Page.IsPostBack) {
    12. 
    13.        ContentManager oContentMgr = new ContentManager(CaseStudyManager.DefaultContentProvider);
    14.        IList oContent = oContentMgr.GetContent();
    15.        oSelectedItemsGrid.DataSource = oContent;
    16.        oSelectedItemsGrid.DataBind();
    17. 
    18.    }
    19. 
    20.}
    21. 
    22.public override void OnSaving() {
    23. 
    24.    // Set the underlying control properties to the new values.
    25.    ((CaseStudySelectorControlDesignerBase)DesignedControl).strControlTitle = oControlTitle.Text;
    26. 
    27.    //setup local variables for use
    28.    Guid guContentID = Guid.Empty;
    29.    List<Guid> guContentIDs = new List<Guid>();
    30. 
    31.     
    32. 
    33.    //loop through each row in grid
    34.    foreach (GridDataItem item in this.oSelectedItemsGrid.MasterTableView.Items) {
    35. 
    36.        if (item.Selected == true) {
    37. 
    38.            //add to local variable
    39.            guContentID = new Guid(item["ContentID"].Text);
    40.            guContentIDs.Add(guContentID);
    41. 
    42.        }
    43. 
    44.    }
    45.    ((CaseStudySelectorControlDesignerBase)DesignedControl).ContentIDs = guContentIDs.ToArray();
    46. 
    47.}
    48. 
    49./// <summary>
    50./// Gets a reference to the RadGrid control contained in the LayoutTemplatePath template.
    51./// </summary>
    52.protected virtual RadGrid oSelectedItemsGrid {
    53.    get { return base.Container.GetControl<RadGrid>("grdSelectedCaseStudies", true); }
    54.}

    The problem i am experiencing is within the OnSAving event. I am looping through all of the rows in the grid, and then checking whether the row has been selected - if it has I add the GUID of the content item to an array. It is this array that sitefinity will store. The problem is that when debugging, line 36 (if(item.Selected == true)) is always false.

    I've used this exact code in loads of places throughout the project im working on without any issues, so i am wondering why the behaviour is different within a control designer.

    Are you able to help?
    Thanks
    higgsy
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    01 Oct 2010
    Link to this post
    Hello Alastair,

    I am not able to reproduce this issue in the context of ControlDesgner. I prepared a sample short video and code for you.

    <telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowPaging="true" AllowMultiRowSelection="True">
        <MasterTableView DataKeyNames="ID">
            <Columns>
              <telerik:GridBoundColumn UniqueName="DateCreated" DataField="DateCreated" HeaderText="DateCreated"></telerik:GridBoundColumn>
              <telerik:GridBoundColumn UniqueName="ID" DataField="ID" HeaderText="ID" ></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
          <ClientSettings >
                    <Selecting AllowRowSelect="true" />
          </ClientSettings>
    </telerik:RadGrid>

    ControlDesigner

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Framework.Web.Design;
    using Telerik.Web.UI;
    using Telerik.Cms.Engine;
     
    /// <summary>
    /// Summary description for SitefinityControlDesginer
    /// </summary>
    public class SitefinityControlDesginer : ControlDesigner
    {
        public SitefinityControlDesginer()
        {
        }
     
        public override string LayoutTemplatePath
        {
            get
            {
                return "~/Controls/SitefinityControlDesigher.ascx";
            }
        }
     
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            var manager = new ContentManager("Generic_Content");
            RadGrid1.DataSource = manager.GetContent();
            RadGrid1.DataBind();
        }
     
        public override void OnSaving()
        {
            base.OnSaving();
     
            foreach (Telerik.Web.UI.GridDataItem dataItem in RadGrid1.MasterTableView.Items)
            {
                if (dataItem.Selected == true)
                {
                    var itemID = dataItem.GetDataKeyValue("ID");
                }
            }
     
        }
     
     
        public virtual RadGrid RadGrid1
        {
            get
            {
                return this.Container.GetControl<RadGrid>("RadGrid1", true, Telerik.Framework.Web.TraverseMethod.BreadthFirst);
            }
        }
    }


    All the best,
    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
Register for webinar
4 posts, 0 answered