Ok, I finally finished it.
- We ended up creating Page descendant with the some code in it to substitute for script manager (see below). We call to it from control s OnPreRender method using control s Page property. Thanks to guys from http://dj.codeplex.com/ for providing example of how to do it.
2, 3, 4. We used jQuery.data method to bind instances of script classes to DOM elements. We execute instantiation, initialization and binding code using jQuery.ready method. This code is added to control in its Render method using AddScript method (see below). Maybe later we would use JavaScriptSerializer for passing values from C# control to javascript classes, but at the moment we do it by hands, passing parameters to javascript class constructor.
HashSet<string> scriptReferences = new HashSet<string>();
HashSet<string> cssReferences = new HashSet<string>();
List<string> styles = new List<string>();
public void AddScriptReference(string url, bool resolve)
{
string realUrl = url;
if (resolve)
realUrl = ResolveClientUrl(url);
if (!scriptReferences.Contains(realUrl))
{
scriptReferences.Add(realUrl);
Header.Controls.Add(
new LiteralControl(
"<script type= text/javascript src= " +
realUrl + " ></script>"));
}
}
public void AddCssReference(string url)
{
if (!cssReferences.Contains(url))
{
cssReferences.Add(url);
HtmlLink link = new HtmlLink();
//link.Href = ResolveClientUrl("~/jQuery-ui/css/ui-lightness/jquery-ui.css");
link.Href = url;
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
Header.Controls.Add(link);
}
}
public void AddCssStyle(string style)
{
styles.Add(style);
}
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e);
Header.Controls.Add(
new LiteralControl(
"<style type= text/css >" + styles.Join("
") + "</style>"
)
);
}
public static void AddScript(HtmlTextWriter writer, string script,
bool executeWhenReady)
{
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
writer.RenderBeginTag(HtmlTextWriterTag.Script);
if (executeWhenReady)
{
writer.Write("$(function(){
");
}
writer.Write(script);
if (executeWhenReady)
{
writer.Write("});
");
}
writer.RenderEndTag();
}