Get Current Rating of AJAXControlToolkit Rating Control from Client Side JavaScript
Read my previous article that discusses how to customize tooltip of rating control Customizing the Tooltip of Rating Control using jQuery
This little code snippet will help us to get the current rating of AJAX control toolkit's Rating control using JavaScript.
We can get if the rating control is read-only by calling a function called get_Rating() of RatingBehaviour of Rating control. The Rating control is associated with a rating behaviour by default. If you see the HTML source generated by the page that has a rating control, we can see this behaviour defined by the Rating control somewhere near the bottom. Something similar to,
Sys.Application.add_init(function() {
$create(AjaxControlToolkit.RatingBehavior, {"AutoPostBack":true,"CallbackID":"RatingCtrl", "ClientStateFieldID":"RatingCtrl_RatingExtender_ClientState", "EmptyStarCssClass":"emptyRatingStar","FilledStarCssClass":"filledRatingStar"
,"Rating":4,"ReadOnly":true,"StarCssClass":"ratingStar",
"WaitingStarCssClass":"savedRatingStar", "id":"RatingCtrl_RatingExtender"}, null, null, $get("RatingCtrl"));
});
You can get the ID of the behaviour here. If the ID of your Rating control is "RatingCtrl" then the id of the behaviour object will be Rating control ID concatenated by "_RatingExtender"( RatingCtrl_ RatingExtender, in our example.). You can also set this property manually by setting BehaviourID property of the Rating control to have whatever name you like.
Get current rating by,
function pageLoad()
{
$find("RatingCtrl_RatingExtender").get_Rating();
}
The above JavaScript code will make the AJAX Rating control to Read-Only from the client side PageLoad event of PageRequestManager class.
|