How to integrate radGrid into a customized webcontrol?

7 posts, 0 answers
  1. Liu Peng
    Liu Peng avatar
    18 posts
    Member since:
    Oct 2006

    Posted 31 Jan 2008 Link to this post

    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?

  2. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 04 Feb 2008 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ron Hary
    Ron Hary avatar
    38 posts
    Member since:
    Jun 2005

    Posted 11 Jun 2008 Link to this post

    Hi,

    I have the same scenario (integrated RadGrid) but another problem.
    My problem is that the grid does not show in design time...
  5. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 13 Jun 2008 Link to this post

    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
  6. Ron Hary
    Ron Hary avatar
    38 posts
    Member since:
    Jun 2005

    Posted 15 Jun 2008 Link to this post

        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  
        }  
     
  7. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 18 Jun 2008 Link to this post

    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
  8. Ron Hary
    Ron Hary avatar
    38 posts
    Member since:
    Jun 2005

    Posted 24 Jun 2008 Link to this post

    Thank you
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017