AJAX Control toolkit provides sophisticated RATING control using which you can have rating facility in your website. Its very simple to use but there is a problem you could come across while creating it dynamically. The problem is in the ViewState, which means you can’t get the selected rating value of dynamic Rating control (from codeĀ behind). So for solving this problem i have taken the following steps –
1. I have created a Hidden field along with creating Rating control (i.e one HiddenField for each Rating control).
2. Through Javascript kept the selected rating value in the corresponding Hidden field.
So the code looks like this :
for (int dynamicControlCount = 0; dynamicControlCount < 5; dynamicControlCount++) { // Rating Control for taking user's rating. AjaxControlToolkit.Rating rating = new AjaxControlToolkit.Rating(); rating.ID = "rating" + dynamicControlCount; rating.BehaviorID = "ratingBehaviour" + dynamicControlCount; rating.StarCssClass = "rating"; rating.WaitingStarCssClass = "waiting"; rating.FilledStarCssClass = "filled"; rating.EmptyStarCssClass = "empty"; // HiddenField to store selected rating. HiddenField hfRating = new HiddenField(); hfRating.ID = "hfRating" + dynamicControlCount; hfRating.Value = "0"; // Add the Rating control and HiddenField to a Container(PlaceHolder/Panel). // CODE // Attaching Javascript function to store selected rating in a HiddenField on clicking the Rating control. rating.Attributes.Add("onClick", "storeRating('" + rating.BehaviorID + "', '" + hfRating.ClientID + "');"); } // Javascript function to store selected rating in a HiddenField. function storeRating(ratingBehaviourID, hiddenControlID) { // Collect selected rating. var currentRating = $find(ratingBehaviourID).get_Rating(); // Store selected rating value in a HiddenField. document.getElementById(hiddenControlID).value = currentRating; return false; }
In the above Javascript function i am getting the selected rating by using the Behaviour ID of the Rating control & storing the selected rating in the HiddenField.
Now since this piece of Javascript code will place the selected Rating value in its associated HiddenField, you can get it from Code behind.
So, if you want to get the selected Rating value (from code behind) then you can collect it from the Hidden field.