Norman
Norman

Reputation: 785

Adding a CSS link to the head using .NET

I'm using this bit of code to add a CSS link in the head section :

<script runat="server">
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
Dim oCSS As Control = Me.Page.FindControl("CSS")
If Not oCSS is Nothing Then
Dim oLink As New HtmlGenericControl("link")
oLink.Attributes("id") = "MyCss"
oLink.Attributes("rel") = "stylesheet"
oLink.Attributes("type") = "text/css"
oLink.Attributes("href") = SkinPath & "MyCss.css"
oCSS.Controls.AddAt(0, oLink)
End if
End Sub
</script>

It works, but the problem is that this places the CSS link on top of all other links in the HEAD section. Is there a way to make this link appear at the bottom of all other CSS links ?

Better still, can it be placed after a particular CSS link already in the head section ?

Also, how does they Controls.AddAt(0, oLink) work. It does not accept any other number other than "0" for the index.

Thank you in advance, :).

Upvotes: 0

Views: 481

Answers (1)

Preet Sangha
Preet Sangha

Reputation: 65466

From your code I suspect the Controls is a ControlsCollection and the documentation for the AddAt method tells you that the first argument is the place in the collection (0 = first) where the control is to be added.

try this instead

oCSS.Controls.Add(oLink)

or

oCSS.Controls.AddAt(oCSS.Controls.Count, oLink)

Edit:

Try finding the right control with the Me.Page.FindControl("NameOfControl")

Upvotes: 1

Related Questions