自定义控件-编写笔记(简单的时间验证码和事件控件)

本文介绍了如何在ASP.NET中创建自定义控件,包括时间验证码控件、事件触发按钮及自定义Label。通过示例代码展示了控件的实现方式及其在网页上的呈现效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单的时间验证码自定义控件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace MyDIYControl.YZM
{
    public class SuperYZM:Control
    {
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                writer.Write("这是验证码");
                return;
            }
            if (HttpContext.Current.Request.QueryString["123123"] == null)
            {
                //发送标签
                string pageName = HttpContext.Current.Request.Url + "?123123=123";
                writer.Write("<img src='{0}' />", pageName);
            }
            else
            {
                //清除缓冲内容
                HttpContext.Current.Response.Clear();
                生成图片();
                //输出缓冲
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }

        private void 生成图片()
        {
            //发送图片
            System.Drawing.Image 画布 = new Bitmap(300, 40);
            using (Graphics 作图器 =Graphics.FromImage(画布))
            {
                作图器.Clear(Color.White);
                作图器.DrawString(
                        DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), //内容
                        new Font("宋体", 12),           //字体,大小
                        new SolidBrush(Color.Red),      //线型种类,色彩
                        new PointF(2, 2)                //位置
                    );

                //图片保存到输出流,即响应客户端
                画布.Save(
                          HttpContext.Current.Response.OutputStream,
                          System.Drawing.Imaging.ImageFormat.Jpeg
                    );
            }
        }
    }
}

事件控件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyDIYControl.Button
{
    public class MyPicButton:Control,IPostBackEventHandler
    {

        public string ImageURL { get; set; }

        //单击事件
        public event EventHandler OnClick;
        public event EventHandler OnChange;

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                writer.Write("自定义图片按钮");
            }
            string html = string.Format("<a herf=\"{0}\" id='{1}' name='{1}'><img src='{2}'/></a>",
                Page.ClientScript.GetPostBackClientHyperlink(this,"click"),
                this.UniqueID,ImageURL
                );

            writer.Write(html);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            switch (eventArgument)
            { 
                case "click":
                    if (this.OnClick == null) return;
                    this.OnClick(this, EventArgs.Empty);
                    break;
                case "change":
                    if (this.OnChange == null) return;
                    this.OnChange(this, EventArgs.Empty);
                    break;
            }
        }
    }
}

简单的Label

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace MyDIYControl.MyLable
{
    public class Lable:Control
    {
        private string text;

        public string Text
        {
            get 
            {
                return ViewState[base.UniqueID] == null ? "" : ViewState[base.UniqueID].ToString();
            }
            set 
            {
                ViewState[base.UniqueID] = value;
            }
        }

        public string Color
        {
            get
            {
                return ViewState[base.UniqueID+"Color"] == null ? "Black" : ViewState[base.UniqueID+"Color"].ToString();
            }
            set
            {
                ViewState[base.UniqueID + "Color"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                if (Text!=null)
                {
                    writer.Write(Text);
                }
                writer.Write("自定义Label");
                return;
            }
            writer.Write("<span style='color:{1}'>{0}</span>",Text,Color);
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值