Monday, March 31, 2008

Dynamically Adding Items Between ListView Items

On my new wedding website directory I wanted to add Google adsense between certain listings on each page. This can be achieved in the following manner.



Protected Sub ListView1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemCreated
Dim lblGoogle As New Literal

If e.Item.ID = "ctrl2" Or e.Item.ID = "ctrl5" Or e.Item.ID = "ctrl7" Then
lblGoogle.Text = "<script type=""text/javascript""><!--" & vbCrLf & "google_ad_client = ""pub-6774188899700328"";" & vbCrLf & "//300x250, created 11/20/07" & vbCrLf & "google_ad_slot = ""6463796932"";" & vbCrLf & "google_ad_width = 728;" & vbCrLf & "google_ad_height = 90;" & vbCrLf & "//--></script>" & vbCrLf & "<script type=""text/javascript"" src=""http://pagead2.googlesyndication.com/pagead/show_ads.js""></script>"
e.Item.Controls.Add(lblGoogle)

End If
End Sub

No comments: