Wednesday, May 16, 2012

Asp.Net Star Rating User Control

With a little CSS, a tiny amount of jQuery and a little ingenuity you can easily create a reusable Asp.Net star rating system for your site.

The Basics

For the CSS I decided there was no reason to re-invent the wheel, so I used Komodo Media’s CSS Star Rating Part Duex for the styles, although I did rename them (only because I prefer short, simple class names).

Here’s the basic HTML structure of the rating system:

<ul class="rating">
 <li><a class="one" title="1 out of 5">1</a></li>
 <li><a class="two" title="2 out of 5">2</a></li>
 <li><a class="three current" title="3 out of 5">3</a></li>
 <li><a class="four" title="4 out of 5">4</a></li>
 <li><a class="five" title="5 out of 5">5</a></li>
 </ul>

And here are the styles:

 .rating, .rating a:hover, .rating a:focus, .rating .current {
 background: url(/images/star.gif) left -1000px repeat-x;
 }
 .rating {
 position: relative;
 width: 125px;
 height: 25px;
 list-style: none;
 margin: 0;
 padding: 0;
 background-position: left top;
 }
 .rating li {
 display: inline;
 }
 .rating a, .rating .current {
 position: absolute;
 top: 0;
 left: 0;
 text-indent: -1000px;
 height: 25px;
 line-height: 25px;
 outline: none;
 overflow: hidden;
 border: none;
 }
 .rating a:hover, .rating a:active, .rating a:focus {
 background-position: left bottom;
 }
 .rating a.one {
 width: 20%;
 z-index: 6;
 }
 .rating a.two {
 width: 40%;
 z-index: 5;
 }
 .rating a.three {
 width: 60%;
 z-index: 4;
 }
 .rating a.four {
 width: 80%;
 z-index: 3;
 }
 .rating a.five {
 width: 100%;
 z-index: 2;
 }
 .rating .current {
 z-index: 1;
 background-position: left center;
 }
 

And, here’s the star image:

If you add what we have here to a page and view it in the browser, you’ll see a nice rating system. The current class represents the selected rating. That will show the lighter star, then hovering over the stars will show the darker star, otherwise the star outline will appear.

This looks quite nice and works great. However, there’s no real practical application to it. Clicking on one of the stars does nothing. Even if it did there is currently no way to send it back to the server, say, to update a rating field stored in a database field.

Continue Reading…

Monday, March 19, 2012

The Healthy Coder: A Shake A Day

Several years ago I lost over 100lbs. Since then staying healthy has been part of my mission. Having a career that puts me in a chair for 8-10 hours a day doesn’t help with that so much–especially when it’s so easy to have a bag of Doritos and a Mountain Dew within arms reach.

One solution I’ve recently discovered is Shakeology.  Shakeology is a meal replacement shake packed with a plethora (yes, a plethora) of healthy stuff. It comes in three flavors: Chocolate, Greenberry and new Tropical Strawberry (my favorite is Tropical Strawberry).

If you’re trying to drop a few pounds or just trying to stay more healthy, you may want to give Shakeology a try. If you order it as Home Direct, a new shipment will arrive every 30 days and shipping is free.

Friday, March 16, 2012

Delete Button Field for GridView with Confirmation

One of the features I find lacking when working with GridViews is when adding a delete button you are not prompted with a confirmation. In my early years I used to add a TemplateField with a Button (or LinkButton) to handle the deletion. I would add a OnClientClick event that would show a JavaScript confirmation box confirming that the row is to be completed.

This worked fine but took several extra steps. I found that when I was working with a lot of GridViews, this really added a lot of extra work. So I created a new Delete Button Control that automatically adds a confirmation message to any GridView. Continue Reading…

Tuesday, March 13, 2012

Staying Productive: Using App Themes for Global Control Properties

In my last post, Staying Productive: Code Snippets are Your Friend, I showed how I use Code Snippets in Visual Studio to drastically slash the time it takes to build Asp.Net forms. In this article I’ll show how I use App Themes within web applications to set some global properties for Asp.Net controls.

You may have noticed in the last article that the Code Snippets leave out some properties for certain controls that usually are set. For instance, the validation controls only use the common properties to make the validator work, such as the ID, ControlToValidate and ErrorMessage. Where is the CssClass, Display, or Text properties?

I like to cut as much of the clutter from my forms as possible. I prefer to only show properties that make controls function in my Asp.Net forms. All of the validation control I use always has the same values set for the Display, CssClass and Text. Display is always set to “Dynamic,” CssClass is always set to “validator” and Text is always set to “&larr;” (I think the “*” is boring). Every time I drop a validator on the page I don’t want to re-type those same values every time. I could put them in the Code Snippet, but again, I don’t like to add a bunch of extra stuff in my form.  This is why use App Themes. Continue Reading…

Wednesday, March 7, 2012

Staying Productive: Code Snippets are Your Friend

One of the aspects of my job that I hate the most is building long, mundane Asp.Net forms. I get so tired of dropping labels, textboxes, validators on a page and then formatting them over and over again. Every project I work on I find myself building the exact same form fields: First Name, Last Name, Email Address, Address, City, State, blah, blah, blah. It drives me nuts.

You’re probably thinking, why don’t you just copy and paste? That’s a valid question, but copying and pasting form fields, to me, is almost as bad as just building the thing from scratch. I never can remember the exact file name of an existing file I need to open or even where the project is stored. It’s a real hassle for me.

Enter code snippets.

Code snippets in Visual Studio can be a great tool for making long forms a lot less painful. Code snippets are basically XML files that you can assign shortcuts to that inserts a block of code at a certain place in Visual Studio. There is another kind of snippet that will wrap around a selection…but only works in C#, not VB or HTML…which drives me crazy, so don’t get me started!

Code snippets are stored (generally) in a Visual Studio folder in your Documents folder (the path for mine is Documents\Visual Studio 1010\Code Snippets). Inside the Code Snippets folder there are folders for each types of snippets. HTML snippets go in Visual Web Developer (so C# snippets go in the Visual C# folder and so forth). I created a folder called “My HTML Snippets” to store all my snippets (how many times can I possibly use the word snippet in a post? I guess we’ll see.) Continue Reading…

Wednesday, February 29, 2012

Online Tool to Escape HTML

Here’e a great resource that can serve as a real lifesaver. If you have a lot of HTML you need to store in an XML document, you of course could use CDATA, but for those time you can’t there’s (un)Escape HTML for use in XML. Simply paste your HTML markup in the Unescaped HTML box, click the escape button (it only escapes characters, if you need an escape…that’s a different site altogether) and wa-la, your HTML is ready to be used in XML.

Here’s a bonus, you can reverse it. Paste escaped HTML and it can unescape it for you.

(un)Escape HTML for use in XML

Give it a try: http://escapehtmlforxml.com/

Thursday, December 22, 2011

JavaScript Christmas Countdown Timer

View the Demo

JavaScript timers and clocks have been around for a while. There’s nothing new with this one. I just thought it would be fun to do a countdown to Christmas. So before Christmas a display will show how many days, hours, minutes and seconds until 12/25 of the current year. On Dec. 25th a “Merry Christmas” message will appear. On Dec. 26th the countdown will begin to next Christmas.

Just to throw in some extra interestingness, I’m using a couple of the Google Web Fonts to spice things up a little, specifically I’m using “Qwigley.”

Here’s the HTML, pretty simple really.

<!doctype html>
<html>
<head></head>
<body>
<div class="countdown"></div>
</body>
</html>

Here’s the CSS that goes in the <head>.

<link href='http://fonts.googleapis.com/css?family=Qwigley'
 rel='stylesheet' type='text/css'>
<style type="text/css">
 .countdown
 {
 color: #009933;
 font: normal bold 44px 'Qwigley';
 }
 .merryxmas
 {
 color: #990000;
 font: normal bold 72px 'Qwigley';
 }
 </style>

Now here’s the JavaScript. This can go in either the <head> or just before the closing <body> tag. Make sure to include the jQuery library if you’re using $(document).ready(function() {}); like I am.

<script type="text/javascript">
 var currentYear = new Date().getFullYear();
 var now = new Date();
 var xmas = new Date('12/25/' + currentYear + ' 00:00:00');
 var countdown = xmas - now;
$(document).ready(function () {
 doCountdown();
 doTimer();
 console.log(newyears - now);
 });
var t;
 function doTimer() {
 doCountdown();
 t = setTimeout('doTimer()', 1000);
 }
function doCountdown() {
 now = new Date();
 countdown = xmas - now;
var ms = countdown / 1000;
var sec = Math.floor(ms % 60)
 ms /= 60;
var min = Math.floor(ms % 60)
 ms /= 60;
var hr = Math.floor(ms % 24)
 ms /= 24;
 var day = Math.floor(ms);
var div = $('div.countdown');
 if (countdown > 0) { // Not yet Christmas
 div.html(day + ' days, ' + hr + ' hours, ' + min + ' minutes and ' + sec + ' seconds');
 div.removeClass('merryxmas');
 } else if (countdown > -86400000) { // Christmas! Stop showing at 12:00:00 12/16
 div.html('Merry Christmas!');
 div.addClass('merryxmas');
 } else { // Christmas has passed. Update xmas date to next year
 xmas = new Date('12/25/' + (currentYear + 1) + ' 00:00:00');
 }
}
</script>

So, to start with I’m getting the current year and setting it to a variable. Then I set a variable to the current datetime (new Date() without any parameters gets the current datetime). Next, since in the US Christmas is on Dec. 25th every year I’m creating another datetime variable to hold Christmas day for the current year. I then create a countdown variable which will hold the milliseconds between now and Christmas day (at midnight).

When the document is loaded I call doCountdown() to set the text when the page loads. This really isn’t necessary but I don’t like the little flicker on the page if you don’t do it. Next I call the doTimer() function which sets an infinite loop, continuously calling the doCountdown() function every 1000 milliseconds (every second).

The doCountdown() function calculates the days, minutes, hours and seconds until Christmas. If it is before Christmas a message with the days, hours, minutes,  and seconds. There are 8640000 milliseconds in a day so it is Christmas from when the countdown hits 0 to when it hits -86400000 (countdown > -86400000). So when it is Christmas a “Merry Christmas” message will be displayed.

If neither of these conditions are true (Christmas hasn’t yet come or it is Christmas day) then, alas, we’ve missed Christmas and look forward to next year, so we set the xmas variable to Dec. 25th of next year.

One thing that takes a little time is understanding how to calculate how many days, hours, minutes, seconds there are in a certain number of milliseconds.

My best advice, though,  is to just copy what I’ve done. It works.

Thursday, December 15, 2011

ASP.Net Refresh Data on Parent Page from Lightbox without Postback

Something that has always bothered me about using “light boxes” or even popup windows and data is added or updated in that light box the parent window that is displaying the data doesn’t refresh the data without refreshing the page. Using AJAX to improve the user experience  makes matters worse because the data should automatically refresh when changed. This tutorial will show how to update a GridView on a parent page when a light box updates the data. I will be using Fancybox but the same functionality can be added to any kind of light box.

 The Data Page

For simplicity I’ll be using trusty old AdventureWorks. To cut down on the fields my GridView will display, I’m going to create class called LightContact. Here’s the class:

public class LightContact
{ public int id { get; set; }
 public string Title { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string EmailAddress { get; set; }
 public string Phone { get; set; }
}

I totally stole this idea from a recent Silverlight video I was watching. I would give the author credit but I completely forgot which one it was. Sorry! Basically the point of this is to simply cut down on the amount of data that will display in the GridView.

I created an Entity Data Model that defines the Contact table from AdventureWorks. The data method or model you choose really doesn’t matter. I really like the Entity Framework so that’s what I’m sticking with.

Continue Reading…

Thursday, December 1, 2011

Monday, November 28, 2011

Cyber Monday & Standard Theme 3

For all of the WordPress blogs I set up I recommend building the site around 8Bit’s Standard Theme. It’s fast, looks great, and features several built-in social media tools to help get the most out of your blog. Today (Cyber Monday) The creators of Standard Theme are offering 30% off of all their premium themes. With the discount you can purchase a Support License that includes all future upgrades (including version 3 coming out soon) and access to the very helpful support forums for only $69.

Page 1 of 212»