Rating System: jQuery & C#.NET - No Value Passed Through

Hi Everyone,

This could be something simple I am missing, so my apologies if it is, this is new terrain for me.

Just to rule it out, I have tried using the AJAX Rating system to no success, so i’ve decided to develop my own (as useful as AJAX seems to be for some reason I’ve never got it working on my system).

I’m using a Star Rating Widget I attained from http://orkans-tmp.22web.net/star_rating/index.html#demos=&main-menu=0&demo-tabs=3 …and from there I’ve taken a demo (so that it is working) and started editing it accordingly to my needs.

What I am setting out to do convert the radio buttons to stars (done via jQuery and working correctly). The user selects their rating then clicks the the Submit Button. The C# in the Submit OnClick Event then accesses the database, using LINQ to SQL, taking the current ‘NumberOfVotes’ value and adds one to the number, storing in an int object ready to update. The current ‘Rating’ Value is then updated too, by adding the Value of the Radio button to the current Rate Value then divided by the number of votes (to create the average rating)

e.g. ‘This article has been rated 4.28/5 by 286 Voters.’

However, the value from the selected radio button isnt being passed through to the back end, as debug has shown, and as you will see in the code below. The jQuery does state a callback which i had initially commented out (due to errors being caused). I have since reassigned it as advised, with a small edit in it to call the btnSubmit control and thus its onclick Event. Im not sure on a resolution to this having tried the only advised suggestion fail. The result of which posts back the message which should appear if no value/star was selected (“You have not selected a rating!”).

If I can somehow transfer the jQuery to the ASp:RadioList I would do so, as I think this would be better in terms of calling and processing the data. Being new to it, I have no idea. In addition, apologies if my coding appears scrappy or poor - it probably is lol

C#:


protected void btnSubmit_Click(object sender, EventArgs e)
    {
        RatingSys rate = RatingSys.Get(1);

        int RateID = Convert.ToInt32(hdnRateID.Value);
        int CurrentRating = rate.Rating.Value;
        int RadioValue = 0;
        int NewRating = 0;
        int NumberOfVotes = 0;

        if (!radio1.Checked || !radio2.Checked || !radio3.Checked || !radio4.Checked || !radio5.Checked)
        {
            lblMessage.Text = "You have not selected a rating!";

            return;
        }
        else if (radio1.Checked)
        {
            //Assign Int to Radio's Value
            RadioValue = Convert.ToInt32(radio1.Value);

            //Calculating Average Rate and Number of Votes
            NumberOfVotes = rate.NumberOfVotes.Value + 1;
            NewRating = CurrentRating + RadioValue / NumberOfVotes;

            //Store New Values
            rate.Rating = NewRating;
            rate.NumberOfVotes = NumberOfVotes;

            rate.Update(RateID);

            lblMessage.Text = "You have selected Very Poor!" ;
        }
        else if (radio2.Checked)
        {
            //Assign Int to Radio's Value
            RadioValue = Convert.ToInt32(radio2.Value);

            //Calculating Average Rate and Number of Votes
            NumberOfVotes = rate.NumberOfVotes.Value + 1;
            NewRating = CurrentRating + RadioValue / NumberOfVotes;

            //Store New Values
            rate.Rating = NewRating;
            rate.NumberOfVotes = NumberOfVotes;

            rate.Update(RateID);

            lblMessage.Text = "You have selected Not Bad!";
        }
        else if (radio3.Checked)
        {
            //Assign Int to Radio's Value
            RadioValue = Convert.ToInt32(radio3.Value);

            //Calculating Average Rate and Number of Votes
            NumberOfVotes = rate.NumberOfVotes.Value + 1;
            NewRating = CurrentRating + RadioValue / NumberOfVotes;

            //Store New Values
            rate.Rating = NewRating;
            rate.NumberOfVotes = NumberOfVotes;

            rate.Update(RateID);

            lblMessage.Text = "You have selected Average!";
        }
        else if (radio4.Checked)
        {
            //Assign Int to Radio's Value
            RadioValue = Convert.ToInt32(radio4.Value);

            //Calculating Average Rate and Number of Votes
            NumberOfVotes = rate.NumberOfVotes.Value + 1;
            NewRating = CurrentRating + RadioValue / NumberOfVotes;

            //Store New Values
            rate.Rating = NewRating;
            rate.NumberOfVotes = NumberOfVotes;

            rate.Update(RateID);

            lblMessage.Text = "You have selected Good!";
        }
        else if (radio5.Checked)
        {
            //Assign Int to Radio's Value
            RadioValue = Convert.ToInt32(radio5.Value);

            //Calculating Average Rate and Number of Votes
            NumberOfVotes = rate.NumberOfVotes.Value + 1;
            NewRating = CurrentRating + RadioValue / NumberOfVotes;

            //Store New Values
            rate.Rating = NewRating;
            rate.NumberOfVotes = NumberOfVotes;

            rate.Update(RateID);

            lblMessage.Text = "You have selected Excellent!";
        }
    }

ASPX:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Star Rating Widget</title>
	
	<!-- Star Rating widget stuff here... -->
	<script type="text/javascript" src="js/jquery.ui.stars.js?v=3.0.0b38"></script>
	<link rel="stylesheet" type="text/css" href="css/crystal-stars.css?v=2.0.3b38"/>

	<!-- demo page css -->
	<link rel="stylesheet" type="text/css" media="screen" href="css/demos.css?b38"/>

	<!-- demo page js -->
	<script type="text/javascript" src="js/jquery.min.js?v=1.4.2"></script>
	<script type="text/javascript" src="js/jquery-ui.custom.min.js?v=1.8"></script>

	<!-- Star Rating widget stuff here... -->
	<script type="text/javascript" src="js/jquery.ui.stars.js?v=3.0.0b38"></script>
	<link rel="stylesheet" type="text/css" href="css/jquery.ui.stars.css?v=3.0.0b38"/>
	<script type="text/javascript">
	    $(document).ready(function () {
        $("#ratings").stars({
            callback: function (ui, type, value) {
                __doPostBack("btnSubmit", "");
            }
        });
    });   
//		$(function(){
//			//$("#ratings").children().not(":radio").hide();
//			$("#ratings").stars({
//				callback: function(ui, type, value)
//				{
//					$.post("demo1.php", {rate: value}, function(data)
//					{
//						$("#ajax_response").html(data);
//					});
//				}
//			});
//		});
	</script>


</head>
<body>
    <div id="messages"/>
    
    <form id="ratings" runat="server">
	Rating: <span id="stars-cap"></span>

		<input id="radio1" runat="server" type="radio" name="newrate" value="1" title="Very poor" />
		<input id="radio2" runat="server" type="radio" name="newrate" value="2" title="Not Bad" />
		<input id="radio3" runat="server" type="radio" name="newrate" value="3" title="Average" />
		<input id="radio4" runat="server" type="radio" name="newrate" value="4" title="Good" />
		<input id="radio5" runat="server" type="radio" name="newrate" value="5" title="Excellent" />

	    <br /><br />
    <div id="caption"/>
        
	<asp:Label ID="lblMessage" runat="server" />
	<asp:Button ID="btnSubmit" runat="server" Text="Rate!" OnClick="btnSubmit_Click" UseSubmitBehavior="false" />
	    
    <asp:HiddenField ID="hdnRateID" runat="server" Value="1" />

    </form>
    
    <p id="ajax_response"></p>
    
</body>
</html>

Any help is very much appreciated!

Many Thanks,

Daniel

This has now since been resolved.

It was the start of my ‘if’ statement that was causing problems.

With the addition of me working out my average wrong. I over-looked dividing the total and was dividing the average by accident :blush:

Won’t be hard to fix :slight_smile:

Regards,

Daniel