How to override default insert method of Details View in Dynamic Data

Last post 04-21-2009 6:54 AM by zorgit. 6 replies.

Sort Posts:

  • How to override default insert method of Details View in Dynamic Data

    04-20-2009, 7:55 AM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 5:43 AM
    • Posts 16

    I would like to change the default method of inserting data to make it use a stored procedure, plus I would like to get a return value from this stored procedure returned to the calling code.

    Scenario is that on insert the default behaviour is to go to the list view, but I would like to get from the insert the autogenerated primary key and use this to re-direct to a different form showing the newly inserted record.

    I see that I can create a partial class for the DataContext class and then create my own InsertTable method which will be called on the insert action, but this is a sub so it is not clear how to return a value to the Insert.aspx code behind class for processing.

    I can override the DetailsView ItemInserting method, but to use this I would need to set the cancel property to true to prevent double insertion, and then the validation behaviour does not work.

    So how would this be done?

    Thanks

     

    Filed under:
  • Re: How to override default insert method of Details View in Dynamic Data

    04-20-2009, 2:05 PM
    Answer
    • Star
      11,914 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,501
    • TrustedFriends-MVPs
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under: ,
  • Re: How to override default insert method of Details View in Dynamic Data

    04-21-2009, 6:12 AM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 5:43 AM
    • Posts 16

    Steve,

    Thanks for the reply, the article was very helpful, I can now use the stored procedure to insert the information with the returned value added to the passed in instance. However it is still not clear how to access this from the DetailsView_ItemInserted method.

    In the DataContextClass the generated code is:

    Private Sub InsertArticle(ByVal obj As Article)
      Dim p1 As System.Nullable(Of Integer) = obj.ArticleId
      Me.fgp_InsertArticle(p1, obj.HTMLPageName, obj.Title, obj.Summary, obj.PageContent)
      obj.ArticleId = p1.GetValueOrDefault
    End Sub

    this is called correctly when I click the Insert button on the detailsview object and the correct Id is returned to the InsertArticle routine but in the DetailsView_ItemInserted how to I get access to this value- looping through the values collection of the DetailsViewCommandEventArgs displays all the values of the fields as entered by the user but not this returned value. Even if I add a field for the ArticleID to the DetailsView it does not get the value from the InsertArticle routine. Generally how would I access the instance of the DataContextClass used by the DetailsView to perform this insert?

    Thanks

  • Re: How to override default insert method of Details View in Dynamic Data

    04-21-2009, 6:24 AM
    Answer
    • Star
      11,914 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,501
    • TrustedFriends-MVPs

    You should now get an entity with the PK value in it now in the DetailsView_ItemInserted method.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under: ,
  • Re: How to override default insert method of Details View in Dynamic Data

    04-21-2009, 6:35 AM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 5:43 AM
    • Posts 16

    Steve,

    I'm not sure how I would reference it, but I have found that in DetailsDataSource_Inserted the eventargs contains a Result object which is the returned value, which can be cast to the object type and contains the returned ID.

    Thanks for your help!

  • Re: How to override default insert method of Details View in Dynamic Data

    04-21-2009, 6:44 AM
    Answer
    • Star
      11,914 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,501
    • TrustedFriends-MVPs

    Sorry I was forgetting (I can't converet VB=C# in my head) you need to use the LinqDataSource's Inserted event see: 

    Protected Sub DetailsDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceStatusEventArgs) Handles DetailsDataSource.Inserted
        If ((e.Exception Is Nothing) _
                    OrElse e.ExceptionHandled) Then
            Response.Redirect(table.GetActionPath(PageAction.Details, e.Result))
        End If
    End Sub
    
    Hope this is it Big Smile
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: How to override default insert method of Details View in Dynamic Data

    04-21-2009, 6:54 AM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 5:43 AM
    • Posts 16

    Does exactly what it says on the tin!

Page 1 of 1 (7 items)