带提示的输入文本框ASP.NET服务器控件
需要设置的属性如下:
效果如下:
代码如下:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web;
[assembly: WebResource("Liping.HintTextBox.Image.ydot.png", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintdownbottom.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintdowntop.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintupbottom.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintuptop.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.script.hintJS.js", "application/javascript")]
namespace Liping.HintTextBox
{
[ToolboxData("<{0}:HintTextBox runat=server></{0}:HintTextBox>")]
public class HintTextBox : WebControl, IPostBackDataHandler
{
#region 字段属性
private string text;//文本框值
/// <summary>
/// 获取或设置文本
/// </summary>
[Description("设置文本值")]
public string Text
{
get { return text; }
set { text = value; }
}
private string hintText = "这是地球人都知道的东西,没什么好提示的。";//提示信息
/// <summary>
/// 获取或设置提示信息
/// </summary>
[Description("设置指示信息")]
public string HintText
{
get { return hintText; }
set { hintText = value; }
}
private int aspect;//朝向,有:up down
/// <summary>
/// 获取或设置提示框朝向:0为下1为上
/// </summary>
[Description("设置提示框朝向:0为下1为上")]
public int Aspect
{
get { return aspect; }
set { aspect = value; }
}
#region 提示框图片字段、属性
private string hintBorderColor = "000000";//提示框边框颜色
/// <summary>
/// 获取或设置提示框边框颜色
/// </summary>
[Description("设置提示框边框颜色")]
public string HintBorderColor
{
get { return hintBorderColor; }
set { hintBorderColor = value; }
}
private string hintBackColor = "FFFFE1";//提示框背景颜色
/// <summary>
/// 获取或设置提示框背景颜色
/// </summary>
[Description("设置提示框背景颜色")]
public string HintBackColor
{
get { return hintBackColor; }
set { hintBackColor = value; }
}
private string topImg = "";
/// <summary>
/// 设置提示框顶部图片
/// </summary>
[Description("设置提示框顶部图片")]
public string TopImg
{
get { return topImg; }
set { topImg = value; }
}
private string bottomImg = "";
/// <summary>
/// 设置提示框底部图片
/// </summary>
[Description("设置提示框底部图片")]
public string BottomImg
{
get { return bottomImg; }
set { bottomImg = value; }
}
private string hintImg = "";
//"style/images/ydot.png";//提示图片地址
/// <summary>
/// 获取或设置提示框标识图片
/// </summary>
[Description("设置提示框标识图片")]
public string HintImg
{
get { return hintImg; }
set { hintImg = value; }
}
#endregion
#endregion
protected override void Render(HtmlTextWriter writer)
{
#region 第一次使用必须加载的样式和脚本
string scriptKey = "hintJS";
if (!Page.IsClientScriptBlockRegistered(scriptKey) && !Page.IsStartupScriptRegistered(scriptKey))
{
string scriptBlock = "<script type=\"text/javascript\" src=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.script.hintJS.js") + "\" language=\"JavaScript\"></script>";
Page.RegisterStartupScript(scriptKey, scriptBlock);
writer.WriteLine("<span id=\"hintdiv\" style=\"display:none;position:absolute;z-index:500;\"></span>");
}
#endregion
//文本框
writer.RenderBeginTag(HtmlTextWriterTag.Div);
AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddAttribute(HtmlTextWriterAttribute.Value, text);
if (string.IsNullOrEmpty(topImg))
{
if (aspect == 1)
{
topImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintuptop.gif");
}
else
{
topImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintdowntop.gif");
}
}
if (string.IsNullOrEmpty(bottomImg))
{
if (aspect == 1)
{
bottomImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintupbottom.gif");
}
else
{
bottomImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintdownbottom.gif");
}
}
if (string.IsNullOrEmpty(hintImg))
{
hintImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.ydot.png");
}
writer.AddAttribute("onmouseover", "showhintinfo(this,0,0,'提示','" + hintText + "','0','" + (aspect == 1 ? "up" : "down") + "','" + topImg + "','" + bottomImg + "','" + hintImg + "')");
writer.AddAttribute("onmouseout", "hidehintinfo()");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
}
#region IPostBackDataHandler 成员
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
text = postCollection[this.UniqueID];
return false;
}
public void RaisePostDataChangedEvent()
{
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web;
[assembly: WebResource("Liping.HintTextBox.Image.ydot.png", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintdownbottom.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintdowntop.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintupbottom.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.Image.hintuptop.gif", "image/gif")]
[assembly: WebResource("Liping.HintTextBox.script.hintJS.js", "application/javascript")]
namespace Liping.HintTextBox
{
[ToolboxData("<{0}:HintTextBox runat=server></{0}:HintTextBox>")]
public class HintTextBox : WebControl, IPostBackDataHandler
{
#region 字段属性
private string text;//文本框值
/// <summary>
/// 获取或设置文本
/// </summary>
[Description("设置文本值")]
public string Text
{
get { return text; }
set { text = value; }
}
private string hintText = "这是地球人都知道的东西,没什么好提示的。";//提示信息
/// <summary>
/// 获取或设置提示信息
/// </summary>
[Description("设置指示信息")]
public string HintText
{
get { return hintText; }
set { hintText = value; }
}
private int aspect;//朝向,有:up down
/// <summary>
/// 获取或设置提示框朝向:0为下1为上
/// </summary>
[Description("设置提示框朝向:0为下1为上")]
public int Aspect
{
get { return aspect; }
set { aspect = value; }
}
#region 提示框图片字段、属性
private string hintBorderColor = "000000";//提示框边框颜色
/// <summary>
/// 获取或设置提示框边框颜色
/// </summary>
[Description("设置提示框边框颜色")]
public string HintBorderColor
{
get { return hintBorderColor; }
set { hintBorderColor = value; }
}
private string hintBackColor = "FFFFE1";//提示框背景颜色
/// <summary>
/// 获取或设置提示框背景颜色
/// </summary>
[Description("设置提示框背景颜色")]
public string HintBackColor
{
get { return hintBackColor; }
set { hintBackColor = value; }
}
private string topImg = "";
/// <summary>
/// 设置提示框顶部图片
/// </summary>
[Description("设置提示框顶部图片")]
public string TopImg
{
get { return topImg; }
set { topImg = value; }
}
private string bottomImg = "";
/// <summary>
/// 设置提示框底部图片
/// </summary>
[Description("设置提示框底部图片")]
public string BottomImg
{
get { return bottomImg; }
set { bottomImg = value; }
}
private string hintImg = "";
//"style/images/ydot.png";//提示图片地址
/// <summary>
/// 获取或设置提示框标识图片
/// </summary>
[Description("设置提示框标识图片")]
public string HintImg
{
get { return hintImg; }
set { hintImg = value; }
}
#endregion
#endregion
protected override void Render(HtmlTextWriter writer)
{
#region 第一次使用必须加载的样式和脚本
string scriptKey = "hintJS";
if (!Page.IsClientScriptBlockRegistered(scriptKey) && !Page.IsStartupScriptRegistered(scriptKey))
{
string scriptBlock = "<script type=\"text/javascript\" src=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.script.hintJS.js") + "\" language=\"JavaScript\"></script>";
Page.RegisterStartupScript(scriptKey, scriptBlock);
writer.WriteLine("<span id=\"hintdiv\" style=\"display:none;position:absolute;z-index:500;\"></span>");
}
#endregion
//文本框
writer.RenderBeginTag(HtmlTextWriterTag.Div);
AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddAttribute(HtmlTextWriterAttribute.Value, text);
if (string.IsNullOrEmpty(topImg))
{
if (aspect == 1)
{
topImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintuptop.gif");
}
else
{
topImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintdowntop.gif");
}
}
if (string.IsNullOrEmpty(bottomImg))
{
if (aspect == 1)
{
bottomImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintupbottom.gif");
}
else
{
bottomImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.hintdownbottom.gif");
}
}
if (string.IsNullOrEmpty(hintImg))
{
hintImg = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Liping.HintTextBox.Image.ydot.png");
}
writer.AddAttribute("onmouseover", "showhintinfo(this,0,0,'提示','" + hintText + "','0','" + (aspect == 1 ? "up" : "down") + "','" + topImg + "','" + bottomImg + "','" + hintImg + "')");
writer.AddAttribute("onmouseout", "hidehintinfo()");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
}
#region IPostBackDataHandler 成员
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
text = postCollection[this.UniqueID];
return false;
}
public void RaisePostDataChangedEvent()
{
}
#endregion
}
}