Last post Jan 09, 2012 05:07 AM by micmac
Jun 01, 2009 12:28 PM|geltendorf|LINK
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
Jun 01, 2009 12:49 PM|sjnaughton|LINK
I've done it but not generically I did a custom page for each page I wanted this on.
Jun 01, 2009 03:36 PM|geltendorf|LINK
Do you have some sample posted somewhere?
Jun 01, 2009 05:00 PM|sjnaughton|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
// goto the new product
Jun 02, 2009 09:33 AM|geltendorf|LINK
Yes, it does. Thank you for your help.
Jun 03, 2009 01:43 PM|geltendorf|LINK
I've found much better generic solution done by member of CodePlex. See it at http://n41dynamicdataex.codeplex.com/.
Jun 04, 2009 01:25 AM|ricka6|LINK
Thanks for the link to the Clone Extender. I'm going to add it to the
Dynamic Data FAQ
Dec 01, 2009 10:43 AM|steddyman|LINK
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.
Dec 01, 2009 01:21 PM|sjnaughton|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:
this post seems to have been moved or deleted.
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.
Dec 02, 2009 05:45 AM|steddyman|LINK
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.
Dec 02, 2009 06:44 AM|steddyman|LINK
This is worse than I though. It doesn't look like I can use FindControl on the FormView to locate the Dynamic control and cast it to the appropriate type.
The FindControl is returning a type of DynamicControl, so how do I set the text value of the underlying Textbox (or other type)?
I remember asking this question before in this thread, but the answer given there is not suitable in this situation since i want the value to go into the form so it can be edited by the user:
Dec 02, 2009 09:26 AM|steddyman|LINK
I've found a way to do this. Its not beautiful but it work:
String ID = Request.QueryString["copyid"];
if (ID != null && !Page.IsPostBack)
Int32 intID = Int32.Parse(ID);
// Find the record
VRMDataContext db = new VRMDataContext();
Request r = db.Requests.Single(p => p.id == intID);
// Populate all the controls with the existing value passed to the function
// Change the value of the named Dynamic Control. Must be a child of FormView1 and use TextBox1 in the FieldTemplate
protected void SetDCTextControl(String name, String value)
DynamicControl dc = ((DynamicControl)FormView1.FindControl(name));
((TextBox)dc.FieldTemplate.FindControl("TextBox1")).Text = value;
// Change the value of the named Dynamic Control. Must be a child of FormView1 and use DropDownList1 in the FieldTemplate
protected void SetDCDropControl(String name, int value)
DynamicControl dc = ((DynamicControl)FormView1.FindControl(name));
DropDownList dl = ((DropDownList)dc.FieldTemplate.FindControl("DropDownList1"));
String svalue = value.ToString();
ListItem item = dl.Items.FindByValue(svalue);
if (item != null) dl.SelectedValue = svalue;
Feb 25, 2011 08:32 AM|PeterSkellern|LINK
What to do if you want to clone many? I have added a checkbox column to my grid so I can choose the records to be cloned. It is clear that I don't need to copy records with the very same data on them so I need to add some work to display dropdownlists in
the records to be changed as to make the cloning process meaningful. An N41 DynamicData Extender sample is not really clear regarding its implementation, it works fine but trying to use it in my own web site has not been really straightforward as I imagined.
Feb 25, 2011 10:43 AM|sjnaughton|LINK
Hi Steddyman, have a look at N41 DynamicData Extender adds clone behaviour to Scaffold Tables it has issues with DD4 but it's worth taking a look at how it does it's cloning, you could just implemnet the
bits you need,
Mar 01, 2011 01:32 AM|klca|LINK
Carlos Porras (El Salvador)
Jan 09, 2012 04:51 AM|micmac|LINK
Where does this code get loaded to? As a button on the Edit page? This looks like it will work but I am not sure where to implement it in my website?
Jan 09, 2012 05:07 AM|micmac|LINK
In short, I am totally lost on how best to implement this solution. N41 DynamicData Extender was built in C# with Data Entity model where my site is Linq to SQL in VB. Now I am able to conver the C# to vb but having a whale of a time trying to implement
N41 DynamicData Extender into my site. Any suggestions?