/// <summary>
/// created by 章松山 2012-09-20
/// </summary>
public class TipTextBox:TextBox
{
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public string DefalutText
{
get
{
String s = (String)ViewState["DefalutText"];
return ((s == null) ? String.Empty : s.Trim());
}
set
{
ViewState["DefalutText"] = value;
}
}
public string TipDisplay
{
get
{
String s = (String)ViewState["TipDisplay"];
return ((s == null) ? "inline" : s.Trim());
}
set
{
ViewState["TipDisplay"] = value;
}
}
public string ContentDisplay
{
get
{
String s = (String)ViewState["ContentDisplay"];
return ((s == null) ? "none" : s.Trim());
}
set
{
ViewState["ContentDisplay"] = value;
}
}
protected override void RaisePostDataChangedEvent()
{
if (!string.IsNullOrEmpty(this.Text))
{
TipDisplay = "none";
ContentDisplay = "inline";
}
else
{
TipDisplay = "inline";
ContentDisplay = "none";
}
base.RaisePostDataChangedEvent();
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string tiponclickstr =GetOnclickStr("spantip");
writer.Write("<span id=\"spantip" + this.ID + "\" style=\"" + this.Width.ToString() + ";display:" + TipDisplay + "\" onclick=" + tiponclickstr + ">");
string htmltip = "<input type=\"text\" style=\"" + this.Width.ToString() + ";color:Gray\" value='" + DefalutText + "' id=txtTip" + this.ID + " />";
writer.Write(htmltip);
writer.Write("</span>");
string contentonclickstr = GetOnclickStr("spancontent");
writer.Write("<span id=\"spancontent" + this.ID + "\" style=\"" + this.Width.ToString() + ";display:"+ContentDisplay+"\">");
base.Render(writer);
writer.Write("</span>");
}
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
this.Attributes.Add("onblur", GetOnclickStr("spancontent"));
this.Style.Add("width", this.Width.ToString());
base.AddAttributesToRender(writer);
}
private string GetOnclickStr(string htmlkind)
{
string str = string.Empty;
if (htmlkind == "spantip")
{
str = "document.getElementById('spantip" + this.ID + "').style.display='none';";
str += "document.getElementById('spancontent" + this.ID + "').style.display='inline';";
str += "document.getElementById('"+this.ClientID + "').focus();";
}
else
{
str = "if(this.value==''){document.getElementById('spantip" + this.ID + "').style.display='inline';";
str += "document.getElementById('spancontent" + this.ID + "').style.display='none';}";
}
return str;
}
}