我是靠谱客的博主 鲤鱼早晨,最近开发中收集的这篇文章主要介绍C#自定义评分控件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;


namespace WebControlLibrary
{
    [DefaultProperty("Text"), ToolboxData("<{0}:ImitateInputCalendar runat=server></{0}:ImitateInputCalendar>")]

    public class myImitateInputCalendar : System.Web.UI.WebControls.WebControl, IPostBackDataHandler
    {
        #region 公共属性
        private string size;
 
  [Bindable(true),Category("Appearance"),DefaultValue("")]
  
        public string Text
  {
   get
   {
    if (ViewState["Text"] != null)
    {
     return ViewState["Text"].ToString();
    }
    else
    {
     return null;
    }
   }

   set
   {
    ViewState["Text"] = value;
   }
  }

  [Bindable(true),Category("Appearance"),DefaultValue("")]

  public string Size
  {
   get
   {
    return size;
   }

   set
   {
    size = value;
   }
        }
        #endregion

        /// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name="output"> 要写出到的 HTML 编写器 </param>
  protected override void Render(HtmlTextWriter output)
  {
   output.AddAttribute(HtmlTextWriterAttribute.Id,"score");
   output.AddAttribute(HtmlTextWriterAttribute.Name,this.ID);
   if (this.CssClass != ""){output.AddAttribute(HtmlTextWriterAttribute.Class,this.CssClass);}
   if (this.Text != "" && this.Text != null){output.AddAttribute(HtmlTextWriterAttribute.Value,this.Text);}
   if (this.Size != "" && this.Size != null){output.AddAttribute(HtmlTextWriterAttribute.Size,this.Size);}
   output.AddAttribute(HtmlTextWriterAttribute.Style,"cursor:hand");
   output.AddAttribute(HtmlTextWriterAttribute.Type,"text");
   output.AddAttribute(HtmlTextWriterAttribute.ReadOnly,"true");
   output.AddAttribute(HtmlTextWriterAttribute.Onclick,"javascript:this.focus()");
   output.AddAttribute("onFocus","fPopCalendar(this,this,PopCal); return false;");
   output.RenderBeginTag(HtmlTextWriterTag.Input);
   output.RenderEndTag();

            if (!this.Page.IsClientScriptBlockRegistered("clientScript"))
   {
    output.WriteLine("<script language=/"JavaScript/">");

                #region js args
                output.WriteLine("var gdCtrl = new Object();");
                output.WriteLine("var VicPopCal = new Object();"); 
                output.WriteLine("var gcGray = /"#808080/";");   
                output.WriteLine("var gcToggle = /"pink/";");  //鼠标所在日期单元格的底色
                output.WriteLine("var gcBG = /"blue/";");      //背景色
                #endregion

                output.WriteLine("function fPopCalendar(popCtrl, dateCtrl, popCal){");
                //取消js冒泡
                output.WriteLine("parent.event.cancelBubble=true;");
                output.WriteLine("VicPopCal = popCal;");               
                output.WriteLine("gdCtrl = dateCtrl;");
                //得到一个point对象,由fGetXY(aTag)函数初始化,fGetXY(aTag)依赖function Point(iX, iY)。
                output.WriteLine("var point = fGetXY(popCtrl);");
                output.WriteLine("with (VicPopCal.style) {left = point.x;top = point.y+popCtrl.offsetHeight+1;visibility = 'visible';}");
                output.WriteLine("VicPopCal.focus();");
                output.WriteLine("}");

                #region 鼠标异动到某对象上的一系列函数,改变单元格的样式。
                //鼠标指向图标范围
                output.WriteLine("function mouseover(obj){");
                //output.WriteLine("obj.style.borderTop = 'buttonshadow 1px solid';");
                //output.WriteLine("obj.style.borderLeft = 'buttonshadow 1px solid';");
                //output.WriteLine("obj.style.borderRight = 'buttonhighlight 1px solid';");
                //output.WriteLine("obj.style.borderBottom = 'buttonhighlight 1px solid';");
                output.WriteLine("}");

                //鼠标移出图标范围
                output.WriteLine("function mouseout(obj){");
                //output.WriteLine("obj.style.borderTop = 'buttonhighlight 1px solid';");
                //output.WriteLine("obj.style.borderLeft = 'buttonhighlight 1px solid';");
                //output.WriteLine("obj.style.borderRight = 'buttonshadow 1px solid';");
                //output.WriteLine("obj.style.borderBottom = 'buttonshadow 1px solid';");
                output.WriteLine("}");

                //鼠标按下
                output.WriteLine("function mousedown(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonhighlight 1px solid';");
                output.WriteLine("}");

                //鼠标抬起
                output.WriteLine("function mouseup(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonshadow 1px solid';");
                output.WriteLine("}");
                #endregion

                #region 获得选中的单元格,并且传递其值到fSetValue(value)函数。
                output.WriteLine("function fSetSelected(aCell){");
                output.WriteLine("gdCtrl.value = aCell.value;");
                output.WriteLine("VicPopCal.style.visibility = /"hidden/";");
                output.WriteLine("}");
                #endregion

                output.WriteLine("function Point(iX, iY){this.x = iX;this.y = iY;}");//定义point

                #region 初始化图形函数
                output.WriteLine("function fDrawCal(TotalScore,Distance){");
                output.WriteLine("var totalTD = TotalScore / Distance;var MaxColumnNum = 4;var totalTR = Math.round(totalTD / MaxColumnNum);var otd = new Array();var tab = document.getElementById('tab');var score = 0;");
                output.WriteLine("for(i=0;i<totalTR;i++){var otr=tab.insertRow(-1);");
                output.WriteLine("for(j=0;j<MaxColumnNum&&j<totalTD;j++){otd[j]=otr.insertCell(-1);");
                output.WriteLine("otd[j].innerHTML='<input id=/"calCell/" type=/"text/" class=/"txt/" name=/"infoName_txt/" maxlength=/"2/" size=/"2/" value=/"/" style/"+styleTD+/" οnmοuseοver=/"mouseover(this)/" οnmοuseοut=/"mouseout(this)/" οnmοusedοwn=/"mousedown(this)/" οnmοuseup=/"mouseup(this)/" readonly=/"true/" οnclick=/"fSetSelected(this)/" />'");
                output.WriteLine("otd[j].childNodes[0].value=score+=Distance;");
                output.WriteLine("}}}");
                #endregion

                #region 得到鼠标的位置
                output.WriteLine("function fGetXY(aTag){");
                output.WriteLine("var oTmp = aTag;");
                output.WriteLine("var pt = new Point(0,0);");
                output.WriteLine("do {");
                output.WriteLine("pt.x += oTmp.offsetLeft;");
                output.WriteLine("pt.y += oTmp.offsetTop;");
                output.WriteLine("oTmp = oTmp.offsetParent;");
                output.WriteLine("} while(oTmp.tagName!=/"BODY/");");
                output.WriteLine("return pt;");
                output.WriteLine("}");
                #endregion

                #region 评分体
                output.WriteLine("with (document){");
                output.WriteLine("write(/"<Div id='PopCal' οnclick='event.cancelBubble=true' style='POSITION:absolute; VISIBILITY: hidden; bordercolor:#000000;border:2px ridge;width:10;z-index:100;'>/");");
                output.WriteLine("write(/"<TABLE id='popTable' border='1' bgcolor='#eeede8' cellpadding='0' cellspacing='0' style='font-size:12px;Z-INDEX:202;position:absolute;top:0;left:0;'>/");");
                output.WriteLine("write(/"<TR>/");");
                output.WriteLine("write(/"<td valign='middle' align='center' style='cursor:default'>/");");
                output.WriteLine("write(/"</td></TR><TR><td align='center'>/");");
                output.WriteLine("write(/"<DIV style='background-color:teal;'><table id='tab' width='100%' border='0' bgcolor='#E6EAE9' cellpadding='0' cellspacing='0'>/");");
                output.WriteLine("fDrawCal(100,5);");//这里要给出tr和td。
                output.WriteLine("write(/"</table></DIV>/");");
                output.WriteLine("write(/"</td></TR></TABLE>/");");
                output.WriteLine("write(/"</Div>/");");
                output.WriteLine("}");
                #endregion 评分体

                output.WriteLine("</script>");
    this.Page.RegisterClientScriptBlock("clientScript", "");
                /*
                 RegisterStartupScript      把script放置在 ASP.NET page的底部
                 RegisterClientScriptBlock  把script放置在 ASP.NET page的顶部。
                 */
            }
  }

  #region IPostBackDataHandler 成员
  
  public event EventHandler TextChanged;
  /// <summary>
  /// 当由类实现时,为 ASP.NET 服务器控件处理回发数据。
  /// </summary>
  /// <param name="postDataKey">控件的主要标识符</param>
  /// <param name="postCollection">所有传入名称值的集合</param>
  /// <returns>如果服务器控件的状态在回发发生后更改,则为 true;否则为 false。</returns>
  public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
  {
   String presentValue = Text;
   String postedValue = postCollection[postDataKey];

   if (presentValue == null || !presentValue.Equals(postedValue))
   {
    Text = postedValue;
    return true;
   }

   return false;
  }

     
  /// <summary>
  /// 当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
  /// </summary>
  public virtual void RaisePostDataChangedEvent()
  {
   OnTextChanged(EventArgs.Empty);
  }
     

  protected virtual void OnTextChanged(EventArgs e)
  {
   if (TextChanged != null)
    TextChanged(this,e);
  }
  #endregion
 }
}

最后

以上就是鲤鱼早晨为你收集整理的C#自定义评分控件的全部内容,希望文章能够帮你解决C#自定义评分控件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部