Javascript – Copy info from one dynamically created user control to another dynamically created user control

asp.netcopyjavascriptuser-controls

My ASP.NET WebForm has a place holder on it and user controls are dynamically added to it. The controls mostly have textboxes. Sometimes there are two user controls, sometimes there are ten. Everything is working as expected. My question is about how to implement a new feature.

My customer has a new request to copy data from the first control to another control checking a checkbox associated with the additional control in question.

At first, this sounded easy… Add a checkbox to the placeholder for each user control and then write some JavaScript to copy the data from the first control to the additional control. Then I realized that by those textboxes being in a user control, I don't really have access to modify the HTML inputs directly.

Next I started thinking about adding checkboxes that will automatically post back, but how do I go about dynamically adding a checkbox to a placeholder, and then come up with a way to add event handler to the checkbox and pass in the information necessary to loop through the controls and copy the values. This approach seems way too complicated with too much overhead to accomplish my goal.

Any suggestions?

Best Answer

You mentioned that since the checkboxes are in a user control, you don't have access to them.

Could you expose the ClientIDs using a property of the user control and then work with them in javascript? Something like this:

user_control {
   int checkboxId { get { return checkbox.ClientId; } }
}

If you have more code that would be helpful...