Is there anybody having sample of templates to add "clone/copy" functionality to ASP.NET dynamic data? I would like to clone/copy existing record into new one and insert to the table with new primary key. Click on "Clone"/"Copy" link next to edit/delete/details
should display identical original record content and click on "Update" link insert such record to the underlying table.Thank you.
Well to put it simply what I did was copy an Edit page into a custom Pages folder, then add a button for copy/clone and in the click event take all the fields and create a new one and then insert it into the DB.
protected void btnCopyProduct_Click(object sender, EventArgs e)
{
var DC = new BFDataContext();
int pf_id = (int)FormView1.SelectedValue;
var product = DC.bf_products.FirstOrDefault(p => p.pf_id == pf_id);
var copyProduct = new bf_product()
{
Accessories = product.Accessories,
active = product.active,
AddtoRooms = product.AddtoRooms,
attr_label1 = product.attr_label1,
attr_label2 = product.attr_label2,
attr_label3 = product.attr_label3,
attr_label4 = product.attr_label4,
attr_label5 = product.attr_label5,
CategoryID = product.CategoryID,
Color = product.Color,
cost = product.cost,
DateEntered = product.DateEntered,
description = product.description,
Features = product.Features,
Height = product.Height,
image_file = product.image_file,
image_height = product.image_height,
image_width = product.image_width,
ItemType = product.ItemType,
lastmodified = product.lastmodified,
Length = product.Length,
list_price = product.list_price,
ManufacturerID = product.ManufacturerID,
ManufacturerURL = product.ManufacturerURL,
Model = product.Model,
// increment name to prevent identical products
name = "COPY - " + product.name, // + "_" + i,
ObjectModel = product.ObjectModel,
OwnerID = product.OwnerID,
PreviousModel = product.PreviousModel,
sdesc = product.sdesc,
Size = product.Size,
Sku = product.Sku,
SpecialRemarks1 = product.SpecialRemarks1,
Style = product.Style,
Type = product.Type,
Weight = product.Weight,
Width = product.Width
};
DC.bf_products.InsertOnSubmit(copyProduct);
DC.SubmitChanges();
// goto the new product
Response.Redirect(table.GetActionPath(PageAction.Details, copyProduct));
}
Does this make some sense?
Dynamic DataCustom PagesClone/Copy
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Marked as answer by geltendorf on Jun 02, 2009 03:37 PM
Can you think of a way to do this without inserting the record in the database first?
I have custom pages derived from the standard DD ones, one is a View/Edit page and another is an Insert page all using FormView's with DynamicControls.
I have a button on the View/Edit page that when clicked I want it to redirect to the Insert page but with all the fields filled in from the data on the original record.
I don't want this data inserted into the database until the user clicks the Insert button on the Insert page. The reason for this is I want to copy across some but not all the fields from the source record, and force the user to enter values for the ones
that are not copied across. This is because some of the fields have Unique constraints.
I can obviously understand how to clone records easily, but what I dont get is how to get the Insert page to show the data to edit in Insert mode since it is a FormView using DD controls so they cannot easily be manipluated in the codebehind.
Also in the Futures and .Net 4.0 Beta 2 there is a way of passing in an object with the default values in it using: the FormView extension method SetMetaTable() to pass in the default values object.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Thanks for your response. The second link above seems to be incorrect and just points back to this post.
My plan was to pass the PK_id of the source recrod to copy to my insert page and then have that populate all the controls on the Page_Load or some other event.
But my problem is all these controls are dynamic controls and I would need to resort to messy FindControl methods to implement it that way. I was kind of hoping there was a way to hack into the existing session / postdata to populate the controls but don't
understand it well enough.
I also wasn't sure how to prevent this posted data reseting the controls back to that state when a validation error occurs.
geltendorf
Member
3 Points
17 Posts
Clone/Copy existing table row
Jun 01, 2009 04:28 PM|LINK
Hi guys,
Is there anybody having sample of templates to add "clone/copy" functionality to ASP.NET dynamic data? I would like to clone/copy existing record into new one and insert to the table with new primary key. Click on "Clone"/"Copy" link next to edit/delete/details should display identical original record content and click on "Update" link insert such record to the underlying table.Thank you.
Custom Page Templates Code Generation
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Clone/Copy existing table row
Jun 01, 2009 04:49 PM|LINK
I've done it but not generically I did a custom page for each page I wanted this on.
Dynamic Data Custom Pages Clone/Copy
Always seeking an elegant solution.
geltendorf
Member
3 Points
17 Posts
Re: Clone/Copy existing table row
Jun 01, 2009 07:36 PM|LINK
Do you have some sample posted somewhere?
Thank you.
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Clone/Copy existing table row
Jun 01, 2009 09:00 PM|LINK
Well to put it simply what I did was copy an Edit page into a custom Pages folder, then add a button for copy/clone and in the click event take all the fields and create a new one and then insert it into the DB.
protected void btnCopyProduct_Click(object sender, EventArgs e) { var DC = new BFDataContext(); int pf_id = (int)FormView1.SelectedValue; var product = DC.bf_products.FirstOrDefault(p => p.pf_id == pf_id); var copyProduct = new bf_product() { Accessories = product.Accessories, active = product.active, AddtoRooms = product.AddtoRooms, attr_label1 = product.attr_label1, attr_label2 = product.attr_label2, attr_label3 = product.attr_label3, attr_label4 = product.attr_label4, attr_label5 = product.attr_label5, CategoryID = product.CategoryID, Color = product.Color, cost = product.cost, DateEntered = product.DateEntered, description = product.description, Features = product.Features, Height = product.Height, image_file = product.image_file, image_height = product.image_height, image_width = product.image_width, ItemType = product.ItemType, lastmodified = product.lastmodified, Length = product.Length, list_price = product.list_price, ManufacturerID = product.ManufacturerID, ManufacturerURL = product.ManufacturerURL, Model = product.Model, // increment name to prevent identical products name = "COPY - " + product.name, // + "_" + i, ObjectModel = product.ObjectModel, OwnerID = product.OwnerID, PreviousModel = product.PreviousModel, sdesc = product.sdesc, Size = product.Size, Sku = product.Sku, SpecialRemarks1 = product.SpecialRemarks1, Style = product.Style, Type = product.Type, Weight = product.Weight, Width = product.Width }; DC.bf_products.InsertOnSubmit(copyProduct); DC.SubmitChanges(); // goto the new product Response.Redirect(table.GetActionPath(PageAction.Details, copyProduct)); }Does this make some sense?Dynamic Data Custom Pages Clone/Copy
Always seeking an elegant solution.
geltendorf
Member
3 Points
17 Posts
Re: Clone/Copy existing table row
Jun 02, 2009 01:33 PM|LINK
Yes, it does. Thank you for your help.
geltendorf
Member
3 Points
17 Posts
Re: Clone/Copy existing table row
Jun 03, 2009 05:43 PM|LINK
Hi,
I've found much better generic solution done by member of CodePlex. See it at http://n41dynamicdataex.codeplex.com/.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Clone/Copy existing table row
Jun 04, 2009 05:25 AM|LINK
Thanks for the link to the Clone Extender. I'm going to add it to the Dynamic Data FAQ
steddyman
Member
23 Points
82 Posts
Re: Clone/Copy existing table row
Dec 01, 2009 02:43 PM|LINK
Hi Steve
Can you think of a way to do this without inserting the record in the database first?
I have custom pages derived from the standard DD ones, one is a View/Edit page and another is an Insert page all using FormView's with DynamicControls.
I have a button on the View/Edit page that when clicked I want it to redirect to the Insert page but with all the fields filled in from the data on the original record.
I don't want this data inserted into the database until the user clicks the Insert button on the Insert page. The reason for this is I want to copy across some but not all the fields from the source record, and force the user to enter values for the ones that are not copied across. This is because some of the fields have Unique constraints.
I can obviously understand how to clone records easily, but what I dont get is how to get the Insert page to show the data to edit in Insert mode since it is a FormView using DD controls so they cannot easily be manipluated in the codebehind.
Thanks
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Clone/Copy existing table row
Dec 01, 2009 05:21 PM|LINK
Hi Steaddyman, yes I can but its a pain pass all the propert values in the query string and ad an insert default check see:
http://csharpbits.notaclue.net/2009/01/getting-default-values-from-list-page.html
http://forums.asp.net/AddPost.aspx?ReplyToPostID=3541909&Quote=False this post seems to have been moved or deleted.
http://csharpbits.notaclue.net/2008/06/dynamicdata-foregnkeyedit-default.html
Also in the Futures and .Net 4.0 Beta 2 there is a way of passing in an object with the default values in it using: the FormView extension method SetMetaTable() to pass in the default values object.
Always seeking an elegant solution.
steddyman
Member
23 Points
82 Posts
Re: Clone/Copy existing table row
Dec 02, 2009 09:45 AM|LINK
Hi Steve
Thanks for your response. The second link above seems to be incorrect and just points back to this post.
My plan was to pass the PK_id of the source recrod to copy to my insert page and then have that populate all the controls on the Page_Load or some other event.
But my problem is all these controls are dynamic controls and I would need to resort to messy FindControl methods to implement it that way. I was kind of hoping there was a way to hack into the existing session / postdata to populate the controls but don't understand it well enough.
I also wasn't sure how to prevent this posted data reseting the controls back to that state when a validation error occurs.
Thanks