Nemisis posted on Friday, March 27, 2009
hi everyone, i am trying to use a custom itemtemplate and edititemtemplate with the formview and the csladatasource. But i cannot get it to update the values and e.Values always is blank.
here is a simplfied version of the code, as you will see i add each property to the ITemplate using reflection of the propertyname, this is done because didnt properties are visible/hidden in each or our databases.
'**************
.ASPX PAGE
'**************
asp:FormView ID="FormView1" runat="server" DataSourceID="CslaDataSource1" DataKeyNames="Id" Width="100%" />
cc1:CslaDataSource ID="CslaDataSource1" runat="server" TypeAssemblyName=""
TypeName="Company, Demo"
TypeSupportsPaging="False" TypeSupportsSorting="False" />
'**************
' CODE BEHIND
'**************
Partial Public Class Edit
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not Me.IsPostBack Then
Me.FormView1.ChangeMode(FormViewMode.ReadOnly)
Else
Me.FormView1.ChangeMode(Me.FormView1.CurrentMode)
End If
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Me.FormView1.DataBind()
End Sub
#Region " Buttons "
Private Sub OnEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles btnEdit.Command
Me.FormView1.EditItemTemplate = New EditItemTemplate(Me)
Me.FormView1.ChangeMode(FormViewMode.Edit)
Me.btnSave.Visible = True
End Sub
Private Sub OnSave(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles btnSave.Command
Dim newObj As Company = CType(Me.FormView1.DataItem, Company)
Me.FormView1.UpdateItem(True)
End Sub
#End Region
#Region " DataSource "
Private Sub CslaDataSource1_SelectObject(ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs) Handles CslaDataSource1.SelectObject
e.BusinessObject = GetCompany()
End Sub
Private Sub CslaDataSource1_UpdateObject(ByVal sender As Object, ByVal e As Csla.Web.UpdateObjectArgs) Handles CslaDataSource1.UpdateObject
Dim obj As Company = GetCompany()
Csla.Data.DataMapper.Map(e.Values, obj)
obj = obj.Save
End Sub
#End Region
#Region " Company "
Public Function GetCompany() As Company
Return Company.GetCompany(Me.Request.QueryString.ToString)
End Function
Public Function SaveCompany(ByRef pObj As Company) As Integer
Try
pObj = pObj.Save
Return 1
Catch ex As Csla.Validation.ValidationException
Return 0
End Try
Return 0
End Function
#End Region
#Region " ItemTemplate "
Public Class ItemTemplate
Implements ITemplate
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
container.Controls.Add(New LiteralControl("
"))
' ADD A HEADING
container.Controls.Add(New LiteralControl("
" & Group.Name & "
"))
' add 3 textboxes
Me.CreateLabelChildControl(container, "Name")
Me.CreateLabelChildControl(container, "Reference")
Me.CreateLabelChildControl(container, "Town")
container.Controls.Add(New LiteralControl("
"))
container.Controls.Add(New LiteralControl("
"))
End Sub
Private Sub CreateLabelChildControl(ByRef container As System.Web.UI.Control, ByRef pName As BSL.Library.Configuration.Name.Name)
Dim Label As New Label
Label.ID = pName.PropertyName
AddHandler Label.DataBinding, AddressOf Label_DataBinding
container.Controls.Add(pObj)
End Sub
Private Sub Label_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim lbl As Label = CType(sender, Label)
Dim obj As FormView = CType(lbl.NamingContainer, FormView)
Dim item As Object = obj.DataItem
' use reflection to get value
Dim Type As Type = GetType(Company)
Dim PropInfo As PropertyInfo = Type.GetProperty(lbl.ID)
If PropInfo.GetValue(item, Nothing) IsNot Nothing Then
lbl.Text = PropInfo.GetValue(item, Nothing)
End If
End Sub
End Class
#End Region
#Region " EditItemTemplate "
Public Class EditItemTemplate
Implements ITemplate
'''
''' Page property needed to add UserControls using LoadControl method
'''
'''
Private mPage As Page
Public ReadOnly Property Page() As Page
Get
Return mPage
End Get
End Property
Public Sub New(ByRef pPage As Page)
mPage = pPage
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
container.Controls.Add(New LiteralControl("
"))
' ADD HEADER
container.Controls.Add(New LiteralControl("
" & Group.Name & "
"))
' ADD 3 TEXTBOXES
Me.CreateTextBoxChildControl(container, "Name")
Me.CreateTextBoxChildControl(container, "Reference")
Me.CreateTextBoxChildControl(container, "Town")
container.Controls.Add(New LiteralControl("
"))
container.Controls.Add(New LiteralControl("
"))
End Sub
Private Sub CreateTextBoxChildControl(ByRef container As System.Web.UI.Control, ByRef pName As BSL.Library.Configuration.Name.Name)
Dim TextBox As New TextBox
TextBox.ID = pName.PropertyName
AddHandler TextBox.DataBinding, AddressOf TextBox_DataBinding
container.Controls.Add(pObj)
End Sub
Private Sub TextBox_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim t As TextBox = CType(sender, TextBox)
Dim obj As FormView = CType(t.NamingContainer, FormView)
Dim item As Object = obj.DataItem
' use reflection to get value
Dim Type As Type = GetType(Company)
Dim PropInfo As PropertyInfo = Type.GetProperty(t.ID)
If PropInfo.GetValue(item, Nothing) IsNot Nothing Then
t.Text = PropInfo.GetValue(item, Nothing)
End If
End Sub
#End Region
End Class