ASP.NET自定义Web服务器控件-TextBox文本框控件

40 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

//自定义服务器文本框控件
namespace MyControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MyTextBox runat=server></{0}:MyTextBox>")]
    public class MyTextBox : WebControl,IPostBackDataHandler
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

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

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AutoPostBack
        {
            get
            {
                if (ViewState["AutoPostBack"]==null) {
                    ViewState["AutoPostBack"] = false;//默认不是回传
                }
                return (bool)ViewState["AutoPostBack"];
            }

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

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Input; //input标签
            }
        }
        //初始化
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
        //加载
        protected override void OnLoad(EventArgs e)
        {
            //Properties(系统资源文件)这是此文本框资源文件里面增加的内容
            //[assembly:System.Web.UI.WebResource("MyControls.css.textbox.css","text/css",PerformSubstitution=true)]//添加样式资源
            //[assembly: System.Web.UI.WebResource("MyControls.js.textbox.js", "application/x-javascript")]         //添加javaScript资源
            //[assembly: System.Web.UI.WebResource("MyControls.images.form_input.png", "image/png")]                //添加图片资源(因为css文件中需要使用此图片设background图片)

            //(添加样式步骤2)拿到样式文件的路径
            string cssPath = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyControls.css.textbox.css");
            //(添加样式步骤3)根据引入外部样式<link rel="stylesheet" href="textbox.css" type="text/css" />这种语法,引入样式
            HtmlLink link = new HtmlLink();
            link.Href = cssPath;
            link.Attributes.Add("rel","stylesheet");
            link.Attributes.Add("type", "text/css");
            //(添加样式步骤4)在header[头部标签]添加一个样式文件,(添加样式步骤5)在Properties(系统资源文件)中添加样式资源
            this.Page.Header.Controls.Add(link);

            //(添加JS步骤1)获取js文件路径
            string jsPath = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyControls.js.textbox.js");
            //(添加JS步骤2)根据js路径注册js文件
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "textboxJavascript", "<script   src='" + jsPath + "' type='text/javascript'  language= 'javascript'></script> ");
            //(添加JS步骤3)在Properties(系统资源文件)中添加JS资源
            base.OnLoad(e);
        }
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            this.Attributes.Add("type","text");
            this.Attributes.Add("value",Text);
            //(添加样式步骤1)增加一个类样式
            this.Attributes.Add("class","txt_style"); 
            this.Attributes.Add("name",this.UniqueID);  //回传的时候必须加上此属性才行

            if (AutoPostBack)
            {
                this.Attributes.Add("onchange", "javascript:" + this.Page.GetPostBackEventReference(this)); //自动调用回发事件
            }
            base.RenderBeginTag(writer);
        }
        //打印内容
        protected override void RenderContents(HtmlTextWriter output)
        {
            //output.Write(Text);
        }

        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            string postData = this.Page.Request.Form[this.UniqueID]; //拿到提交文本框的值
            if (postData != Text)
            {  //如果从页面提交上来的数据不等于原来数据就返回true
                Text = postData;  //保存文本框内容
                return true;
            }
            return false;//返回false表示不执行后面的方法
        }
        public delegate void TxtChangeHandle();
        private object key;
        public event TxtChangeHandle Change {
            add {
                this.Events.AddHandler(key, value);
            }
            remove {
                this.Events.RemoveHandler(key, value);
            }
        }
        public void RaisePostDataChangedEvent()
        {
            //自定义的文本框改变事件
            TxtChangeHandle handle = (TxtChangeHandle)base.Events[key];
            if (handle != null) {
                handle();
            }
        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="MyControls" namespace="MyControls" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <!--自定义服务器文本框控件-->
        <cc1:MyTextBox ID="MyTextBox1" AutoPostBack="true" runat="server" />
    </div>
    </form>
</body>
</html>


.txt_style{ border:none; width:230px; height:25px; background:url(<%=WebResource("MyControls.images.form_input.png")%>)}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值