UPDATE: Apologies on lack of clarity I realise I could iterate over the controls collection, was just looking for a better/more efficient method.
I am trying to dynamically add some css and js elements into an ASP.Net page at runtime, but because this code is used in mutliple controls I need to ensure that the relevant links are only injected once.
Currently I have the folowing code in the OnPreRender event.
Dim head As HtmlControls.HtmlHead = Me.Parent.Page.Header
If Not _useCustomStyles Then
Dim litCustomCss As New LiteralControl("<link rel=" & Chr(34) & "stylesheet" & Chr(34) & " href=" & Chr(34) & "/css/udPart_core.css" & Chr(34) & " type=" & Chr(34) & "text/css" & Chr(34) & " media=" & Chr(34) & "screen" & Chr(34) & " />" & vbCrLf)
litCustomCss.ID = "cssCustom"
If Not head.Controls.Contains(litCustomCss) Then
head.Controls.Add(litCustomCss)
End If
End If
Does .Contains look for this instance of an object (which I assume is why this is failing)?
Is there a way to check the controls collection for a specific id? Or am I going to have to write a sub to loop through the existing controls in the collection checking for the id.
Thanks
For external script sources you can use this :
if (!ClientScript.IsClientScriptIncludeRegistered("externalResuorce"))
{
ClientScript.RegisterClientScriptInclude("externalResuorce",
"scripts/myscripts.js");
}
But for stylesheet files, you can use this :
HtmlLink link = new HtmlLink();
link.Href = "main.css";
link.Attributes["type"] = "text/css";
link.Attributes["rel"] = "stylesheet";
Page.Header.Controls.Add(link);
and to control maybe you should loop Page.Header.Controls collection and look for a HtmlLink that has main.css href.
EDIT : Also this might help for finding your literal control in header's controls collection :
Page.Header.FindControl("your_literals_id");