by
Nicole
Thursday, December 02, 2010 8:02 PM
I'm currently working on a redesign for a client in the health industry. They wanted a random testimonial to appear in the master page template on each page load. I started out by creating a user control called Testimonial and registering it in the Web.Config like so:
-
<pages>
-
<controls>
-
<add tagPrefix="ajx" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>
-
<add tagPrefix="asp" tagName="MessageBox" src="~/usercontrols/MessageBox.ascx"/>
-
<add tagPrefix="asp" tagName="PhoneBox" src="~/usercontrols/PhoneBox.ascx"/>
-
<add tagPrefix="asp" tagName="Testimonial" src="~/usercontrols/Testimonial.ascx"/>
-
<add tagPrefix="asp" tagName="PageContent" src="~/usercontrols/PageContent.ascx"/>
-
<add tagPrefix="asp" assembly="FreeTextBox" namespace="FreeTextBoxControls"/>
-
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
-
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
-
</controls>
-
</pages>
The UI of the Testimonial user control is very simple. The template design had a area allotted for random testimonials in the shape of a rectangle located under the left hand navigation menu, so I used that to create the control UI design. The code comprises a basic table with the testimonial box images as a header, background, and footer, as well as an ASP:Label control. Here's a code snippet of the ASPX page:
-
<table cellpadding="0" cellspacing="0" style="background-color: #004e7c; width:191px;">
-
<tr>
-
<td rowspan="3" style="vertical-align: top; width: 31px;">
-
<img src="../images/testimonial_text.jpg" alt="My Client's Name" />
-
</>td>
-
<td style="vertical-align: top; width: 160px;">
-
<img src="../images/testimonial_top.jpg" alt="My Client's Name" />
-
</td>
-
</tr>
-
<tr>
-
<td style="background: url('../images/testimonial_bg.jpg'); background-repeat: repeat-y;
-
height: 150px; vertical-align: top; padding: 0px 11px 0px 16px;">
-
<asp:Label runat="server" ID="lblTestimonialText" Font-Size="11px" />
-
</td>
-
</tr>
-
<tr>
-
<>td style="vertical-align: top;">
-
<img src="../images/testimonial_bottom.jpg" alt="My Client's Name" />
-
</td>
-
</tr>
-
</table>
-
The control code renders like this when the user control tag is placed in the master page, which fits in very nicely with the rest of the website design/template:

What's left to do is the get the random testimonial and apply it to the control. This website contains an administration panel where the client can login and perform CRUD actions on testimonials. We need a SQL query that will get a random testimonial from the Testimonials database table. This website has an entire Testimonial class to perform the select/insert/update/delete operations for the admin-only Maintain Testimonials screen, so I created a static method there that returns the testimonial text string. The key to the query is selecting the TOP 1 testimonial recording, by ordering the rows by "ORDER BY NEWID()", which randomizes the TOP 1 record returned. The method fills a data table with the query result (you could also use a data reader here), and then returns the testimonial text and name. If an error occurs, I'm catching in and returning a friendly message saying that testimonials are unavailable after the exception detail is sent to me to fix using my EmailError class.
-
public static string GetRandomTestimonial()
-
{
-
comm = new SqlCommand();
-
-
try
-
{
-
comm.CommandText = "SELECT TOP 1 Testimonial_Text, "
-
+ "Testimonial_Name "
-
+ "FROM Testimonials "
-
+ "ORDER BY NEWID()";
-
-
dt = DBCommands.GetDataTableCommand(comm);
-
-
if (dt != null)
-
{
-
strResult = dt.Rows[0]["Testimonial_Text"].ToString() + "<br /><div style=\"padding-left:12px; padding-top:4px;\"> - "
-
+ dt.Rows[0]["Testimonial_Name"].ToString() + "</div>";
-
}
-
else
-
{
-
strResult = "There are no testimonials in the system.";
-
}
-
}
-
catch (Exception ex)
-
{
-
EmailError.EmailHandledError(ex);
-
return "Testimonials are currently not available.";
-
}
-
finally
-
{
-
comm.Dispose();
-
}
-
-
return strResult;
-
}
And lastly, in the Testimonial.asxc.cs (Testimonial user control code-behind), simply call the static method to get the random testimonial and apply the returned string to the lblTestimonialText text attribute on the Page_Load method, like so:
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
lblTestimonialText.Text = Testimonial.GetRandomTestimonial();
-
}
Putting it all together produces this, when a testimonial is in the system. Hopefully it'll be a nicer testimonial than mine!

And this is what displays when there are no testimonials in the table. Likewise, when an error is handled while getting the random testimonial, the message will say that testimonials are currently unavailable. Happy coding!
