// Get the ScriptTemplateControl object from a TemplateControl internal static ScriptTemplateControl GetScriptTemplateControl(Control c) { TemplateControl templateControl = c.TemplateControl; IScriptTemplateControl iscriptTemplateControl = templateControl as IScriptTemplateControl; if (iscriptTemplateControl == null) { throw new Exception("The page '" + templateControl.AppRelativeVirtualPath + "' doesn't have the expected script base type (i.e. ScriptPage, ScriptUserControl or ScriptMaster)"); } return(iscriptTemplateControl.ScriptTemplateControl); }
protected override void OnInit(EventArgs e) { base.OnInit(e); // Fail if we didn't get a virtual path if (String.IsNullOrEmpty(_virtualPath)) { ThrowException("The UserControl tag must have a 'VirtualPath' attribute", null); } try { _uc = TemplateControl.LoadControl(_virtualPath); } catch (Exception exception) { ThrowException(null, exception); } // Give our ID to the created UserControl, and change ours to avoid conflict string id = ID; ID = "__" + id; _uc.ID = id; // Handle expando attributes by forwarding them to the created control IScriptTemplateControl scriptUC = _uc as IScriptTemplateControl; if (scriptUC != null) { if (_attributes != null) { foreach (KeyValuePair <string, string> entry in _attributes) { try { scriptUC.ScriptTemplateControl.SetProperty(entry.Key, entry.Value); } catch (Exception exception) { ThrowException(null, exception); } } } } Controls.Clear(); Controls.Add(_uc); }
private string GetScriptFromTemplateControl() { IScriptTemplateControl iscriptTemplateControl = (IScriptTemplateControl)_templateControl; string code = iscriptTemplateControl.InlineScript; if (code == null) { return(String.Empty); } StringBuilder builder = new StringBuilder(); // Append enough blank lines to reach the start line of the script block in the page for (int line = 1; line < iscriptTemplateControl.InlineScriptLine; line++) { builder.AppendLine(); } builder.Append(code); return(builder.ToString()); }