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.
Exception Details: System.InvalidOperationException: Script controls may not be registered before PreRender.
Source Error:
Stack Trace:
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?
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.] |
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?