Getting contents of dynamic fields in .net, based on the AutoForm source Options
thomwhaites
Posted: Friday, October 20, 2006 3:31:36 PM
Rank: Fanatic

Joined: 7/20/2006
Posts: 256
Location: Boston, Massachusetts
I'm trying to build a .net control, based on the AutoForm source, which can be used to create mail contact forms with dynamic fields.

I've got the control so that it builds the form correctly, but I can't figure out the proper way to reference the contents of the dynamic fields to include them in my e-mail message. I don't want to store the messages in Umbraco, I just want to send an e-mail.

Can anyone give me a .NET lesson??

Here's my code snippet, help greatly appreciated!

Code:
       override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            buildForm();
        }

        private void buildForm()
        {
            //GET THE DOCUMENT TYPE USED TO BUILD THE FORM
            foreach (umbraco.cms.businesslogic.ContentType.TabI t in new umbraco.cms.businesslogic.web.DocumentType(_nodeTypeId).getVirtualTabs)
            {
                //GET THE FIELDS USED IN THE FORM BY SELECTING THE APPROPRIATE TAB
                if (("," + _tabName + ",").IndexOf("," + t.Caption.ToLower() + ",") > -1)
                {
                    //GET THE PROPERTY TYPES FROM THE SELECTED TAB
                    foreach (umbraco.cms.businesslogic.propertytype.PropertyType pt in t.PropertyTypes)
                    {
                        umbraco.interfaces.IDataType dt = pt.DataTypeDefinition.DataType;
                        dt.DataEditor.Editor.ID = pt.Alias;

                        umbraco.interfaces.IData df = pt.DataTypeDefinition.DataType.Data;

                        ((Control)dt.DataEditor.Editor).ID = pt.Alias;
                        phFormFields.Controls.Add(new LiteralControl("<fieldset><label for=\"" + userControlId + ((Control)dt.DataEditor.Editor).ClientID + "\">" + pt.Name + ":</label>"));
                        phFormFields.Controls.Add((Control)dt.DataEditor.Editor);
                        
                                //    ... ADD VALIDATION CONTROLS for MANDITORY & REGEX FIELDS ...
                 
                        phFormFields.Controls.Add(new LiteralControl("</fieldset>"));
                    }
                }
            } 
        }

        private void sendMail()
        {
                    // ... USE SYSTEM.NET.MAIL TO SEND SMTP
                    // ... GET BODY OF MAIL TEXT WITH buildMsgBody() ...
                }

        private string buildMsgBody()
        {
            StringBuilder sb = new StringBuilder();
                // **** HOW DO I REFERENCE THE DYNAMICALLY CREATED CONTROLS?
                // **** I NEED TO GRAB TEXT CONTENTS FROM CONTROLS HERE....           

            return sb.ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                sendMail();
            }
        }



Hope that formats correctly! Thanks for the help.

Tom
thomwhaites
Posted: Friday, October 20, 2006 3:33:30 PM
Rank: Fanatic

Joined: 7/20/2006
Posts: 256
Location: Boston, Massachusetts
This question is still open.

I've just noticed that the first reply sometimes fixes the code formatting, hope it works here.
thomwhaites
Posted: Monday, October 23, 2006 5:06:06 PM
Rank: Fanatic

Joined: 7/20/2006
Posts: 256
Location: Boston, Massachusetts
Is this the very obvious solution I've overlooked, or is there something insecure in directly grabbing data from the Response.Form collection?

Code:

        private string buildMsgBody()
        {
            StringBuilder sb = new StringBuilder();
           
            IDictionaryEnumerator en = _dataFields.GetEnumerator();
            while (en.MoveNext())
            {
                sb.Append("<strong>" + en.Key + "</strong><br />");
                try
                {
                    string msg = System.Web.HttpContext.Current.Request.Form[en.Value.ToString()].ToString();
                    sb.Append(Server.HtmlEncode(msg));

                }
                catch (Exception e)
                {
                    sb.Append("ERROR: " + e);
                }

                sb.Append("<br /><br />");
            }
            return sb.ToString();
        }


_dataFields is a Hashtable with the label and name of the form fields I'd like to pass (rather than include all form fields in my email).
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.