Creating the Submit testimonial view

To create the Submit testimonial view, perform the following:

  1. In the context menu of the Testimonials folder, click Add » New Item...

  2. In the left pane, select Visual C# » Web.

  3. Click Web User Control and in the Name input field, enter SubmitTestimonial.

  4. Open the newly created file and clear its content.

  5. Define the markup by pasting the following code:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SubmitTestimonial.ascx.cs" Inherits="SitefinityWebApp.Modules.Testimonials.SubmitTestimonial" %>
    <%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.Fields" TagPrefix="sf" %>
     
    <h1>Submit a testimonial</h1>
    <p>Share your thoughts below. Your testimonial will be posted after being reviewed by an administrator.</p>
     
    <asp:Literal ID="Status" runat="server" />
     
    <ul class="submit">
        <li>
            <asp:Label ID="Label1" runat="server"AssociatedControlID="Name" Text="Name" CssClass="sfTxtLbl" />
            <asp:TextBox ID="Name" runat="server" CssClass="sfTxt" />
            <asp:RequiredFieldValidator runat="server"ControlToValidate="Name" Text="Required" />
        </li>
        <li>
            <asp:Label ID="Label2" runat="server"AssociatedControlID="Summary" Text="Summary" CssClass="sfTxtLbl" />
            <asp:TextBox ID="Summary" runat="server" CssClass="sfTxt"/>
            <asp:RequiredFieldValidator runat="server"ControlToValidate="Summary" Text="Required" />
        </li>
        <li>
            <asp:Label ID="Label3" runat="server"AssociatedControlID="Text" Text="Testimonial" CssClass="sfTxtLbl" />
            <sf:formmanager id="formManager" runat="server" />
            <sf:htmlfield id="Text" runat="server" width="99%"height="370px" displaymode="Write" fixcursorissue="True"CssClass="testimonial_text" />
            <asp:RequiredFieldValidator runat="server"ControlToValidate="Text" Text="Required" />
        </li>
        <li>
            <asp:Label ID="Label4" runat="server"AssociatedControlID="Rating" Text="Rating" CssClass="sfTxtLbl" />
            <telerik:RadRating ID="Rating" runat="server"Precision="Item" ItemCount="5" />
        </li>
        <li>
            <p><asp:Button ID="btnSave" runat="server" Text="Save Testimonial" OnClick="btnSave_Click" /></p>
        </li>
    </ul>
    <p><a href="<%= ResolveUrl("~/") %>">« Back to Testimonials</a></p>
  6. Open the code-behind file SubmitTestimonial.ascx.cs in Visual Studio and add the following namespaces:

    using System.Text.RegularExpressions;
    using SitefinityWebApp.Modules.Testimonials.ControlDesigners;
    using SitefinityWebApp.Modules.Testimonials.Data;
    using Telerik.Sitefinity.Web.UI.ControlDesign;
  7. Add the following attribute to the class definition:

    [ControlDesigner(typeof(SubmitTestimonialDesigner)), PropertyEditorTitle("Submit Testimonial")]
  8. Provide property to control whether to auto publish the testimonial by pasting the following code:

    private bool autoPublish = false;
     
    public bool AutoPublish
    {
        get return autoPublish; }
        set { autoPublish = value; }
    }
  9. Handle the Click event of the btnSave control in the following way:

    private const string UrlNameCharsToReplace = @"[^\w\-\!\$\'\(\)\=\@\d_]+";
    private const string UrlNameReplaceString = "-";
     
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            // create and save unpublished testimonial
            var context = TestimonialsContext.Get();
            var newTestimonial = new Testimonial();
            newTestimonial.Name = Name.Text;
            newTestimonial.UrlName = Regex.Replace(Name.Text.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
            newTestimonial.Summary = Summary.Text;
            newTestimonial.Text = Text.Value.ToString();
            newTestimonial.Rating = Rating.Value;
            newTestimonial.Published = AutoPublish;
            context.Add(newTestimonial);
     
            context.SaveChanges();
     
            if (AutoPublish)
                Status.Text = "<p><strong>Your testimonial has been submitted successfully!</p>";
            else
                Status.Text = "<p><strong>Your testimonial has been submitted successfully! Please allow 24 hours for review by the administration.</p>";
     
            // reset form
            Name.Text = string.Empty;
            Summary.Text = string.Empty;
            Text.Value = string.Empty;
            Rating.Value = 0;
        }
        catch (Exception ex)
        {
            Status.Text = "<p><em>An error occurred while submitting your testimonial. Please try again.</p>";
        }
    }

In the markup, first, you register the necessary assemblies, so that they can be used in the template. In SubmitTestimonial.ascx.cs, you set the control designer that the control uses. You do this by the ControlDesigner attribute. You define the control designer in Creating the Submit testimonial view control designer. Then, you handle the Click event of the btnSave control by creating and saving unpublished testimonial.

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK