Wednesday, 28 October 2009

Adding javascript or style sheet links to page head

You can do this dynamically for any page, even if it's header is provided by a master page elsewhere. Simply use the following code in your Page_Load event:

A link to a JS file. In this case it's Scriptaculous:
Dim jslink As HtmlGenericControl = New HtmlGenericControl("Script")
jslink.Attributes.Add("type", "text/javascript")
jslink.Attributes.Add("src", "/Scripts/scriptaculous.js")
Me.Header.Controls.Add(jslink)

A link to a CSS:
Dim link As HtmlLink = New HtmlLink()
link.Attributes.Add("type", "text/css")
link.Attributes.Add("rel", "stylesheet")
link.Attributes.Add("href", "../css/sortableLists.css")
Me.Header.Controls.Add(link)

Wednesday, 14 October 2009

Hide your datapager when there's only one page

It seems really strange to me that this isn't a configurable property of a datapager. If your datapager is in the LayoutTemplate of your Listview then this code will hide it for you:

Protected Sub ListView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
Dim pager As DataPager = DirectCast(ListView1.FindControl("DataPager1"), DataPager)
If pager.PageSize < pager.TotalRowCount Then
pager.Visible = True
Else
pager.Visible = False
End If
End Sub

Just place this as an OnPreRender handler for the list.

Tuesday, 13 October 2009

Using a like % @parameter % clause in a stored procedure

I wasted a few minutes scratching my head over this simple one, so it's worth me noting it down:

whatever like '%' + @parameter + '%'

It's not what I first did...whatever like '%@parameter%'.

Doh!