using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web.UI;
namespace DPLib
{
/// <summary>
/// A simple control for adding client scripts to web form pages.
/// This is the main class of the ClientScripts control.
/// </summary>
[DefaultProperty("Scripts"),
ToolboxData("<{0}:ClientScripts runat=server></{0}:ClientScripts>"),
Designer("DPLib.ClientScriptsDesigner"), PersistChildren(false),
ParseChildren(true,"Scripts")]
public class ClientScripts : System.Web.UI.WebControls.WebControl
{
// An array of ClientScript objects.
private ScriptCollection clientScripts ;
/// <summary>
/// Initializes an instance of the ClientScripts class with no parameters.
/// </summary>
public ClientScripts()
{
}
/// <summary>
/// A ScriptCollection of ClientScript objects for this instance.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(ScriptCollectionEditor), typeof(UITypeEditor)),
NotifyParentProperty(true),PersistenceMode(PersistenceMode.InnerDefaultProperty),
Category("Data")]
public ScriptCollection Scripts
{
get
{
if (clientScripts == null) {
clientScripts = new ScriptCollection(this);
}
return clientScripts;
}
}
// The control must be "Rendered" in OnPreRender for the
// RegisterClientScriptBlock method to work. RegisterClientScriptBlock
// will not render the script if called from Render - it is too late in
// the processing of the Page by that time.
protected override void OnPreRender(EventArgs e)
{
// Loop through the scripts collection and render the enabled scripts.
for (int count = 0; count < this.Scripts.Count; count++)
{
ClientScript clientScript = this.clientScripts[count];
// Only render scripts that have ScriptEnabled set to true
if (clientScript.ScriptEnabled)
{
switch(clientScript.ScriptType)
{
case ScriptTypes.ClientScript :
// If the ClientScript has not already been registered then
// call RegisterClientScriptBlock.
if (!Page.IsClientScriptBlockRegistered(clientScript.ID))
{
Page.RegisterClientScriptBlock(clientScript.ID,clientScript.ToHtmlString(this.UniqueID));
}
break;
case ScriptTypes.StartupScript :
// If the StartupScript has not already been registered then
// call RegisterStartupScript
if (!Page.IsStartupScriptRegistered(clientScript.ID))
{
Page.RegisterStartupScript(clientScript.ID,clientScript.ToHtmlString(this.UniqueID));
}
break;
}
}
}
base.OnPreRender (e);
}
}
}