【原创】模仿以前新浪播客的评星控件
好长时间没有更新博客了,差点忘了这里还有我一个帐号, 呵呵
这个控件还是春节时在家无聊时写的,后来就长眠在我的机器里了,今天无意中才发现,写的比较烂,有些功能还没有实现,所以请园子里的高手帮我改改。
代码下载
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
namespace StarLike
{
[DefaultProperty("StarNum")]
[ToolboxData("<{0}:StarLike runat=server></{0}:StarLike>")]
public class StarLike : WebControl
{
字段#region 字段
private EnumDB _selectDB;
private string _xmldocpath;
private int _starnum = 5;//默认设置5颗星
private int playnum=0; //播放次数
private int attachnum; //参与评论人数
private string playurl;
System.Collections.ArrayList fimgList = null;
System.Collections.ArrayList imgList = null;
Image fimage = null;//评级档案
Image image = null;//评级图片
#endregion
属性#region 属性
[Description("设置显示几颗星")]
[DefaultValue(5)]
[Browsable(false)]
public int StarNum
{
get { return _starnum; }
set { _starnum = value; }
}
[Description("选择数据存放位置")]
public EnumDB SelectDB
{
get { return _selectDB; }
set { _selectDB = value; }
}
[Description("设置创建XML文档的位置,网站虚拟路径如:xmldrv/xmldoc.xml")]
public string XMLDocPath
{
get { return _xmldocpath; }
set { _xmldocpath = value; }
}
#endregion
重载方法#region 重载方法
protected override void OnInit(EventArgs e)
{
GetPlayURLQuest();
base.OnInit(e);
}
protected override void RenderContents(HtmlTextWriter output)
{
EnsureChildControls();//如果子控件不存在则创建
RenderChildren(output);
}
/**//// <summary>
/// 创建子控件
/// </summary>
protected override void CreateChildControls()
{
Controls.Clear();
ClearChildControlState();
TrackViewState();
Create_MyChildControl();//创建自定义子控件
Prepare_ControlHierarchy();//排序子控件
base.CreateChildControls();
}
/**//// <summary>
/// 注册Javascript
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), BuildingScript());
base.OnPreRender(e);
}
#endregion
私有方法#region 私有方法
/**//// <summary>
/// 创建自定义子控件
/// </summary>
private void Create_MyChildControl()
{
fimgList = new System.Collections.ArrayList();
for (int i = 1; i <= _starnum; i++)
{
fimage = new Image();
fimage.ID = "fstar" + i.ToString();
fimage.ImageUrl = "image\\v2_gx_hxx2.gif";
fimgList.Add(fimage);
}
imgList = new System.Collections.ArrayList();
for (int i = 1; i <= _starnum; i++)
{
image = new Image();
image.ID = "star" + i.ToString();
image.ImageUrl = "image\\v2_gx_hxx2.gif";
image.Attributes.Add("onMouseOver", "mn(" + i + ")");
image.Attributes.Add("onMouseOut", "mf(" + i + ")");
image.Attributes.Add("onClick", "xl_mc(" + i + ",\"" + playnum + "\",\"" + playurl + "\"" + ",\"" + DateTime.Now.ToString("yyyy MM dd_hh:mm:ss") + "\")");
imgList.Add(image);
}
image.Attributes.Add("onload","requestXMLDOM('XMLdoc/star.xml');");
}
/**//// <summary>
/// 排序子控件
/// </summary>
private void Prepare_ControlHierarchy()
{
HtmlTable tb = new HtmlTable();
tb.Width = "100%";
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td1 = new HtmlTableCell();
td1.ID = "td1";
td1.Width = "100px";
td1.InnerHtml = "<span id=\"playnum\"></apan>";
tr.Controls.Add(td1);
HtmlTableCell td2 = new HtmlTableCell();
td2.ID = "td2";
for (int i = 0; i < fimgList.Count; i++)
{
td2.Controls.Add(fimgList[i] as Image);
}
tr.Controls.Add(td2);
HtmlTableCell td3 = new HtmlTableCell();
td3.ID = "td3";
td3.InnerHtml = "<span style=\"color:#666\" id=\"divattach\"></span>";
tr.Controls.Add(td3);
HtmlTableCell td4 = new HtmlTableCell();
td4.Width = "100px";
td4.ID = "td4";
td4.InnerHtml = "<div id=\"desc\" style=\"color:#666\">请您评星:</div>";
tr.Controls.Add(td4);
HtmlTableCell td5 = new HtmlTableCell();
td5.ID = "td5";
for (int i = 0; i < imgList.Count; i++)
{
td5.Controls.Add(imgList[i] as Image);
}
tr.Controls.Add(td5);
tb.Controls.Add(tr);
this.Controls.Add(tb);
}
private string BuildingScript()
{
JavaScriptWriter js = new JavaScriptWriter(true);
js.AddConnentLine("创建XMLHTTPRequest对象");
js.AddLine(" var canstar=0;");
js.AddLine("var xRequest=null;");
js.AddLine("function getXMLHTTPRequest()");
js.AddLine("{");
js.AddLine("if(window.XMLHttpRequest)");
js.AddLine("{");
js.AddLine("xRequest=new XMLHttpRequest();");
js.AddLine("}");
js.AddLine("else if(typeof ActiveXObject)");
js.AddLine("{");
js.AddLine("xRequest=new ActiveXObject(\"Microsoft.XMLHTTP\");");
js.AddLine("}");
js.AddLine("return xRequest;");
js.AddLine("}");
js.AddConnentLine("发送请求");
js.AddLine("function sendRequest(callback,url,params,HttpMethod){");
js.AddLine("if (!HttpMethod){");
js.AddLine("HttpMethod=\"POST\";");
js.AddLine("}");
js.AddLine("var req=getXMLHTTPRequest();");
js.AddLine("if (req)");
js.AddLine("{");
js.AddLine("req.onreadystatechange= function() ");
js.AddLine("{");
js.AddLine("var ready=req.readyState;");
js.AddLine("var data=null;");
js.AddLine("if (ready==4)");
js.AddLine("{");
js.AddLine("data=req.responseXML;");
js.AddLine("callback(data);");
js.AddLine("} else ");
js.AddLine("{");
js.AddLine("data=\"loading[\"+ready+\"]\";");
js.AddLine("}");
js.AddLine("};");
js.AddLine("req.open(HttpMethod,url,true);");
js.AddLine("req.setRequestHeader(\"Content-Type\",\"application/x-www-form-urlencoded\");");
js.AddLine("req.send(params);");
js.AddLine("}");
js.AddLine("}");
js.AddConnentLine("获取getElementById对象");
js.AddLine("function $(idname){");
js.AddLine("if (document.getElementById){ return document.getElementById(idname); } ");
js.AddLine("else if (document.all) { return document.all[idname];} ");
js.AddLine("else if (document.layers) { return document.layers[idname]; }");
js.AddLine("else { return null; }");
js.AddLine("}");
//x选择星号的
//playnum 播放媒体号
//
//date服务器时间
js.AddLine("function xl_mc(x,playnum,playurl,date)");
js.AddLine("{");
js.AddLine("if(x>0)");
js.AddLine("createCookie(playurl,date);");//创建Cookie
js.AddLine("{ td5.innerHTML='<img src=image/v2_gx_pxjg'+x+'.gif>';");
js.AddLine("desc.innerHTML='<img src=image/ajax_loader1.gif/>';");
js.AddLine("var httprequest= sendRequest(star_callback,\"RequestStar.aspx\",\"starn=\"+x+\"&playnum=\"+playnum+\"&playurl=\"+playurl+\"&date=\"+date,\"POST\");");
js.AddLine("");
js.AddLine("}}");
js.AddLine("");
js.AddLine("function star_callback(data)");
js.AddLine("{");
js.AddLine("");
js.AddLine("if(data==null)");
js.AddLine("{desc.innerHTML=\"没有提交成功!\";");
js.AddLine("}");
js.AddLine("desc.innerHTML=data.getElementsByTagName('request')[0].getElementsByTagName('submitSuccess')[0].firstChild.nodeValue;");
js.AddLine("divattach.innerHTML=data.getElementsByTagName('request')[0].getElementsByTagName('evaluationNum')[0].firstChild.nodeValue+'人评星';");
//js.AddLine("requestXMLDOM('XMLdoc/star.xml');");
js.AddLine("}");
js.AddConnentLine("当鼠标移动到图片上时动态的显示评级信息");
js.AddLine("function mn(x){");
js.AddLine("var starnum=\"" + _starnum.ToString() + "\";");
js.AddLine("var appraise=\"\";");
js.AddLine("for(i=1;i<=x;i++){");
js.AddLine("var starnum=\"star\"+i;");
js.AddLine("var imgid=$(starnum);");
js.AddLine("imgid.src=\"image/v2_gx_hxx1.gif\"");
js.AddLine("var description=$(\"desc\");");
js.AddLine("switch (i){");
js.AddLine("case 1:");
js.AddLine("appraise=\"太差劲了\";");
js.AddLine("break;");
js.AddLine("case 2:");
js.AddLine("appraise=\"不怎么样\";");
js.AddLine("break;");
js.AddLine("case 3:");
js.AddLine("appraise=\"还可以\";");
js.AddLine("break;");
js.AddLine("case 4:");
js.AddLine("appraise=\"真不错\";");
js.AddLine("break;");
js.AddLine("case 5:");
js.AddLine("appraise=\"太棒了\";");
js.AddLine("break;");
js.AddLine("default:");
js.AddLine("appraise=\"出错了\";");
js.AddLine("}");
js.AddLine("description.style.color=\"#ff0000\"");
js.AddLine("description.innerHTML=appraise;");
js.AddLine("}}");
js.AddLine("function mf(x)");
js.AddLine("{");
js.AddLine("for(i=1;i<=x;i++)");
js.AddLine("{");
js.AddLine("var starnum=\"star\"+i;");
js.AddLine("var imgid=$(starnum);");
js.AddLine("imgid.src=\"image/v2_gx_hxx2.gif\";");
js.AddLine("var description=$(\"desc\");");
js.AddLine("description.style.color=\"#666\"");
js.AddLine("description.innerHTML=\"请您评星:\"");
js.AddLine("}");
js.AddLine("}");
#region
js.AddLine("var XMLDOM=null");
js.AddLine("function requestXMLDOM(url)");
js.AddLine("{");
js.AddLine("if(window.ActiveXObject)");
js.AddLine("{");
js.AddLine("XMLDOM=new ActiveXObject(\"Microsoft.XMLDOM\");");
js.AddLine("XMLDOM.onreadystatechange=xmlColback;");
js.AddLine("XMLDOM.load(url);");
js.AddLine("}");
js.AddLine("}");
js.AddLine("function xmlColback()");
js.AddLine("{");
js.AddLine("if(XMLDOM.readyState==4)");
js.AddLine("{");
js.AddLine("var rootNodes=XMLDOM.selectNodes(\"root/" + playurl + "\");");
js.AddLine("var playnum=$('divattach');");
js.AddLine("if(rootNodes.length!=0)");
js.AddLine("{");
js.AddLine("var lastnode=rootNodes.item(0).lastChild;");
js.AddLine("var lastid=lastnode.getElementsByTagName(\"id\")[0].firstChild.nodeValue;");
js.AddLine("var lastTotalScore=lastnode.getElementsByTagName(\"TotalScore\")[0].firstChild.nodeValue;");
js.AddLine("playnum.innerHTML=lastid+'人评星';");
js.AddLine("var average=lastTotalScore/lastid; ");//平均值
js.AddLine("var j=0;");
js.AddLine("var ronundvalue=Math.ceil(average)-1; ");//平均值的整数部分
js.AddLine("var fstars=null;");
js.AddLine("for(i=1;i<=ronundvalue;i++)");
js.AddLine("{fstars=$('fstar'+i);j=i+1;");
js.AddLine("fstars.src=\"Image/v2_gx_hxx1.gif\";");
js.AddLine("}");
js.AddLine("fstars=$('fstar'+j);");
js.AddLine("var remainder=(average-ronundvalue)*10;");//小数部分
js.AddLine("if(remainder>1 && remainder<3){");
js.AddLine("fstars.src=\"Image/v2_gx_hxx3n1.gif\";}");
js.AddLine("else if(remainder>=3 && remainder<7)");
js.AddLine("{fstars.src=\"Image/v2_gx_hxx3n2.gif\";}");
js.AddLine("else if(remainder>=7 && remainder<=9)");
js.AddLine("{fstars.src=\"Image/v2_gx_hxx3n3.gif\";}");
js.AddLine("else if(remainder==10){");
js.AddLine("fstars.src=\"Image/v2_gx_hxx1.gif\";}");
js.AddLine("}else playnum.innerHTML='0人评星';}");
js.AddLine("}");
#endregion
js.AddLine("function createCookie(key,value)");
js.AddLine("{");
js.AddLine("var today=new Date();");
js.AddLine("var expireDay=new Date();");
js.AddLine("var expireTime=24*60*60*1000");//保存一天
js.AddLine("expireDay.setTime(today.getTime()+expireTime);");
js.AddLine("document.cookie=key+\"=\"+value+\";expires=\"+expireDay.toGMTString();");
js.AddLine("}");
return js.ToString();
}
private void GetPlayURLQuest()
{
playurl=HttpContext.Current.Request.Url.OriginalString;
if (playurl.IndexOf('?') != -1)
{
playurl = playurl.Substring(playurl.IndexOf('?') + 1);
}
else
{
playurl = playurl.Substring(playurl.LastIndexOf('/')+1);
}
}
private void Fimgbuding()
{
int starno;
int fullstar = 0;
int halfstarnum = 0;
bool HalfImage = false;
XmlDocument doc = null;
switch (_selectDB)
{
case EnumDB.XMLDoc: //XML文档模式
if (_xmldocpath == null || _xmldocpath == string.Empty)//如果未设置文档路径
{
_xmldocpath = "XMLDoc/star.xml";
}
if (RetingXMLDoc.IsFileExists(_xmldocpath))//判断文档是否存在如果不存在则创建
{
doc = RetingXMLDoc.AddRootNodeToXmlDoc(_xmldocpath, "root");
RetingXMLDoc.AddNode(doc, "root", playurl);
RetingXMLDoc.AddNode(doc, "root" + playurl, "desponst");
}
else
{
NameValueCollection namevalue = RetingXMLDoc.SelectLastNodeValue(_xmldocpath, "root/"+playurl);
if (namevalue != null && namevalue.Get("id") != null)
{
attachnum = int.Parse(namevalue.Get("id"));
RetingSettings se = new RetingSettings(int.Parse(namevalue.Get("TotalScore")), attachnum);
//if (se.ShowHalfImage)//是否有半星图片
//{
halfstarnum = se.ImageHalfnum;
fullstar = se.RroundValue;
HalfImage = se.ShowHalfImage;
//}
}
}
break;
case EnumDB.SQLServer:
break;
default:
break;
}
fimgList = new System.Collections.ArrayList();
for (int j = 1; j <= fullstar; j++)
{
fimage = new Image();
fimage.ID = "ss_starfull" + j.ToString();
fimage.ImageUrl = "image\\v2_gx_hxx1.gif";
fimgList.Add(fimage);
}
if (HalfImage)
{
fimage = new Image();
fimage.ID = "ss_starhalf";
fimage.ImageUrl = "image\\v2_gx_hxx3n" + halfstarnum.ToString() + ".gif";
fimgList.Add(fimage);
}
if (HalfImage)
starno = _starnum - fullstar - 1;
else
starno = _starnum - fullstar;
for (int i = 1; i <= starno; i++)
{
fimage = new Image();
fimage.ID = "ss_starno" + i.ToString();
fimage.ImageUrl = "image\\v2_gx_hxx2.gif";
fimgList.Add(fimage);
}
}
#endregion
}
}