Creating the Testimonials admin view

To create the Testimonials admin view, 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 TestimonialsAdminView.

  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="TestimonialsAdminView.ascx.cs" Inherits="SitefinityWebApp.Modules.Testimonials.Admin.TestimonialsAdminView" %>
     
    <h1 class="sfBreadCrumb">Testimonials</h1>
     
    <div class="sfMain sfClearfix">
        <div class="sfMain sfClearfix">
            <div class="sfAllToolsWrapper">
            <div class="sfAllTools">
            <div class="sfActions">
                <ul>
                    <li class="sfMainAction">
                        <span><a class="sfLinkBtn sfNew" href="<%= ResolveUrl(Telerik.Sitefinity.Web.SiteMapBase.GetActualCurrentNode().Url) %>/Create"><span class="sfLinkBtnIn">Create a Testimonial</span></a></span>
                    </li>
                </ul>
            </div>
            </div>
            </div>
     
            <div class="sfWorkArea sfClearfix">
            <div class="rgTopOffset">
            <telerik:RadGrid ID="TestimonialsGrid" runat="server" Skin="Sitefinity" ondeletecommand="TestimonialsGrid_DeleteCommand">
                <MasterTableView DataKeyNames="ID" AutoGenerateColumns="false">
                <NoRecordsTemplate>
                    <div class="sfEmptyList" style="margin-top: 10px;">
                        <span class="sfMessage sfMsgNeutral sfMsgVisible" style="background-color: #ffffcc; display: inline">No Testimonials have been created yet</span>
                        <ol class="sfWhatsNext">
                            <li class="sfCreateItem"><a href="<%= ResolveUrl(Telerik.Sitefinity.Web.SiteMapBase.GetActualCurrentNode().Url) %>/Create">Create
                                a Testimonial <span class="sfDecisionIcon"></span></a></li>
                        </ol>
                    </div>
                </NoRecordsTemplate>
                    <Columns>
                        <telerik:GridHyperLinkColumn UniqueName="ID" DataNavigateUrlFields="Id" Text="Edit" />
                        <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Delete" Text="Delete" ConfirmDialogType="Classic" ConfirmText="Are you sure you want to delete this item?" />
                        <telerik:GridBoundColumn DataField="Name" HeaderText="Author" />
                        <telerik:GridBoundColumn DataField="Summary" HeaderText="Summary" />
                        <telerik:GridRatingColumn DataField="Rating" HeaderText="Rating" ReadOnly="true" />
                        <telerik:GridDateTimeColumn DataField="DatePosted" HeaderText="Date Posted" DataFormatString="{0:ddd MMM dd, yyyy h:mm tt}" />
                        <telerik:GridCheckBoxColumn DataField="Published" HeaderText="Published" />
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
            </div>
            </div>
        </div>
    </div>

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

    using SitefinityWebApp.Modules.Testimonials.Data;
    using Telerik.Sitefinity.Web;
    using Telerik.Web.UI;

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

    TestimonialsContext context = TestimonialsContext.Get();

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

    protected void Page_Load(object sender, EventArgs e)
    {
        var linkColumn = TestimonialsGrid.MasterTableView.Columns.FindByUniqueName("ID") as GridHyperLinkColumn;
        linkColumn.DataNavigateUrlFormatString = string.Concat(ResolveUrl(SiteMapBase.GetActualCurrentNode().Url), "/Edit/{0}");
     
        TestimonialsGrid.DataSource = context.Testimonials;
        TestimonialsGrid.DataBind();
    }

  9. Handle the DeleteCommand event of the TestimonialsGrid control in the following way:

    protected void TestimonialsGrid_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        if (e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"] == null) return;
     
        Guid id;
        if (!Guid.TryParse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString(), out id)) return;
     
        var item = context.Testimonials.FirstOrDefault(t => t.Id == id);
        if (item == null) return;
     
        context.Delete(item);
        context.SaveChanges();
     
        TestimonialsGrid.DataSource = context.Testimonials;
        TestimonialsGrid.DataBind();
    }

  10. Override the OnUnload method:

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

In the markup, you create a RadGrid to visualize all testimonials. In TestimonialsAdminView.ascx.cs, you handle the Load event of the Page control and retrieve all testimonials. Then, you handle the DeleteCommand event of the TestimonialsGrid control by retrieving and deleting the 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