简单的时间验证码自定义控件:
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);
}
}
}