using System; using System.Data; using System.Configuration; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Net.Mail; using System.Collections; using System.Text; using WebBlocks; namespace WebBlocks { public class Contact : System.Web.UI.WebControls.CompositeControl { internal class ContactField : System.Web.UI.Control { protected WebControl ctl; protected RequiredFieldValidator rfv; protected RegularExpressionValidator rev; private ContactFormFieldType FieldType; private string[] Options; private string Regex; private bool Required; private int RowIndex; public string Label; public string Value { get { switch (FieldType) { case ContactFormFieldType.TextBox: return ((TextBox)Controls[0]).Text; case ContactFormFieldType.TextArea: return ((TextBox)Controls[0]).Text; case ContactFormFieldType.DropDownList: return ((DropDownList)Controls[0]).SelectedValue; case ContactFormFieldType.CheckBox: return (((CheckBox)Controls[0]).Checked ? "Yes" : "No"); case ContactFormFieldType.CheckBoxList: StringBuilder sb = new StringBuilder(); CheckBoxList cbl = (CheckBoxList)Controls[0]; foreach (ListItem li in cbl.Items) { sb.Append(li.Value); sb.Append(","); } return sb.ToString().TrimEnd(','); case ContactFormFieldType.RadioButtonList: return ((RadioButtonList)Controls[0]).SelectedValue; case ContactFormFieldType.Separator: return null; default: return null; } } set { switch (FieldType) { case ContactFormFieldType.TextBox: ((TextBox)Controls[0]).Text = value; break; case ContactFormFieldType.TextArea: ((TextBox)Controls[0]).Text = value; break; case ContactFormFieldType.DropDownList: ((DropDownList)Controls[0]).SelectedValue = value; break; case ContactFormFieldType.CheckBox: ((CheckBox)Controls[0]).Checked = value.ToLower().Equals("yes"); break; case ContactFormFieldType.CheckBoxList: CheckBoxList cbl = (CheckBoxList)Controls[0]; foreach (ListItem li in cbl.Items) li.Selected = false; foreach (string s in value.Split(',')) foreach (ListItem li in cbl.Items) if (s == li.Value) li.Selected = true; break; case ContactFormFieldType.RadioButtonList: ((RadioButtonList)Controls[0]).SelectedValue = value; break; case ContactFormFieldType.Separator: break; default: break; } } } public ContactField(int _RowIndex, ContactFormFieldType _FieldType, string _Label, string _Options, string _Regex, bool _Required) { RowIndex = _RowIndex; FieldType = _FieldType; Label = _Label; Options = _Options.Split(','); Regex = _Regex; Required = _Required; } protected override void OnInit(EventArgs e) { string ControlID = Parent.ID + "_" + RowIndex; switch (FieldType) { case ContactFormFieldType.TextBox: TextBox tb = new TextBox(); tb.ID = ControlID; tb.CssClass = "textField"; ctl = tb; Controls.Add(tb); break; case ContactFormFieldType.TextArea: TextBox ta = new TextBox(); ta.ID = ControlID; ta.TextMode = TextBoxMode.MultiLine; ta.CssClass = "textArea"; ctl = ta; Controls.Add(ta); break; case ContactFormFieldType.DropDownList: DropDownList ddl = new DropDownList(); ddl.ID = ControlID; foreach (string s in Options) ddl.Items.Add(s); ctl = ddl; Controls.Add(ddl); break; case ContactFormFieldType.CheckBox: CheckBox cb = new CheckBox(); cb.ID = ControlID; ctl = cb; Controls.Add(cb); break; case ContactFormFieldType.CheckBoxList: CheckBoxList cbl = new CheckBoxList(); cbl.ID = ControlID; foreach (string s in Options) cbl.Items.Add(s); ctl = cbl; Controls.Add(cbl); break; case ContactFormFieldType.RadioButtonList: RadioButtonList rbl = new RadioButtonList(); rbl.ID = ControlID; foreach (string s in Options) rbl.Items.Add(s); ctl = rbl; Controls.Add(rbl); break; case ContactFormFieldType.Separator: Label li = new Label(); li.ID = ControlID; li.Text = Label; li.CssClass = "separator"; ctl = li; Controls.Add(li); Label = ""; break; default: break; } if (Required) { rfv = new RequiredFieldValidator(); rfv.ControlToValidate = ControlID; rfv.ErrorMessage = "Required"; rfv.Display = ValidatorDisplay.Dynamic; rfv.ValidationGroup = Parent.ID; Controls.Add(rfv); } if (Regex != null && Regex.Length > 0) { rev = new RegularExpressionValidator(); rev.ControlToValidate = ControlID; rev.ErrorMessage = "Please ensure your entry is valid."; rev.Display = ValidatorDisplay.Dynamic; rev.ValidationExpression = Regex; rev.ValidationGroup = Parent.ID; Controls.Add(rev); } } protected override void Render(HtmlTextWriter writer) { writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.AddAttribute(HtmlTextWriterAttribute.Class, "labelColumn"); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.Write(Label); writer.RenderEndTag(); writer.AddAttribute(HtmlTextWriterAttribute.Class, "fieldColumn"); writer.RenderBeginTag(HtmlTextWriterTag.Td); if (ctl != null) ctl.RenderControl(writer); if (rfv != null) rfv.RenderControl(writer); if (rev != null) rev.RenderControl(writer); writer.RenderEndTag(); writer.RenderEndTag(); } } private DatabaseClass Database { get { return (DatabaseClass)((WebBlockPage)Page).Database; } } public string BlockTitle { get { return (string)ViewState["BlockTitle"]; } set { ViewState["BlockTitle"] = value; } } protected ImageButton btnSubmit; protected DataRow ContactFormRow = null; protected override void CreateChildControls() { // load in contact form data ContactFormRow = Database.ContactForm_Get(BlockTitle); if (ContactFormRow == null) { // we need to autogenerate this contact form Database.ContactForm_AutoGenerate(BlockTitle); ContactFormRow = Database.ContactForm_Get(BlockTitle); } DataTable dt = Database.ContactFormFields_GetByContactFormID((int)ContactFormRow["ContactFormID"]); for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr = dt.Rows[i]; ContactFormFieldType FieldType = (ContactFormFieldType)dr["FieldType"]; string Label = (string)dr["Label"]; string Options = (dr["Options"] == DBNull.Value ? "" : (string)dr["Options"]); string Regex = (dr["Validation"] == DBNull.Value ? "" : (string)dr["Validation"]); bool Required = (bool)dr["Required"]; ContactField cf = new ContactField(i, FieldType, Label, Options, Regex, Required); Controls.Add(cf); } btnSubmit = new ImageButton(); btnSubmit.ID = "btnSubmit"; btnSubmit.ImageUrl = "images/submit.gif"; btnSubmit.Click += new ImageClickEventHandler(btnSubmit_Click); btnSubmit.ValidationGroup = this.ID; Controls.Add(btnSubmit); } protected void btnSubmit_Click(object sender, EventArgs e) { System.Collections.Specialized.NameValueCollection results = new System.Collections.Specialized.NameValueCollection(Controls.Count-1); StringBuilder sb = new StringBuilder(); MailMessage msg = new MailMessage(); int ContactFormID = (int)ContactFormRow["ContactFormID"]; string FromName = (string)ContactFormRow["FromName"]; string FromAddress = (string)ContactFormRow["FromAddress"]; string RecipientAddresses = (string)ContactFormRow["RecipientAddresses"]; string Subject = (string)ContactFormRow["Subject"]; string ThankYouMessage = (string)ContactFormRow["ThankYouMessage"]; msg.From = new MailAddress(FromAddress, FromName); msg.To.Add(RecipientAddresses); msg.Subject = Subject; msg.IsBodyHtml = false; foreach (Control c in Controls) { if (c is ContactField) { ContactField cf = (ContactField)c; sb.Append(cf.Label); sb.Append(": "); sb.Append(cf.Value); sb.Append("\r\n"); results.Add(cf.Label, cf.Value); } } sb.Append("\r\n\r\nSubmitted on: "); sb.Append(DateTime.Now.ToShortDateString()); sb.Append(" @ "); sb.Append(DateTime.Now.ToShortTimeString()); msg.Body = sb.ToString(); string ErrorMessage = null; try { SmtpClient sc = new SmtpClient(); sc.Send(msg); } catch (Exception ex) { ErrorMessage = ex.Message; } // save submission to database Database.ContactFormSubmissions_Add(ContactFormID, results, ErrorMessage); // display thank-you message if there is one if (ThankYouMessage != null && ThankYouMessage.Length > 0) Page.ClientScript.RegisterStartupScript(typeof(string), "success", "alert('" + ThankYouMessage + "');", true); } protected override void Render(HtmlTextWriter writer) { writer.RenderBeginTag(HtmlTextWriterTag.Table); foreach (Control c in Controls) { if (c is ContactField) c.RenderControl(writer); } writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.RenderEndTag(); writer.RenderBeginTag(HtmlTextWriterTag.Td); btnSubmit.RenderControl(writer); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderEndTag(); } } public enum ContactFormFieldType : int { TextBox = 1, TextArea = 2, DropDownList = 3, CheckBox = 4, CheckBoxList = 5, RadioButtonList = 6, Separator = 7 } }