Datagrid Update and Cancel Events not firing in WebPart
I am working on a webpart to process our user agreements(XML form signed by new users requesting an account). However, for some reason the cancel & update events are not firing. I have attached event handlers, but still get nothing for those two. The edit and sort commands will fire. Anyone have any ideas?
I have tried it with the onitemcommand code below. Still nothing comes through there but edit commands.
Imports System
Imports System.ComponentModel
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Serialization
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Utilities
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages
'Imports EU.SharePoint.Utilities
'Description for AccountCreator.
Public Class AccountCreator
Inherits Microsoft.SharePoint.WebPartPages.WebPart
Private Const _defaultText As String = ""
Dim _text As String = _defaultText
Property [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
'This method gets the custom tool parts for this Web Part by overriding the
'GetToolParts method of the WebPart base class. You must implement
'custom tool parts in a separate class that derives from
'Microsoft.SharePoint.WebPartPages.ToolPart.
'Returns an array of references to ToolPart objects.
Public Overrides Function GetToolParts() As ToolPart()
Dim toolParts(2) As ToolPart
Dim wptp As WebPartToolPart = New WebPartToolPart
Dim custom As CustomPropertyToolPart = New CustomPropertyToolPart
toolParts(0) = wptp
toolParts(1) = custom
'AccountCreator ToolPart is added here
toolParts(2) = New AccountCreatorTP
Return toolParts
End Function
'Render this Web Part to the output parameter specified.
Protected WithEvents _dgReqs As DataGrid
Private _dsReqs As DataSet
Private _lbl As Label
Private _col As BoundColumn
Protected _edtCol As EditCommandColumn
Private Const VS_Cur_Sort_Ord As String = "CurrentSortOrder"
Private Const VS_Cur_Sort_Exp As String = "CurrentSortExpression"
Private _CurSortOrd As String
Private _CurSortExp As String
Private _list As SPList
Private _web As SPWeb
Private _dv As DataView
'build toolpart to set this value
Private _fields() As String
Protected Overrides Sub RenderWebPart(ByVal output As System.Web.UI.HtmlTextWriter)
EnsureChildControls()
_dgReqs.RenderControl(output)
_lbl.RenderControl(output)
End Sub
Protected Overrides Sub CreateChildControls()
'Get the references to the web and List where the User Agreements are stored
_web = SPControl.GetContextWeb(context)
'The list name should be set from the tool part
_list = _web.Lists("User Request form")
'Set the current sort order and expression
Dim SortOrd As String = CStr(viewstate(VS_Cur_Sort_Ord))
Dim SortExp As String = CStr(viewstate(VS_Cur_Sort_Exp))
Try
If SortOrd <> "" Then
Me._CurSortOrd = SortOrd
Else
Me._CurSortOrd = enuSortOrd.ASC.ToString
End If
If SortExp <> "" Then
Me._CurSortExp = SortExp
Else
'Ideally this would be set in the tool part
Me._CurSortExp = "Last"
End If
Catch ex As Exception
End Try
'Save the sort order and expression in the viewstate
viewstate(VS_Cur_Sort_Ord) = _CurSortOrd
viewstate(VS_Cur_Sort_Exp) = _CurSortExp
'Create the DataGrid
If _dgReqs Is Nothing Then
_dgReqs = New DataGrid
With _dgReqs
.AllowSorting = True
.AllowPaging = True
.AutoGenerateColumns = False
End With
End If
'Create the edit column
_edtCol = New EditCommandColumn
With Me._edtCol
.ButtonType = ButtonColumnType.LinkButton
.EditText = "Edit"
.CancelText = "Cancel"
.HeaderText = "Actions"
.UpdateText = "Update"
.Visible = True
End With
_dgReqs.Columns.Add(_edtCol)
AddHandler _dgReqs.EditCommand, New DataGridCommandEventHandler(AddressOf Me._dgReqs_EditCommand)
AddHandler _dgReqs.UpdateCommand, New DataGridCommandEventHandler(AddressOf Me._dgReqs_UpdateCommand)
AddHandler _dgReqs.CancelCommand, New DataGridCommandEventHandler(AddressOf Me._dgReqs_CancelCommand)
AddHandler _dgReqs.SortCommand, New DataGridSortCommandEventHandler(AddressOf Me._dgReqs_SortCommand)
'Create the data columns
If _fields Is Nothing Then
_fields = New String() {"Command", "Rank", "Last", "First", "Middle"}
End If
AddCols(_fields)
'Fill and bind the DataGrid
BindData(_CurSortExp, _CurSortOrd)
Controls.Add(_dgReqs)
'Create the label for success/failure reporting
_lbl = New Label
_lbl.Text = Me._CurSortOrd & " " & Me._CurSortExp
Controls.Add(_lbl)
Me.ChildControlsCreated = True
End Sub
Protected Sub AddCols(ByVal fields As String())
For count As Integer = 0 To fields.Length - 1
_col = New BoundColumn
_col.DataField = fields(count)
_col.HeaderText = fields(count)
_col.SortExpression = fields(count)
_dgReqs.Columns.Add(_col)
Next
End Sub
Private Sub BindData(ByVal sortExp As String, ByVal sortOrd As String)
'Create and load the dataset
_dsReqs = New DataSet
For Each li As SPListItem In _list.Items
_dsReqs.ReadXml(New StringReader(OpenItemAsString(li)))
Next
_dsReqs.Tables(0).DefaultView.Sort() = sortExp & " " & sortOrd
'Bind the data
With _dgReqs
.DataSource = _dsReqs.Tables(0).DefaultView
.DataBind()
End With
End Sub
'Public Sub _dgReqs_OnItemCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
' System.Diagnostics.Debug.Write(e.CommandName)
' Me._lbl.Text = "event fired"
' Dim row As DataRow
' For count As Integer = 0 To _dsReqs.Tables(0).Rows(e.Item.ItemIndex).ItemArray.Length - 1
' _lbl.Text = _lbl.Text & _dsReqs.Tables(0).Rows(e.Item.ItemIndex).ItemArray(count).ToString() & vbCrLf
' Next
' If e.CommandName = "Edit" Then
' _dgReqs.EditItemIndex = e.Item.ItemIndex
' BindData(_CurSortExp, _CurSortOrd)
' Me._lbl.Text = "edit"
' ElseIf e.CommandName = "Cancel" Then
' _dgReqs.EditItemIndex = -1
' BindData(_CurSortExp, _CurSortOrd)
' Me._lbl.Text = "cancel"
' ElseIf e.CommandName = "Update" Then
' _dgReqs.EditItemIndex = -1
' BindData(_CurSortExp, _CurSortOrd)
' Me._lbl.Text = "update"
' End If
'End Sub
Public Shared Function OpenItemAsString(ByVal Item As SPListItem) As String
'Variable used to store the byte array
Dim bytes As Byte()
bytes = Item.File.OpenBinary()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Dim str As String = encoding.GetString(bytes)
Return str
End Function
Private Sub _dgReqs_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles _dgReqs.EditCommand
System.Diagnostics.Debug.Write(e.CommandName)
_dgReqs.EditItemIndex = e.Item.ItemIndex
BindData(_CurSortExp, _CurSortOrd)
Me._lbl.Text = "edit fired & " & _dsReqs.Tables(0).DefaultView.Item(e.Item.ItemIndex).Item("Last").ToString
End Sub
Private Sub _dgReqs_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles _dgReqs.SortCommand
Dim NewSortExp As String = e.SortExpression
Dim CurSortOrd As String = CStr(viewstate(VS_Cur_Sort_Ord))
Dim CurSortExp As String = CStr(viewstate(VS_Cur_Sort_Exp))
If NewSortExp = CurSortExp Then
'The sort expressions are the same so reverse the order
If CurSortOrd = "ASC" Then
CurSortOrd = "DESC"
Else
CurSortOrd = "ASC"
End If
Else
'The sort expressions are different so update the sort expression and set asc order
CurSortExp = NewSortExp
CurSortOrd = "ASC"
End If
'Update the viewstate with the new values
viewstate(VS_Cur_Sort_Ord) = CurSortOrd
viewstate(VS_Cur_Sort_Exp) = CurSortExp
'ReBind the data
BindData(CurSortExp, CurSortOrd)
End Sub
Private Sub _dgReqs_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles _dgReqs.UpdateCommand
'Ad constructor goes here
Me._lbl.Text = "Update Fired"
End Sub
Private Sub _dgReqs_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles _dgReqs.CancelCommand
_dgReqs.EditItemIndex = -1
BindData(_CurSortExp, _CurSortOrd)
Me._lbl.Text = "cancel fired"
End Sub
End Class
Comments
I ended up scrapping the edit column for 3 button columns. With a combination of showing and hiding, it comes out as a simulated edit column. For some reason, this is easy to setup compared to getting the edit, update, and cancel events to fire properly. If I find out what I was missing, I will post it here.
Posted by: Andy | March 19, 2006 09:18 PM