为TextBox增加隐藏属性,Value属性

80酷酷网    80kuku.com

  //代码如下
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;

namespace DXH.Web.UI.WebControls
{
public enum TextBoxMode{
SingleLine = 0,
MultiLine = 1,
Password = 2,
Hidden = 3
}
/// <summary>
/// TextBox 的摘要说明。
/// </summary>
[ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]
public class TextBox : System.Web.UI.WebControls.TextBox
{
[Browsable(false)]
public override System.Web.UI.WebControls.TextBoxMode TextMode
{
get
{
return base.TextMode;
}
set
{
base.TextMode = value;
}
}
[Description("自定义文本框的行为模式"),DefaultValue(0),Browsable(true),Category("Behavior")]
public TextBoxMode Mode{
get
{
object textMode = this.ViewState["TextMode"];
if (textMode != null)
{
return ((TextBoxMode) textMode);
}
return TextBoxMode.SingleLine;
}
set
{
if ((value < TextBoxMode.SingleLine) || (value > TextBoxMode.Hidden))
{
throw new ArgumentOutOfRangeException("value");
}
this.ViewState["TextMode"] = value;
if(value == TextBoxMode.Hidden)
this.TextMode = (System.Web.UI.WebControls.TextBoxMode)TextBoxMode.SingleLine;
else
this.TextMode = (System.Web.UI.WebControls.TextBoxMode)value;
}

}
[Description("对Text修正后的值"),DefaultValue(""),Browsable(true),Category("Appearance")]
public string Value{
get{return InputText(base.Text,base.MaxLength);}
}
private string InputText(string inputString, int maxLength)
{


StringBuilder retVal = new StringBuilder();

if ((inputString != null) && (inputString != String.Empty))
{
inputString = inputString.Trim();

if (0 != maxLength && inputString.Length > maxLength)
inputString = inputString.Substring(0, maxLength);

for (int i = 0; i < inputString.Length; i++)
{
switch (inputString[i])
{
case '"':
retVal.Append(""");
break;
case '<':
retVal.Append("<");
break;
case '>':
retVal.Append(">");
break;
default:
retVal.Append(inputString[i]);
break;
}
}

retVal.Replace("'", "''");
}

return retVal.ToString();

}

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
if(this.Mode == TextBoxMode.Hidden)
{
writer.AddStyleAttribute("display", "none");
writer.AddAttribute(HtmlTextWriterAttribute.Type,"hidden");
}
base.AddAttributesToRender(writer);

}

}
}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: