This is a migrated thread and some comments may be shown as answers.

Problem with selected Rows Grid (loosing State)

2 Answers 31 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 03 May 2010, 03:32 PM
Hello Guys,

i testet some of the functions of your Rad Grid, i can not add the Grid or the Columns
via design mode, they are dynamical added  serverside.
I checked all the Samples that you wrote but can find nothing that fits to my situation.

Sorting stays enabled, but the selected Rows are gone through paging.


Code in ascx

 
<telerik:RadAjaxPanel ID="_panFilec" runat="server" Height="188px" 
    Width="90%" LoadingPanelID="_fileconloadpan" oninit="RadAjaxPanel1_Init" Visible="false" > 
<telerik:RadAjaxLoadingPanel ID="_fileconloadpan" runat="server" Height="75px" 
            Width="75px" Transparency="30"
<img src="/_layouts/images/myTestloader.gif" alt="Loading..." style="border: 0; margin-top: 45px; color:Black;"/> 
</telerik:RadAjaxLoadingPanel> 
    </telerik:RadAjaxPanel> 

Code for Grid:
public partial class TestWebControl : System.Web.UI.UserControl 
    { 
        private const string _LOADINGSOURCE = "/_controltemplates/TestWebControl.ascx"
        private const string _VIEWSTATEITEMID = "VIEWSTATEITEMID"
        private const string _DATASOURCECACHE = "_DATASORCECACHE"
 
        private ICachingOperations _caching; 
        private ITelerikBehavior _telerikbehavior; 
        private ISearchBehavior _search; 
 
        private RadGrid _gridViewContent; 
        private List<GridColumn> _columns; 
 
        public List<GridColumn> Columns 
        { 
            get 
            { 
                 return this._columns; 
            } 
            set 
            { 
                this._columns= value; 
            } 
        } 
        private string _lastchacheDependency; 
 
        private string _chacheDependency; 
 
        public string CacheDependency 
        { 
            get { return _chacheDependency; } 
            set { _chacheDependency = value; } 
        } 
 
        public ISearchBehavior Search 
        { 
            get { return _search; } 
            set { _search = value; } 
        } 
 
        private bool _enableSelectColumn = true
 
        public bool EnableSelectColumn 
        { 
            get { return _enableSelectColumn ; } 
            set { _enableSelectColumn = value; } 
        } 
 
 
        public ITelerikBehavior Telerikbehavior 
        { 
            get 
            { 
                if (_telerikbehavior != null
                { return _telerikbehavior; } 
                else 
                { 
                    this._telerikbehavior = new TestTelerikBehavior(); 
                    return _telerikbehavior; 
                } 
            } 
            set 
            { 
                if (_telerikbehavior != null
                { 
                    _telerikbehavior = value; 
                } 
            } 
        } 
 
        public ICachingOperations Caching 
        { 
            get { 
                 
                if(_caching!=null
                {return _caching;} 
 
                else 
                { 
                    this._caching = new ControlToSession(this.ID); 
                    return _caching; 
                } 
            } 
            set 
            { 
                if (value != null
                {_caching = value;} 
            } 
        } 
 
        public static TestWebControl LoadFileContent(Page page) 
        { 
            return page.LoadControl(_LOADINGSOURCE) as FileContentWebControl; 
        } 
 
        private int pageSize = 5; 
        /// <summary> 
        /// Gets or sets the number of items to show per page 
        /// </summary> 
        public int PageSize 
        { 
            get { return pageSize; } 
            set { pageSize = value; } 
        } 
 
        //public void ResetColumns(List<GridColumn> columns) 
        //{ 
 
        //     
        //} 
 
        protected override void OnInit(EventArgs e) 
        { 
            base.OnInit(e); 
 
            
 
            _gridViewContent = new RadGrid(); 
            _gridViewContent.AutoGenerateColumns = false
            _gridViewContent.Attributes.Add("Width""80%"); 
            _gridViewContent.AllowPaging = true
            _gridViewContent.PageSize = this.PageSize; 
            _gridViewContent.AllowSorting = true
            _gridViewContent.AllowMultiRowSelection = true
            _gridViewContent.PagerStyle.Mode=GridPagerMode.NextPrev; 
            //_gridViewContent.ClientSettings.AllowDragToGroup = true; 
            _gridViewContent.ClientSettings.EnableRowHoverStyle = true
            _gridViewContent.ClientSettings.EnablePostBackOnRowClick = true
            //_gridViewContent.ClientSettings.Selecting.AllowRowSelect = true; 
            //_gridViewContent.ItemCreated += new GridItemEventHandler(_gridViewContent_ItemCreated); 
            _gridViewContent.Skin = this.Telerikbehavior.TelerikSkin; 
            //_gridViewContent.EnableViewState = false; 
            _gridViewContent.NeedDataSource+=new GridNeedDataSourceEventHandler(_gridViewContent_NeedDataSource); 
        } 
 
        void _gridViewContent_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            if (!string.IsNullOrEmpty(_chacheDependency) && Columns != null
            { 
 
                DataTable datssource = getData(); 
                if (source != null && datssource.Rows.Count > 0) 
                { 
                     
                  _gridViewContent.DataSource = datssource; 
  
                } 
                else 
                { 
                    _gridViewContent.DataSource = null
                } 
            } 
            else 
            { 
                _gridViewContent.DataSource = null
            } 
        } 
 
        //void _gridViewContent_ItemCreated(object sender, GridItemEventArgs e) 
        //{ 
 
        //    if (e.Item is GridDataItem) 
        //    { 
        //        e.Item.PreRender += new EventHandler(Item_PreRender); 
        //    } 
        //} 
 
        //void Item_PreRender(object sender, EventArgs e) 
        //{ 
        //    ((sender as GridDataItem)["CheckBoxTemplateColumn"].FindControl("IMGchbx") as CheckBox).Checked = (sender as GridDataItem).Selected; 
        //} 
 
        protected void RadAjaxPanel1_Init(object sender, EventArgs e) 
        { 
        
        } 
 
        protected override void CreateChildControls() 
        { 
            base.CreateChildControls(); 
            string traceConst = _TRACECONST + "CreateChildControls()"
            ECSpand.Logging.Log.WriteDebug("Entering", traceConst); 
 
            
              if (Columns != null
              { 
                  if (_enableSelectColumn ) 
                  { 
                      TESTCHBoxColumn columnun = new TESTCHBoxColumn(); 
                      columnun.UniqueName = "CheckBoxTemplateColumn"
                      _gridViewContent.MasterTableView.Columns.Add(columnun); 
 
                  } 
 
                  //_gridViewContent.Columns.Clear(); 
                  Columns.ForEach(o => _gridViewContent.Columns.Add(o)); 
              } 
             
            if (ViewState[_VIEWSTATEITEMID] != null
                _lastchacheDependency = (ViewState[_VIEWSTATEITEMID] as string); 
 
            _panFilec.Controls.Add(_gridViewContent); 
        } 
 
        protected override void OnPreRender(System.EventArgs e) 
        { 
              if (!string.IsNullOrEmpty(_chacheDependency) && Columns !=null
            { 
                DataTable source = getData(); 
                if (source !=null && source.Rows.Count > 0) 
                { 
                    _gridViewContent.DataSource = source; 
                    if (ItemChanged()) 
                    { 
                        _gridViewContent.Rebind(); 
                    } 
                    _panFilec.Visible = true
                } 
                else 
                { _gridViewContent.Rebind(); } 
 
                ViewState[_VIEWSTATEITEMID] = this._chacheDependency; 
            } 
            else 
            { 
                _gridViewContent.Rebind(); 
                ViewState[_VIEWSTATEITEMID] = null
            } 
 
           
        } 
 
        public void ResetGridView() 
        { 
            if (_gridViewContent != null
            { 
                //_gridViewContent.DataSource = null; 
                this._chacheDependency = null
                _gridViewContent.Rebind(); 
                _panFilec.Controls.Clear(); 
                _panFilec.Visible = false
                this.Caching.RemoveObjectFromSession(_DATASOURCECACHE); 
            } 
        } 
 
        private DataTable getData() 
        { 
 
            if (ItemChanged()&& Search!=null
            { 
                DataTable table = this.Search.PerformSearch(); 
                this.Caching.SaveObjectToSession(_DATASOURCECACHE, table); 
                return table; 
            } 
 
            else 
            { 
                return this.Caching.GetObjectFromSession(_DATASOURCECACHE) as DataTable; 
            } 
        } 
 
 
        protected override void Render(HtmlTextWriter writer) 
        { 
 
            _panFilec.RenderControl(writer); 
 
        } 
 
        /// <summary> 
        /// Checks, if a different item is displayed 
        /// </summary> 
        /// <returns></returns> 
        bool ItemChanged() 
        { 
            return !(_chacheDependency.CompareTo(_lastchacheDependency)==0); 
        } 
 
    } 

Code for Column Template:
 
  public class TESTCHBoxColumn: GridTemplateColumn 
    { 
        public TESTCHBoxColumn() 
        { 
            ISImageButtonTemplate btntemplate = new ISImageButtonTemplate(); 
            ISImageCheckBox chbxtempalte = new ISImageCheckBox(); 
 
            this.HeaderTemplate = btntemplate; 
            this.ItemTemplate = chbxtempalte; 
        } 
    } 
 
 public class ISImageButtonTemplate : System.Web.UI.ITemplate 
    { 
        #region ITemplate Members 
        private const string _CHECKATT = "CHECKATT"
        ImageButton _btn; 
 
        public ISImageButtonTemplate() 
        { 
            _btn = new ImageButton(); 
            _btn.ID = "ISImgBtn"
            _btn.ImageUrl = "/_layouts/images/niceTestpic.png"
            _btn.Click += new System.Web.UI.ImageClickEventHandler(_btn_Click); 
            _btn.Attributes.Add(_CHECKATT, "false"); 
        } 
 
        public void InstantiateIn(System.Web.UI.Control container) 
        { 
            container.Controls.Add(_btn); 
        } 
 
        void _btn_Click(object sender, System.Web.UI.ImageClickEventArgs e) 
        { 
            ImageButton imgbtn = sender as ImageButton; 
            RadGrid grid = imgbtn.Parent.Parent.Parent.Parent.Parent.Parent as RadGrid; 
            if (grid != null
            { 
                bool check = !My.ConversionHelper.StringToBool(imgbtn.Attributes[_CHECKATT] as string); 
 
                foreach (GridDataItem dataItem in grid.Items) 
                { 
                    (dataItem.FindControl("IMGchbx"as CheckBox).Checked = check; 
                    dataItem.Selected = check; 
                } 
                imgbtn.Attributes[_CHECKATT] = (check).ToString(); 
            } 
 
        } 
    } 
 
  public class ISImageCheckBox :System.Web.UI.ITemplate 
    { 
 
       private CheckBox _box; 
       private int number = 0; 
 
        public ISImageCheckBox() 
        { 
            number = 0; 
        } 
 
        public void InstantiateIn(System.Web.UI.Control container) 
        { 
            number++; 
            _box = new CheckBox(); 
            _box.AutoPostBack = true
            _box.ID = "IMGchbx"
            _box.CheckedChanged += new EventHandler(_box_CheckedChanged); 
 
            container.Controls.Add(_box); 
        } 
 
          void _box_CheckedChanged(object sender, EventArgs e) 
        { 
            ((sender as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked; 
        } 
 
   
    } 




2 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 10 May 2010, 06:11 AM
Hello David,

Indeed, selection is not persisted across paging. Please refer to the following help article to see how to preserve the selected rows:

http://www.telerik.com/help/aspnet-ajax/grdpersistselectedrowsonsorting.html

Best wishes,
Dimo
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.
0
David
Top achievements
Rank 1
answered on 14 May 2010, 06:28 PM
Thank you for your Reply,
that it, thank you !
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Dimo
Telerik team
David
Top achievements
Rank 1
Share this question
or