using System; using System.Collections.Generic; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; [assembly: WebResource("ChadSoft.Web.Controls.FormCopier.js", "text/javascript")] namespace ChadSoft.Web.Controls { /// /// Used to create mappings between form input elements to /// allow dynamic synchronization (e.g. "Same as Billing Address") /// public class FormCopier : Control { private const string AddMappingFormat = "{0}.AddMapping(document.getElementById('{1}'), document.getElementById('{2}'));\n"; /// /// Gets or sets a value indicating whether [same as associated]. /// /// true if [same as associated]; otherwise, false. public bool SameAsAssociated { get { object sameAsAssociated = ViewState["SameAsAssociated"]; if (sameAsAssociated != null) return (bool)sameAsAssociated; else return true; } set { ViewState["SameAsAssociated"] = value; } } protected override void OnLoad(EventArgs e) { // Register the associated FormCopier script Page.ClientScript.RegisterClientScriptResource( typeof(FormCopier), "ChadSoft.Web.Controls.FormCopier.js"); } protected override void Render(HtmlTextWriter writer) { // Build and register init script string scriptName = String.Format("PopulateMappings_{0}", ClientID); string initScript = BuildInitScript(); Page.ClientScript.RegisterStartupScript(typeof(FormCopier), scriptName, initScript, true); } protected string BuildInitScript() { StringBuilder script = new StringBuilder(); // Create the formCopier variable script.AppendFormat("var {0} = new ChadSoft.Web.Controls.FormCopier();\n", this.ClientID); // Add all of the mappings foreach (string controlId in ControlMappings.Keys) { script.AppendFormat(AddMappingFormat, this.ClientID, controlId, ControlMappings[controlId]); } script.AppendFormat("{0}.SetSameAsAssociated({1});\n", this.ClientID, this.SameAsAssociated.ToString().ToLower()); return script.ToString(); } public Dictionary ControlMappings { get { object controlMappings = ViewState["ControlMappings"]; if (controlMappings == null) { controlMappings = new Dictionary(); ViewState["ControlMappings"] = controlMappings; } return (Dictionary)controlMappings; } } public string AddMapping(string sourceID, string destinationID) { if (String.IsNullOrEmpty(sourceID)) throw new ArgumentNullException("sourceID"); if (String.IsNullOrEmpty(destinationID)) throw new ArgumentNullException("destinationID"); ControlMappings[sourceID] = destinationID; return sourceID; } public string AddMapping(HtmlInputControl source, HtmlInputControl destination) { if (source == null) throw new ArgumentNullException("source"); if (destination == null) throw new ArgumentNullException("destination"); return AddMapping(source.ClientID, destination.ClientID); } public string AddMapping(WebControl source, WebControl destination) { if (source == null) throw new ArgumentNullException("source"); if (destination == null) throw new ArgumentNullException("destination"); return AddMapping(source.ClientID, destination.ClientID); } public void RemoveMapping(HtmlInputControl source) { RemoveMapping(source.ClientID); } public void RemoveMapping(string sourceID) { ControlMappings.Remove(sourceID); } public void Clear() { ControlMappings.Clear(); } } }