Wednesday, January 2, 2008

Used HttpContext.Items for deleting gridview row and String.TrimEnd()

There’s probably a better way, but I’m short on time so I ended up using a templated column to fire a command event that sets the argument based on the key field.  I have a feeling this is done automatically if you use a “delete button” but I wasn’t sure about the confirm prompt and I just needed this done, so I used the context to pass the value from the GridView_RowDeleting event to the ObjectDataSource.Deleting event where it is trivial to set the input parameters…

 

        Dim compId As Object = gvwComponents.DataKeys.Item(e.RowIndex).Values.Item(0)

        Context.Items.Add("CompId", compId)

 

    Protected Sub odsComponentList_Deleting(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) Handles odsComponentList.Deleting

 

        Dim compId As Object = Context.Items.Item("CompId")

 

        If CanDeleteComponent(compId) Then

            e.InputParameters.Item("componentId") = compId

        Else

            e.Cancel = True

        End If

    End Sub

Note, I discovered a cool new string function called TrimEnd():

 

            For Each row As DataRow In relatedComps.Rows

                compList += " " & row("Name") & ","

            Next

            compList = compList.TrimEnd(",")

 

No comments: