In the recent upgrade from Ektron 8.0.2 to 8.7 I’ve been rebuilding forms to user controls that can be used as widgets.
Below is basic elements to make the form work for your Ektron 8.7 (possibly higher).
Please note the TextBox SubmissionEmail, this allows you to change the email address that the form goes to without having to get into the code.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="form.ascx.cs" Inherits="codebehindclass" %> <asp:MultiView ID="ViewSet" runat="server" ActiveViewIndex="0"> <asp:View ID="View" runat="server"> <asp:Label ID="lblSubmissionEmail" runat="server" Visible="false"></asp:Label> <asp:Label ID="labelErrors" CssClass="jobText" runat="server" Style="color: #81060C;" Visible="false"></asp:Label> <asp:Literal ID="litThanks" runat="server" Visible="false"> Your form has been recieved, thank you for your time.<br /> <a href="<URL of form>">Click here to complete a new form.</a> </asp:Literal> <div id="divForm" class="contact" runat="server"> <asp:Label ID="lblProviderName" runat="server" Text="Provider Name"></asp:Label> <asp:TextBox ID="txtProviderName" runat="server"></asp:TextBox> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> </div> </asp:View> <asp:View ID="Edit" runat="server"> <div id="<%=ClientID%>_edit"> <asp:TextBox ID="SubmissionEmail" runat="server" Style="width: 95%" placeholder="Submission Email Address"> </asp:TextBox> <asp:Button ID="CancelButton" runat="server" Text="Cancel" OnClick="CancelButton_Click" /> <asp:Button ID="SaveButton" runat="server" Text="Save" OnClick="SaveButton_Click" /> </div> </asp:View> </asp:MultiView>
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using Ektron.Cms.Widget; using Ektron.Cms; using Ektron.Cms.API; using Ektron.Cms.Common; using Ektron.Cms.PageBuilder; using System.Configuration; using System.Data.SqlClient; using System.Net.Mail; public partial class codebehindclass : System.Web.UI.UserControl, IWidget { public static string connstring = ConfigurationManager.ConnectionStrings["<Web.config Connection String>"].ToString(); #region properties private string _sSubmissionEmail; [WidgetDataMember("Form")] public string sSubmissionEmail { get { return _sSubmissionEmail; } set { _sSubmissionEmail = value; } } #endregion IWidgetHost _host; protected ContentAPI m_refContentApi = new ContentAPI(); protected EkMessageHelper m_refMsg; protected void Page_Init(object sender, EventArgs e) { string sitepath = new CommonApi().SitePath; JS.RegisterJSInclude(this, JS.ManagedScript.EktronJS); JS.RegisterJSInclude(this, JS.ManagedScript.EktronModalJS); Css.RegisterCss(this, Css.ManagedStyleSheet.EktronModalCss); _host = Ektron.Cms.Widget.WidgetHost.GetHost(this); m_refMsg = m_refContentApi.EkMsgRef; _host.Title = "Submission Email"; _host.Edit += new EditDelegate(EditEvent); _host.Maximize += new MaximizeDelegate(delegate() { Visible = true; }); _host.Minimize += new MinimizeDelegate(delegate() { Visible = false; }); _host.Create += new CreateDelegate(delegate() { EditEvent(""); }); CancelButton.Text = m_refMsg.GetMessage("btn cancel"); SaveButton.Text = m_refMsg.GetMessage("btn save"); PreRender += new EventHandler(delegate(object PreRenderSender, EventArgs Evt) { SetOutput(); }); string myPath = string.Empty; ViewSet.SetActiveView(View); } void EditEvent(string settings) { SubmissionEmail.Text = sSubmissionEmail; ViewSet.SetActiveView(Edit); } protected void SaveButton_Click(object sender, EventArgs e) { sSubmissionEmail = SubmissionEmail.Text; _host.SaveWidgetDataMembers(); ViewSet.SetActiveView(View); } protected void SetOutput() { lblSubmissionEmail.Text = sSubmissionEmail; } protected void CancelButton_Click(object sender, EventArgs e) { ViewSet.SetActiveView(View); } protected void btnSubmit_Click(object sender, EventArgs e) { if (ValidateForm()) { string sSQL = "INSERT INTO <tablename>([SubmissionDateTime] " + ",[ProviderName]) VALUES (@SubmissionDateTime" + ",@ProviderName)"; try { SqlConnection Conn = new SqlConnection(connstring); Conn.Open(); SqlCommand cmd = new SqlCommand(sSQL + " SELECT CAST(scope_identity() AS int)", Conn); cmd.Parameters.AddWithValue("@SubmissionDateTime", DateTime.Now.ToString()); cmd.Parameters.AddWithValue("@ProviderName", txtProviderName.Text); int NewRecord = (int)cmd.ExecuteScalar(); Conn.Close(); #region Development Email MailMessage message = new MailMessage("<Send From Email>", sSubmissionEmail); SmtpClient smtpClient = new SmtpClient(); smtpClient.Port = 25; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.UseDefaultCredentials = false; smtpClient.Host = "<SMTP Server>"; message.Subject = "<Email Subject> - " + NewRecord.ToString(); message.Body = "A new form has been submitted"; #endregion //SmtpHelper.CreateSmtpClient().Send(message); try { smtpClient.Send(message); divForm.Visible = false; litThanks.Visible = true; } catch (Exception ex) { labelErrors.Visible = true; labelErrors.Text = "Mail Problem: " + Environment.NewLine + ex.Message.ToString(); } } catch (Exception ex) { Response.Write(ex.Message.ToString() + "<br/>"); Response.End(); } } if (labelErrors.Visible) labelErrors.Text += "<br /><br />"; } public bool ValidateForm() { if (!ValidateNotBlank(txtProviderName, "Provider Name", labelErrors)) return false; labelErrors.Visible = false; return true; } protected bool ValidateNotBlank(System.Web.UI.WebControls.TextBox control, string message, System.Web.UI.WebControls.Label labelErrors) { if (control.Text.Trim() == string.Empty) { labelErrors.Visible = true; labelErrors.Text = "The " + message + " field is required, please complete the information."; return false; } return true; } }
Last Updated on October 26, 2015