Creating the Testimonials add and edit view

The Testimonials add and edit views are presented with single view. To control the state that the view is rendered to, you change the mode of the view.

To create the Testimonials add and edit views, perform the following:

  1. In the context menu of the Admin 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 TestimonialsAddEditView.

  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="TestimonialsAddEditView.ascx.cs" Inherits="SitefinityWebApp.Modules.Testimonials.Admin.TestimonialsAddEditView" %>
    <%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.Fields" TagPrefix="sf" %>
     
    <h1 class="sfBreadCrumb">Testimonials</h1>
     
    <div class="sfMain sfClearfix">
        <div class="sfContent">
            <div class="rgTopOffset sfWorkArea">
                <fieldset class="sfNewContentForm">
                    <div class="sfFormIn">
                        <ul>
                            <li>
                                <asp:Label ID="Label1" runat="server" AssociatedControlID="Name" Text="Name" CssClass="sfTxtLbl" />
                                <asp:TextBox ID="Name" runat="server" CssClass="sfTxt" />
                            </li>
                            <li>
                                <asp:Label ID="Label2" runat="server" AssociatedControlID="Summary" Text="Summary" CssClass="sfTxtLbl" />
                                <asp:TextBox ID="Summary" runat="server" CssClass="sfTxt" />
                            </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" />
     
                            </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>
                                <asp:Label ID="Label5" runat="server" AssociatedControlID="Published" Text="Published" CssClass="sfTxtLbl" />
                                <asp:CheckBox ID="Published" runat="server" />
                            </li>
                        </ul>
     
                        <p><asp:Button ID="btnSave" runat="server" Text="Save Testimonial" onclick="btnSave_Click" /> or <a href="<%= ResolveUrl("~/SitefinityContent/Testimonials") %>">Cancel and go back to Testimonials</a></p>
                    </div>
                </fieldset>
            </div>
        </div>
    </div>

  6. Open the code-behind file TestimonialsAddEditView.ascx.cs in Visual Studio and add the following namespaces:

    using System.Text.RegularExpressions;
    using SitefinityWebApp.Modules.Testimonials.Data;
    using Telerik.Sitefinity.Web;

  7. Provide instance for the TestimonialsContext by pasting the following code:

    private const string UrlNameCharsToReplace = @"[^\w\-\!\$\'\(\)\=\@\d_]+";
    private const string UrlNameReplaceString = "-";
    TestimonialsContext context = TestimonialsContext.Get();

  8. Create the fields that control the mode of the view by pasting the following code:

    public enum AdminControlMode
    {
        Create,
        Edit
    }
     
    public AdminControlMode Mode { get { return _mode; } set { _mode = value; } }
    private AdminControlMode _mode;
     
    private Guid testimonialID = Guid.Empty;
     
    protected Guid TestimonialID
    {
        get
        {
            if (testimonialID == Guid.Empty)
            {
                var param = Request.RequestContext.RouteData.Values["Params"] as string[];
                if (param == null) return Guid.Empty;
     
                Guid id;
                if (!Guid.TryParse(param[0], out id)) return Guid.Empty;
     
                var testimonial = context.Testimonials.FirstOrDefault(t => t.Id == id);
                testimonialID = (testimonial == null) ? Guid.Empty : testimonial.Id;
            }
            return testimonialID;
        }
    }

  9. Implement the Page_Load method by pasting the following code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack || Mode != AdminControlMode.Edit) return;
     
        var testimonial = context.Testimonials.Where(t => t.Id == TestimonialID).FirstOrDefault();
        if (testimonial == null) return;
     
        Name.Text = testimonial.Name;
        Summary.Text = testimonial.Summary;
        Text.Value = testimonial.Text;
        Rating.Value = testimonial.Rating;
        Published.Checked = testimonial.Published;
    }

  10. Handle the Click event of the btnSave control in the following way:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        switch (Mode)
        {
            case AdminControlMode.Edit:
     
                // update existing testimonial
                var testimonial = context.Testimonials.Where(t => t.Id == TestimonialID).FirstOrDefault();
                if (testimonial == null) return; // default 404 response
     
                // mark route handled/found
                RouteHelper.SetUrlParametersResolved();
     
                testimonial.Name = Name.Text;
                testimonial.UrlName = Regex.Replace(Name.Text.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
                testimonial.Summary = Summary.Text;
                testimonial.Text = Text.Value.ToString();
                testimonial.Rating = Rating.Value;
                testimonial.Published = Published.Checked;
                break;
     
            case AdminControlMode.Create:
                // create and save new testimonial
                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 = Published.Checked;
                context.Add(newTestimonial);
                break;
        }
     
        // save and return to main view
        context.SaveChanges();
        Response.Redirect(ResolveUrl(SiteMapBase.GetActualCurrentNode().ParentNode.Url));
    }

  11. Override the OnUnload method:

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        if (context != null)
            context.Dispose();
    }

In the markup, first, you register the necessary assemblies, so that they can be used in the template. Then, you create the fields to visualize all testimonial details. In TestimonialsAddEditView.ascx.cs, you control the mode of the view by defining the Mode field. Then, you handle the Load event of the Page control and retrieve the testimonial details. Finally, you handle the Click event of the btnSave control by updating the existing testimonial or creating a new one depending on the mode.

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