- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Helpful code to autoupdate entity from datagriditem
Joined: 22-Jul-2004
I wrote these little methods to help with updating and adding entities based on the contents of a datagriditem (asp.net). I thought someone else might find them useful and would appreciate any improvements that anyone has.
Public Sub AddEntity(ByVal Entity As IEntity, ByVal Item As DataGridItem, Optional ByVal NonKeyPredefinedFields As Hashtable = Nothing)
If NonKeyPredefinedFields Is Nothing Then
NonKeyPredefinedFields = New Hashtable
End If
For i As Int32 = 0 To Entity.Fields.Count - 1
With Entity
If Not (.Fields(i).IsPrimaryKey Or .Fields(i).IsReadOnly) And Not NonKeyPredefinedFields.Contains(.Fields(i).Name) Then
If Not ExtractValue(.Fields(i).Name, Item) Is Nothing Then
.SetNewFieldValue(i, System.Convert.ChangeType(ExtractValue(.Fields(i).Name, Item), .Fields(i).DataType))
End If
End If
End With
Next
For Each Key As String In NonKeyPredefinedFields.Keys
Entity.SetNewFieldValue(Key, NonKeyPredefinedFields(Key))
Next
Entity.Save()
End Sub
Public Sub UpdateEntity(ByVal Entity As IEntity, ByVal Item As DataGridItem, Optional ByVal NonKeyPredefinedFields As Hashtable = Nothing)
If NonKeyPredefinedFields Is Nothing Then
NonKeyPredefinedFields = New Hashtable
End If
For i As Int32 = 0 To Entity.Fields.Count - 1
With Entity
If Not (.Fields(i).IsPrimaryKey Or .Fields(i).IsReadOnly Or NonKeyPredefinedFields.Contains(.Fields(i).Name)) Then
If Not ExtractValue(.Fields(i).Name, Item) Is Nothing Then
.SetNewFieldValue(i, System.Convert.ChangeType(ExtractValue(.Fields(i).Name, Item), .Fields(i).DataType))
End If
End If
End With
Next
For Each Key As String In NonKeyPredefinedFields.Keys
Entity.SetNewFieldValue(Key, NonKeyPredefinedFields(Key))
Next
Entity.Save()
End Sub
Public Function ExtractValue(ByVal sFieldName As String, ByVal Item As DataGridItem) As String
If Not Item.FindControl(sFieldName) Is Nothing Then
Return GetValueFromControl(Item.FindControl(sFieldName))
ElseIf Not Item.FindControl(sFieldName & "_Item") Is Nothing Then
Return GetValueFromControl(Item.FindControl(sFieldName & "_Item"))
ElseIf Not Item.FindControl(sFieldName & "_Edit") Is Nothing Then
Return GetValueFromControl(Item.FindControl(sFieldName & "_Edit"))
ElseIf Not Item.FindControl(sFieldName & "_Footer") Is Nothing Then
Return GetValueFromControl(Item.FindControl(sFieldName & "_Footer"))
Else
For Each Control As Control In Item.Controls
If Not Control.ID Is Nothing Then
If Control.ID.ToString.StartsWith(sFieldName) Then
Return GetValueFromControl(Control)
End If
End If
Next
End If
Return Nothing
End Function
Public Function GetValueFromControl(ByVal Control As System.Web.UI.Control) As String
If Control.GetType Is GetType(TextBox) Then
Return GetValueFromControl(CType(Control, TextBox))
ElseIf Control.GetType Is GetType(Label) Then
Return GetValueFromControl(CType(Control, Label))
ElseIf Control.GetType Is GetType(DropDownList) Then
Return GetValueFromControl(CType(Control, DropDownList))
ElseIf Control.GetType Is GetType(ActiveUp.WebControls.ActiveDateTime) Then
Return CType(GetValueFromControl(CType(Control, ActiveUp.WebControls.ActiveDateTime)), String)
Else
Throw New System.Exception("Unhandled Control Type: " & Control.GetType.ToString & " in GetValueFromControl")
End If
End Function
Public Function GetValueFromControl(ByVal WebControl As TextBox) As String
Return WebControl.Text.Trim
End Function
Public Function GetValueFromControl(ByVal WebControl As Label) As String
Return WebControl.Text.Trim
End Function
Public Function GetValueFromControl(ByVal WebControl As DropDownList) As String
If Not WebControl.SelectedItem Is Nothing Then
Return WebControl.SelectedItem.Value.ToString.Trim
Else
Return ""
End If
End Function
Public Function GetValueFromControl(ByVal WebControl As ActiveUp.WebControls.ActiveDateTime) As Date
Return WebControl.Date
End Function
It assumes that the datagrid is setup so that each field that you need has a corresponding item, edit and footer template containing a control with the field name (or some portion of it).
For a bound collection of Customer for example with each entity like:
CustomerEntity CustomerName CustomerPhone CompanyID (FK)
I would probably have templates that looked like <ITEMTEMPLATE> <asp:Label id=CustomerName_Item runat=server text='<%# CType(Container.DataItem,{NameSpace}.CustomerEntity).CustomerName %>'></asp:Label> </ITEMTEMPLATE> <EDITITEMTEMPLATE> <asp:TextBox id=CustomerName_Edit runat=server text='<%# CType(Container.DataItem,{NameSpace}.CustomerEntity).CustomerName %>'></asp:TextBox> </EDITITEMTEMPLATE> <FOOTERTEMPLATE> <asp:TextBox id=CustomerName_Footer runat=server> </asp:TextBox> </FOOTERTEMPLATE> (I use the footer template for my AddRow field)
When I get back the DataGridItem (e in this case) to the ItemCommand for the grid, I just have to do the following:
' Create a new CustomerEntity dim Customer as new CustomerEntity ' Create a hash of non bound Fields that I want to add (FK's and such) dim Hash as new HashTable Hash.Add("CompanyID",GetTheValueFromSomeFunctionForCurrentCompanyID()) ' Call the AddEntity or UpdateEntity Method AddEntity(Customer,e,Hash)
My Customer is then added, by looping through all the field name of the current entity and looking for a control that has it in the name, then returning the current value of that control and setting the field value. I use the hash table to pass in values that are not part of the bound dataitem - like foreign keys, etc... and sedt those as well.
I know it looks pretty convoluted, but MAN has it saved me a TON of time.
Thanks and please let me know if anyone thinks it would be helpful enough for me to build a working example to post...
ALso - in looking through the code, don't let the ActiveUp.WebControls.ActiveDateTime references throw you - I use that for my date controls - it can be found here: http://www.activeup.com/products/components/activedatetime/ if any one is interested... It's handy and free .
Hal
Joined: 22-Jul-2004
Otis wrote:
Thanks for the contribution! I've formatted the posting a bit for easier reading
Thanks Otis - just out of curiosity - how do you get a tab into a multiline textbox ?
ctrl-Tab doesn't seem to work and I have seen some articles that talk about using either javascript or DHTML to handle key events... Any idea if there is any easier way?
Joined: 17-Aug-2003
hlesesne wrote:
Otis wrote:
Thanks for the contribution! I've formatted the posting a bit for easier reading
Thanks Otis - just out of curiosity - how do you get a tab into a multiline textbox ?
ctrl-Tab doesn't seem to work and I have seen some articles that talk about using either javascript or DHTML to handle key events... Any idea if there is any easier way?
Copy code to notepad2 -> 4xspace to tab searchreplace -> copy back