Setting Edit Date for gridview bound to llblgenproDataSource

Posts   
 
    
like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 20-Nov-2007 11:57:43   

Using and a .net gridview bound to a llblgenpro data source 2 I want to save the LastEditDate(and time) without having to manually make the save after datagrid edit. My first (with obvious error) attempt:

 <asp:TemplateField HeaderText="LastEditDate" SortExpression="LastEditDate">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox3" runat="server" Text='<%# Now() %>' ></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastEditDate") %>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>

When Editing, the grid shows the start of edit date/time, but of course I've now lost the binding, so the value for Now(), although it displays, doesn't get written to persistant store.

I understand that with adapter I could intercept/override the .Save to add .LastEditDate = Now(), but I'm using self-service.

Can anyone suggest a "declarative" way of achieving this?

Graham

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Nov-2007 08:35:59   

without having to manually make the save after datagrid edit.

I didn't understand the above line.

But anyway I think you should set the DateTime.Now in the RowDataBound event. Or you may initialize the corresponding entity field value to this DateTime.Now, so when bound to the grid, the required value should be shown.

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 21-Nov-2007 09:22:37   

What I meant by

without having to manually make the save after datagrid edit

was that I wanted the update of LastEditDate to be done everytime a row in the grid was update without me having to "manually": 1. capture an event 2. Dim x as xEntity({primary key read from row}) 3. x.LastEditDate = Now() 4. {set other fields} 5. x.Save

To recap/clarify: The gridview is bound to the datasource and the update/deletes work beautifully and I haven't had to write any code. I now want to set the LastEditDate to be the date/time of the last edit, but I don't want to have to "manually" write all of the code to update the persisitant store when a row is edited in the grid - I want to continue to use the functionality I get by just hooking the gridview to the llblgen datasource.

Can it be done?

Looking into this further, I wonder if I could do something with either:

  • <UpdateParameters> which looks like it might be able to be used a bit like <SelectParameters> . Could this be used to set a default value for a field?

  • the .PerformWork event of the LLBLGenProdatasource

Both are mentioned briefly in the help for LLBLGen Pro v2, but I can't work out how to use them.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Nov-2007 09:56:34   

Looking into this further, I wonder if I could do something with either:

<UpdateParameters> which looks like it might be able to be used a bit like <SelectParameters> . Could this be used to set a default value for a field?

the .PerformWork event of the LLBLGenProdatasource

Both are mentioned briefly in the help for LLBLGen Pro v2, but I can't work out how to use them.

Sure you can use update parameters to set any value for any field. It was mentioned briefly in the docs, coz this is not an LLBLGen specific thing. It's an MS invention simple_smile related to the use of DataSources.

ref: http://msdn2.microsoft.com/EN-US/library/xt50s8kz.aspx

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 21-Nov-2007 17:33:57   

Thanks for your help Walaa

smile My Solution, for others to consider ->

Add 2 asp:Labels:

 <form id="frmProgress" runat="server">

 <asp:Label runat="server" ID="lblEditDate" Text="Set in Page Load  and used by Update Parameters" Visible="false"></asp:Label>
 <asp:Label runat="server" ID="lblEditBy" Text="Set in Page Load and used by Update Parameters" Visible="false"></asp:Label> ... 

Add <UpdateParameters> to datasource, binding values to these controls:

... <llblgenpro:LLBLGenProDataSource ID="dsLine" runat="server" DataContainerType="EntityCollection"
        EnablePaging="True" EntityCollectionTypeName="Fluor.PipeUtils.DAL.CollectionClasses.LineCollection, Fluor.PipeUtils.DAL">
        <SelectParameters>
          <asp:ControlParameter ControlID="ddlModelFilter" DefaultValue="--Pick--" Name="ModelIndexNo"
            PropertyName="SelectedValue" />
        </SelectParameters>
        <%-- Set the auto fields when updating. --%>
        <UpdateParameters>
          <asp:ControlParameter ControlID="lblEditBy" Name="LastEditBy" Type="String" PropertyName="Text"  />
          <asp:ControlParameter ControlID="lblEditDate" Name="LastEditDate" PropertyName="Text" />  
        </UpdateParameters>
      </llblgenpro:LLBLGenProDataSource>... 

Update the value of these controls at page load. (so to be fussy - my edit date/time is actually page load time - but it's good enough for me)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        '  Need to set these as they are referenced by the gridview <updateParameters> section.
        lblEditDate.Text = Now()
        lblEditBy.Text = HttpContext.Current.User.Identity.Name

    
    End Sub

(Name="LastEditBy" referenced in ControlParameter is the name of the column in the underlying table)

Now, I don't have to write any code in order to set default values.

Can anyone improve on this? Any comments welcome!

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Nov-2007 17:44:37   

UpdateParameters can read from a Property on the Page as well (property of the page class). So you don't have to create a label control to store the current date within.

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 05-Dec-2007 18:15:44   

Thanks for another great suggestion, however...

I can now do:

<UpdateParameters>
    <asp:Parameter Type="DateTime" Name="LastUpdated" DefaultValue='10/10/2007'></asp:Parameter>
  </UpdateParameters>

but what I really want is something like:

<UpdateParameters>
    <asp:Parameter Type="DateTime" Name="LastUpdated" DefaultValue=<%#Now()%>'></asp:Parameter>
  </UpdateParameters>

so that the value is updated on every save. However this results in

 Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

Parser Error Message: Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.Parameter does not have a DataBinding event.

Source Error: 


Line 8: EnablePaging="True">
Line 9: <UpdateParameters>
Line 10:     <asp:Parameter Type="DateTime" Name="LastUpdated" DefaultValue='<%#Now() %>'></asp:Parameter>



Is this dynamic updating of the last edit date possible?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 06-Dec-2007 09:36:49   

Add <UpdateParameters> to datasource, binding values to these controls:

Code: ... <llblgenpro:LLBLGenProDataSource ID="dsLine" runat="server" DataContainerType="EntityCollection" EnablePaging="True" EntityCollectionTypeName="Fluor.PipeUtils.DAL.CollectionClasses.LineCollection, Fluor.PipeUtils.DAL"> <SelectParameters> <asp:ControlParameter ControlID="ddlModelFilter" DefaultValue="--Pick--" Name="ModelIndexNo" PropertyName="SelectedValue" /> </SelectParameters> <%-- Set the auto fields when updating. --%> <UpdateParameters> <asp:ControlParameter ControlID="lblEditBy" Name="LastEditBy" Type="String" PropertyName="Text" /> <asp:ControlParameter ControlID="lblEditDate" Name="LastEditDate" PropertyName="Text" /> </UpdateParameters> </llblgenpro:LLBLGenProDataSource>...

Update the value of these controls at page load. (so to be fussy - my edit date/time is actually page load time - but it's good enough for me)

Code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' Need to set these as they are referenced by the gridview <updateParameters> section.
    lblEditDate.Text = Now()
    lblEditBy.Text = HttpContext.Current.User.Identity.Name


End Sub 

(Name="LastEditBy" referenced in ControlParameter is the name of the column in the underlying table)

Now, I don't have to write any code in order to set default values.

Almost similar to the above solution, you can use the PageLoad event to directly insert a parameter (programmatically) into the UpdateParameters collection property of the LLBLGenProDataSource, without the need of using a Form control.

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 06-Dec-2007 12:30:56   

For other people's benefit, this is what is required:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        dsLine.UpdateParameters.Add("LastEditDate", TypeCode.DateTime, Now.ToString)

    End Sub

where LastEditDate is the attribute name (the column name in my underlying oracle table)

Or for a more accurate result, use the ItemUpdating/ItemInserting event on the FormView:


Private Sub fvProjects_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fvProjects.ItemUpdating
        dsProjects.UpdateParameters.Add("LastUpdated", TypeCode.DateTime, Now.ToString)
    End Sub

Many thanks Walaa for help with this... I'm very happy!smile

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 06-Aug-2008 12:15:04   

There is another option for ListView. Use the e.Values and the e.NewValues rather than set the InsertParameters of the llblgenprodatasource:


 Private Sub lvIssue_ItemUpdating(ByVal sender As Object, ByVal e As  System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles lvIssue.ItemUpdating
    e.NewValues("LastUpdate") = Now.ToString
    e.NewValues("LastUpdateBy") = HttpContext.Current.User.Identity.Name
  End Sub


  Private Sub lvIssue_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles lvIssue.ItemInserting

    'dsIssues.InsertParameters.Add("LastUpdate", TypeCode.DateTime, Now.ToString)
    'dsIssues.InsertParameters.Add("LastUpdateBy", HttpContext.Current.User.Identity.Name)

    e.Values("LastUpdate") = Now.ToString
    e.Values("LastUpdateBy") = HttpContext.Current.User.Identity.Name

  End Sub