zoukankan      html  css  js  c++  java
  • 自定义文本验证控件


    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; //引入样式资源(要先设置为嵌入式) [assembly: WebResource("WebControls.CSS.Validate.css", "text/css")] [assembly: WebResource("WebControls.JS.Validate.js", "text/javascript")] namespace WebControls { [DefaultProperty("Text")] [ToolboxData("<{0}:MyTextBoxControl runat=server></{0}:MyTextBoxControl>")] public class MyTextBoxControl : TextBox { [Bindable(true)] [Category("Appearance")] [DefaultValue("Number")] [Localizable(true)] private ValidateType validateType; /// <summary> /// 对外暴露验证类型 /// </summary> public ValidateType ValidateType { get { return validateType; } set { validateType = value; } } /// <summary> /// 控件预呈现 /// </summary> /// <param name="e"></param> protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); //为页面引入JS样式 Page.ClientScript.RegisterClientScriptResource(this.GetType(), "WebControls.JS.Validate.js"); //为页面引入CSS样式 using System.Web.UI.HtmlControls; HtmlLink html = new HtmlLink(); //判断id是否存在 存在就不执行后面的东西 if ("vid".Equals(html.ID)) return; //设置id html.ID = "vid"; //设置href html.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControls.CSS.Validate.css"); //设置type html.Attributes.Add("type", "text/css"); //设置rel html.Attributes.Add("rel", "stylesheet"); //将样式添加到页面头部 Page.Header.Controls.Add(html); } /// <summary> /// 控件呈现 /// </summary> /// <param name="writer"></param> protected override void Render(HtmlTextWriter writer) { base.Render(writer); switch (this.ValidateType) { case ValidateType.Number: writer.Write("请输入数字"); break; case ValidateType.AllChar: writer.Write("请输入字母"); break; case ValidateType.BigChar: writer.Write("请输入大写字母"); break; default: writer.Write("请输入小写字母"); break; } } /// <summary> /// 添加特性到呈现 /// </summary> /// <param name="writer"></param> protected override void AddAttributesToRender(HtmlTextWriter writer) { base.AddAttributesToRender(writer); //调用验证方法 writer.AddAttribute(HtmlTextWriterAttribute.Onchange, "ValidateText('" + this.ValidateType + "', this)"); //引入类样式 writer.AddAttribute(HtmlTextWriterAttribute.Class, "text"); } } /// <summary> /// 验证枚举 /// </summary> public enum ValidateType{ /// <summary> /// 验证数字 /// </summary> Number, /// <summary> /// 验证字母 /// </summary> AllChar, /// <summary> /// 大写字母 /// </summary> BigChar, /// <summary> /// 小写字母 /// </summary> SmallChar } }

    .aspx页面

     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestMyWebService.aspx.cs" Async="true" Inherits="TestMyWebService" %>
     2 
     3 <%@ Register Assembly="ValidateControl" Namespace="WebControls" TagPrefix="cc1" %>
     4 
     5 
     6 <!DOCTYPE html>
     7 
     8 <html xmlns="http://www.w3.org/1999/xhtml">
     9 <head runat="server">
    10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    11     <title></title>
    12 </head>
    13 <body>
    14     <form id="form1" runat="server">
    15     <div>
    16 
    17        <cc1:MyTextBoxControl ID="MyTextBoxControl1" runat="server" ValidateType="AllChar"></cc1:MyTextBoxControl> 

    67     <cc1:MyTextBoxControl ID="MyTextBoxControl2" runat="server" ValidateType="BigChar"></cc1:MyTextBoxControl>

    68     <cc1:MyTextBoxControl ID="MyTextBoxControl3" runat="server" ValidateType="Number"></cc1:MyTextBoxControl>

    69     <cc1:MyTextBoxControl ID="MyTextBoxControl4" runat="server" ValidateType="SmallChar"></cc1:MyTextBoxControl> 64 65 </div> 66 70 71 72 </form> 73 </body> 74 </html>

     显示出来的页面

      

    作者:JamelAr
    个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    C/C++程序内存泄漏检测
    linux下的内存管理
    Linker Script 链接器脚本
    linux内核进程调度以及定时器实现机制
    嵌入式软件设计中查找缺陷的几个技巧
    winCE DEBUGZONE
    程序员三个境界
    EJB初识(通熟易懂)
    JVM/JDK/JRE/IDE—区别(很经典)
    dubbo初识(一)Dubbo架构设计详解
  • 原文地址:https://www.cnblogs.com/JamelAr/p/7002853.html
Copyright © 2011-2022 走看看