The AJAX Rating Control from Microsoft's AJAX Control ToolKit for the ASP.Net framework currently in use on this website has been a bit of a disappointment so far. It performed quite well at first because it made adding a graphic user interface for users to rate posts and binding its CurrentRating property to a SQL Insert Statement quite easy, but after that problems arose that proved inconvenient to fix and could have been hard for a less experienced developer to address.
The first problem occurs when trying to display average ratings for which the value is about halfway between two whole numbers (ex: 3.5) because it can only display whole numbers (ex: 3 or 4). This problem could easily be fixed down the road by using a jQuery plugin with images for half stars.
The second problem is that the CurrentRating property only supports integer values. As a result any non whole number such as a decimal (ex: 3.5) or a null value will result in a runtime error. The decimal bug rules out using it to display accurate averages visually and the null values rules out using it for anything in which a post can have no ratings unless fixed by the developer. The best way to fix this that we have found so far is to convert the text values for the average rating label control in each row to a string, convert that string to a decimal, round the decimal to the nearest whole number, convert the result to an integer, and set the CurrentRating property to the integer value using the RowDataBound event of the GridView.
Sanitized Code Used for Integer Conversion:
AjaxControlToolkit.Rating ratingcontrol = (AjaxControlToolkit.Rating)e.Row.FindControl("Rate");
Label rateavglabel = (Label)e.Row.FindControl("RateAVG");
string rateavg = rateavglabel.Text.ToString();
Decimal exactrating = (Decimal)Decimal.Parse(rateavg);
Math.Round(exactrating);
ratingcontrol.CurrentRating = Convert.ToInt32(exactrating);
We would post more code, but for security reasons we don't allow some characters that are necessary to render C# scripts server side in our input forms on this site. At present it is necessary to prevent cross site scripting (XSS) attacks. The use of e.Row.FindControl is being used because it is inside of an "if (e.Row.RowType == DataControlRowType.DataRow)" if statement inside of the GridView RowDataBound event (ex: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)). There is a little bit more to our code than that, but its mostly for customizing labels. All of this was done with the CurrentRating set to 0 by default.
Login to Comment and Rate
Already a PostAlmostAnything.com member?Login HereRegister to Comment and Rate
If you are not yet a PostAlmostAnything.com member Sign Up Here.