Monday, March 31, 2008

Gridview + Click anywhere on row to Edit + vb.net

If you want to be able to click anywhere on a gridview row and put it into edit mode then you can use this piece of code on the RowDataBound event:



e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(DirectCast(sender, System.Web.UI.Control), "Select$" + e.Row.RowIndex.ToString()))


Then on the RowCommand Event:


If e.CommandName = "Select" Then
GridView1.EditIndex = e.CommandArgument
End If


You can download a sample Visual Studio 2005 solution here

Update
---------------------------
Getting this error message?

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

If you don't want to set enable event validation to false then you can try the following:

Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
For i As Integer = 0 To Me.gv1.Rows.Count - 1
Page.ClientScript.RegisterForEventValidation(Me.gv1.UniqueID, "Select$" & i)
Next
MyBase.Render(writer)
End Sub

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

Monday, March 17, 2008

Umbraco turns to YetAnotherForum.NET

Today Umbraco "upgraded" its forum software to Yetanotherforum.NET this is great for the Umbraco community and will hopefully help the community grow. As you all know I have had a go at installing YAF into umbraco so this brings a happy smile to my face!