Digg 顶 踩(jQuery+Ajax)

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

<!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 runat="server">
    <title>digg</title>
    <link href="css/page.css" rel="stylesheet" type="text/css" media="all" />
    <script src="js/cookie.js" type="text/javascript" language="javascript"  ></script>
    <script src="js/jquery-1.3.2.min.js" type="text/javascript" language="javascript"  ></script>
  
  <script type="text/javascript">
  function postDigg(ftype,aid){
  
  //设置cookie
  var saveid = GetCookie('diggid');
    if(saveid != null)
    {
        var saveids = saveid.split(',');
        var hasid = false;
        saveid = '';
        j = 1;
        for(i=saveids.length-1;i>=0;i--)
        {
            if(saveids[i]==aid && hasid) continue;
            else {
                if(saveids[i]==aid && !hasid) hasid = true;
                saveid += (saveid=='' ? saveids[i] : ','+saveids[i]);
                j++;
                if(j==20 && hasid) break;
                if(j==19 && !hasid) break;
            }
        }
        if(hasid) { alert("您已经顶过该帖,请不要重复顶帖 !"); return; }
        else saveid += ','+aid;
        SetCookie('diggid',saveid,1);
    }
    else
    {
        SetCookie('diggid',aid,1);
    }
    
  
  
  //获取数据
  
    $.ajax({
        url:"Digg.aspx?action="+ftype+"&id="+aid+" &time="+new Date().toString(),
        type:'GET',
        success:function(){
            $('#newdigg').html(arguments[0]);
        }
    });
}


 function  getDigg(aid){
    $.ajax({
        url:"Digg.aspx?id="+aid+"  & time="+new Date().toString(),
        type:'GET',
        success:function(){
            $('#newdigg').html(arguments[0]);
        }
    });
}

  </script>
   
    
</head>
<body>
    <form id="form1" runat="server">
    
  
   <div class="newdigg" id="newdigg">  </div>
   
    <script  language="javascript" type="text/javascript">getDigg(197);</script>
    
   <div class="boxoff">
   <strong>------分隔线----------------------------</strong>
   </div>
   
   
    </form>
</body>
</html>

 

 

//Digg.aspx

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;

public partial class Digg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string s = Request["id"].ToString();
        int aid = int.Parse(s);
        if (Request["action"] != null)  //一定要这个
        {
            string action = Request["action"].ToString();

            db d = new db();

            if (action == "good")
            {
                d.excuteSQL("Update Digg set goodpost=goodpost+1,lastpost=getdate() where contentid=" + aid);
            }
            else if (action == "bad")
            {
                d.excuteSQL("Update Digg set badpost=badpost+1,lastpost=getdate() where contentid=" + aid);
            }
        }
        string ss = getDigg(aid);
        Response.Write(ss);
       
    }

    public string  getDigg(int strId)
    {
        int goodper;
        int badper;
        string digg = "";
        db d = new db();
        DataTable dt = d.getDT("Select goodpost,badpost  From Digg  where contentid="+strId);
        foreach (DataRow dr in dt.Rows)
        {
            int goodposts = Convert.ToInt32(dr["goodpost"]);
            int badposts = Convert.ToInt32(dr["badpost"]);
            if (goodposts + badposts == 0)
            {
                goodper = badper = 0;
            }
            else
            {
                int alls = goodposts + badposts;
                goodper = Convert.ToInt32((double)goodposts / alls * 100);
                badper = 100 - goodper;
            }

            digg += "<div class=\"diggbox digg_good\" onmousemove=\"this.style.backgroundPosition='left bottom';\"  onmouseout=\"this.style.backgroundPosition='left top';\" onclick=\"postDigg('good'," + strId + ")\">";
            digg += "<div class=\"digg_act\">顶一下</div>";
            digg += "<div class=\"digg_num\">(" + goodposts + ")</div>";
            digg += "<div class=\"digg_percent\">";
            digg += "<div class=\"digg_percent_bar\"><span style=\"width:" + goodper + "%\"></span></div>";
            digg += "<div class=\"digg_percent_num\">" + goodper + "%</div>";
            digg += "</div>";
            digg += "</div>";
            digg += "<div class=\"diggbox digg_bad\" onmousemove=\"this.style.backgroundPosition='right bottom';\"  onmouseout=\"this.style.backgroundPosition='right top';\" onclick=\"postDigg('bad'," + strId + ")\">";
            digg += "<div class=\"digg_act\">踩一下</div>";
            digg += "<div class=\"digg_num\">(" + badposts + ")</div>";
            digg += "<div class=\"digg_percent\">";
            digg += "<div class=\"digg_percent_bar\"><span style=\"width:" + badper + "%\"></span></div>";
            digg += "<div class=\"digg_percent_num\">" + badper + "%</div>";
            digg += "</div>";
            digg += "</div>";
          
        }
      
        return digg;
      
    }
}

 

//cookie.js

//读写cookie函数
function GetCookie(c_name)
{
    if (document.cookie.length > 0)
    {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1)
        {
            c_start = c_start + c_name.length + 1;
            c_end   = document.cookie.indexOf(";",c_start);
            if (c_end == -1)
            {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return null
}

function SetCookie(c_name,value,expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
     //使设置的有效时间正确。增加toGMTString()
}
posted @ 2009-10-12 16:10  曾祥展  阅读(2236)  评论(4编辑  收藏  举报