当前位置:首页 > 网络编程 > 软件语言 > .NET > AJAX Rating in ASP.NET 2.0

AJAX Rating in ASP.NET 2.0

点击次数:52 次 发布日期:2008-11-06 08:12:27 作者:源代码网
源代码网推荐
广告载入中

源代码网整理以下This article describes an ASP.NET web module along with some chunks of Javascripts and ...Ajax to create a Rating System. This can be added /customized for any table to add a rating/score module. I was quite amazed, when i first saw the capability of rating multiple items at amazon website without refreshing the page. That nice piece of functionality followed me like destiny, till i needed to implement it in a web application.

源代码网整理以下Well ! putting it in a live scenario needs much more than just javascript. I started with a bit of frenzied hacking. To understand how exactly it works, by scanning the javascript from online asynchronous rating websites. That was just the beginning, on the way i learned much more than earlier imagined.  Here is the result of the effort.

源代码网整理以下The target was to create a cross browser rating/score system like amazon or better:

源代码网整理以下Rating: User should be able to Rate a record (1-5)
Accuracy: Number of votes and total score should be accurate and saved and should not be lost in calculations
Real time: Rate/score should be displayed after rating instantly 
Reusable: It should be easily plugged into any table for reusability
Avoiding multiple ratings: Basic mechanism to avoid multiple ratings by the same user
Best approach: Compare the pros and cons for both Amazon and Ajax approach
Security: Few words\ 软件开发网 www.mscto.com

源代码网整理以下
Those were the initial thoughts, but to make it a general reusable and extensible module, I made some assumptions:

源代码网整理以下The table which will have the records to be rated will have a Primary Key ID (integer and identity) and the it will be the first field in the table.
The Table will have atleast two Fields: RatedBy  & Score
For the current example to work you will need atleast one extra field Title
Data access class

源代码网整理以下
There is a standard data access class, clsDataAccess.cs, which handles all the data related actions.

源代码网整理以下Code: I have kept here only the names of the functions, just to give you a glimpse of the data access methods.

源代码网整理以下 

源代码网整理以下 

源代码网整理以下 

源代码网整理以下using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Rating;

源代码网整理以下public partial class Rating_Rated : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

源代码网整理以下
            if ((Convert.ToInt32(Request.QueryString["Rating"]) == 1) || (Convert.ToInt32(Request.QueryString["Rating"]) == 2) || (Convert.ToInt32(Request.QueryString["Rating"]) == 3) || (Convert.ToInt32(Request.QueryString["Rating"]) == 4) || (Convert.ToInt32(Request.QueryString["Rating"]) == 5))
            {
                Label1.Text = "<IMG src="http://www.mscto.com/Asp_Net/images/" + Request.QueryString["rating"] + "star.gif">";
                Label2.Text = "<IMG src="http://www.mscto.com/Asp_Net/images/saved.gif">";
            }
            else
            {
                Response.Redirect("Rate.aspx?id=" + Convert.ToInt32(Request.QueryString["id"]));
            }

源代码网整理以下 

源代码网整理以下            string p = "Select * from TEST_TABLE1 WHERE id = "1"";

软件开发网 www.mscto.com

源代码网整理以下            clsDataAccess myDAR = new clsDataAccess();
            myDAR.openConnection();
            double myRatedBy = Convert.ToInt32(myDAR.getValue(p, Convert.ToInt32(ConfigurationSettings.AppSettings["RatedByField"])));
            double myScore = Convert.ToDouble(myDAR.getValue(p, Convert.ToInt32(ConfigurationSettings.AppSettings["ScoreField"])));

源代码网整理以下
            double myCRating = Convert.ToDouble(Request.QueryString["Rating"]);

源代码网整理以下            double myTotalRating = 0.0;

源代码网整理以下            if ((Convert.ToInt32(Request.QueryString["Rating"]) == 1) || (Convert.ToInt32(Request.QueryString["Rating"]) == 2) || (Convert.ToInt32(Request.QueryString["Rating"]) == 3) || (Convert.ToInt32(Request.QueryString["Rating"]) == 4) || (Convert.ToInt32(Request.QueryString["Rating"]) == 5))
                myTotalRating = (myScore + myCRating) / (myRatedBy + 1);
            else
                myTotalRating = (myScore) / (myRatedBy);

源代码网整理以下            myDAR.closeConnection();

源代码网整理以下            string myTotalRatingString = "";

源代码网整理以下            if ((myTotalRating < 1) && (myTotalRating > 0))
                myTotalRatingString = ".5";
            else if (myTotalRating == 1.0)
                myTotalRatingString = "1";
            else if ((myTotalRating > 1) && (myTotalRating < 2))
                myTotalRatingString = "1.5";
            else if (myTotalRating == 2.0)
                myTotalRatingString = "2";
            else if ((myTotalRating > 2) && (myTotalRating < 3))


                myTotalRatingString = "2.5";
            else if (myTotalRating == 3.0)
                myTotalRatingString = "3";
            else if ((myTotalRating > 3) && (myTotalRating < 4))
                myTotalRatingString = "3.5";
            else if (myTotalRating == 4.0)
                myTotalRatingString = "4";
            else if ((myTotalRating > 4) && (myTotalRating < 5))
                myTotalRatingString = "4.5"; 软件开发网 www.mscto.com
            else if (myTotalRating == 5.0)
                myTotalRatingString = "5";

源代码网整理以下 

源代码网整理以下            Label3.Text = "<IMG src="http://www.mscto.com/Asp_Net/images/stars" + myTotalRatingString + ".gif">";

源代码网整理以下            int RatedBy = 0;
            int TRating = 0;
            if ((Convert.ToInt32(Request.QueryString["Rating"]) == 1) || (Convert.ToInt32(Request.QueryString["Rating"]) == 2) || (Convert.ToInt32(Request.QueryString["Rating"]) == 3) || (Convert.ToInt32(Request.QueryString["Rating"]) == 4) || (Convert.ToInt32(Request.QueryString["Rating"]) == 5))
            {
                RatedBy = Convert.ToInt32(myRatedBy) + 1;
                TRating = Convert.ToInt32(myScore) + Convert.ToInt32(Request.QueryString["rating"]);
            }
            else
            {
                RatedBy = Convert.ToInt32(myRatedBy);


源代码网推荐

源代码网供稿.
上一篇: ASP.NET Cookies FAQ  下一篇: C#程序书写规范
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华