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

How to integrate radGrid into a customized webcontrol?

6 Answers 250 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Liu Peng
Top achievements
Rank 1
Liu Peng asked on 01 Feb 2008, 05:35 AM
When I integrate radGrid into my customized webcontrol, I meet a error: Script controls may not be registered before PreRender. the following is the source code:
    public class Grid : WebControl, IScriptControl
    {
        // Sub controls
        private RadGrid _radGrid;
        private ScriptManager sm;

        protected override void CreateChildControls()
        {
            Controls.Clear();

            this._radGrid = new RadGrid();
            this._radGrid.NeedDataSource += new GridNeedDataSourceEventHandler(this.radGrid_NeedDataSource);
           
            this.Controls.Add(this._radGrid);

            base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!this.DesignMode)
            {
                // Test for ScriptManager and register if it exists
                sm = ScriptManager.GetCurrent(Page);

                if (sm == null)
                    throw new HttpException("A ScriptManager control must exist on the current page.");

                sm.RegisterScriptControl(this);
            }

        }

        protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
                try
                {
                    DataTable tbl = new DataTable();
                    ....
                    DataView valuesView = tbl.DefaultView;
                    this._radGrid.DataSource = valuesView;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
        }

        #region IScriptControl Members

        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public IEnumerable<ScriptReference> GetScriptReferences()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IScriptControl Members

        IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

And then I re-design my webcontrol inherited from RadWebControl, the same error occur.

Server Error in '/' Application.

Script controls may not be registered before PreRender.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Script controls may not be registered before PreRender.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: Script controls may not be registered before PreRender.]
System.Web.UI.ScriptControlManager.RegisterScriptControl(TScriptControl scriptControl) +236
System.Web.UI.ScriptManager.RegisterScriptControl(TScriptControl scriptControl) +99
Telerik.Web.UI.RadWebControl.RegisterScriptControl() +60
Telerik.Web.UI.RadWebControl.OnPreRender(EventArgs e) +64
System.Web.UI.Control.PreRenderRecursiveInternal() +86
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041


Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433

How can I integrate radGrid into my customized webcontrol?

6 Answers, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 04 Feb 2008, 11:18 AM
Hello pp,

Could you elaborate how could we reproduce the problematic behavior you are experiencing? We tried to observe the problem with your code but it behaves as expected on our end -- please review the attached sample application and let us know how it goes.


Sincerely yours,
Manuel
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ron Hary
Top achievements
Rank 2
answered on 11 Jun 2008, 10:49 AM
Hi,

I have the same scenario (integrated RadGrid) but another problem.
My problem is that the grid does not show in design time...
0
Rosen
Telerik team
answered on 13 Jun 2008, 02:18 PM
Hi Ron,

Can you please provide some more information about your control implementation? Are you using a CompositeControl?

Best regards,
Rosen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ron Hary
Top achievements
Rank 2
answered on 15 Jun 2008, 06:47 AM
    public class Forum : WebControl, INamingContainer  
    {  
        protected RadGrid _MessageList;  
 
        #region Properties  
 
        #region ParentKeyName  
 
        private string _ParentKeyName = "ParentMessage";  
 
        [DefaultValue( "ParentMessage" )]  
        [Category( "Message List" )]  
        public string ParentKeyName  
        {  
            get  
            {  
                return ( _ParentKeyName );  
            }  
            set  
            {  
                _ParentKeyName = value;  
            }  
        }  
 
        #endregion  
 
        #region KeyName  
 
        private string _KeyName = "ID";  
 
        [DefaultValue( "ID" )]  
        [Category( "Message List" )]  
        public string KeyName  
        {  
            get  
            {  
                return ( _KeyName );  
            }  
            set  
            {  
                _KeyName = value;  
            }  
        }  
 
        #endregion  
 
        #region RootKeyValue  
 
        private string _RootKeyValue = "0";  
 
        [DefaultValue( "0" )]  
        [Category( "Message List" )]  
        public string RootKeyValue  
        {  
            get  
            {  
                return ( _RootKeyValue );  
            }  
            set  
            {  
                _RootKeyValue = value;  
            }  
        }  
 
        #endregion  
 
        #endregion  
 
        #region Helpers  
 
        private void InitGrid ( bool bForDesign )  
        {  
            // set grid properties  
            _MessageList = new RadGrid( );  
 
            _MessageList.ID = "idMessageList";  
 
            _MessageList.ShowHeader = true;  
            _MessageList.Visible = true;  
            _MessageList.EnableAJAX = true;  
            _MessageList.EnableOutsideScripts = true;  
            _MessageList.GridLines = GridLines.Horizontal;  
 
            _MessageList.MasterTableView.AutoGenerateColumns = false;  
 
            _MessageList.MasterTableView.DataKeyNames = new string[ ] { _ParentKeyName, _KeyName };  
            _MessageList.MasterTableView.FilterExpression = _ParentKeyName + " = " + _RootKeyValue;  
 
            _MessageList.MasterTableView.SelfHierarchySettings.ParentKeyName = _ParentKeyName;  
            _MessageList.MasterTableView.SelfHierarchySettings.KeyName = _KeyName;  
 
            Controls.Add( _MessageList );  
        }  
 
        #endregion  
 
        #region Render  
 
        protected override void CreateChildControls ( )  
        {  
            InitGrid( false );  
 
            base.CreateChildControls( );  
        }  
 
        protected override void Render ( HtmlTextWriter writer )  
        {  
            if ( DesignMode )  
            {  
                InitGrid( true );  
            }  
 
            base.Render( writer );  
        }  
 
        #endregion  
    }  
 
0
Rosen
Telerik team
answered on 18 Jun 2008, 05:22 AM
Hi Ron Hary,

Unfortunately I'm unable to observe this behavior. I have attached a slightly modified version of your code. Can you please give it a try and see if it helps?

Sincerely yours,
Rosen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ron Hary
Top achievements
Rank 2
answered on 24 Jun 2008, 09:41 AM
Thank you
Tags
Grid
Asked by
Liu Peng
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Ron Hary
Top achievements
Rank 2
Rosen
Telerik team
Share this question
or