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
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.