📄 default.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
public partial class NoBot_Default : System.Web.UI.Page
{
//输出需要客户端执行的脚本
protected void NoBot1_GenerateChallengeAndResponse(object sender, AjaxControlToolkit.NoBotEventArgs e)
{
Label noBotLabel = new Label();//用于检验这个控件的高和宽的乘积
Random rand = new Random();
int width = rand.Next(80);//宽
int height = rand.Next(120);//高
noBotLabel.ID = string.Format("noBotLabel{0}", rand.Next(1000));//随机id
noBotLabel.Width = width;
noBotLabel.Height = height;
noBotLabel.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");//隐藏label,防止对页面布局的干扰
noBotLabel.Style.Add(HtmlTextWriterStyle.Position, "absolute");
(sender as NoBot).Controls.Add(noBotLabel);//把label添加入NoBot中
//需要客户端执行的脚本
e.ChallengeScript = string.Format("var noBotLabel = $get('{0}'); noBotLabel.offsetWidth * noBotLabel.offsetHeight;", noBotLabel.ClientID);
e.RequiredResponse = (width * height).ToString();//需要客户端返回的结果
}
//数据插入前,检验是否为垃圾发布程序所为
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
System.Threading.Thread.Sleep(2000);//休眠2秒,查看UpdateProgress的运行效果
NoBotState state;
if (NoBot1.IsValid(out state))//检验成功
{
Label1.Text = "您提交的成语已经录入成语数据库!";
}
else//检验失败
{
string errorMessage = string.Empty;
switch (state)//根据检验失败的原因不同,给出相应的提示。
{
case NoBotState.InvalidAddressTooActive:
errorMessage = "该IP地址在短时间内提交了过多的请求。";
break;
case NoBotState.InvalidBadResponse:
errorMessage = "浏览器检测脚本未被运行或运行结果不正确。";
break;
case NoBotState.InvalidBadSession:
errorMessage = "ASP.NET会话状态不可用。";
break;
case NoBotState.InvalidResponseTooSoon:
errorMessage = "两次发送表单的时间间隔过短。";
break;
case NoBotState.InvalidUnknown:
errorMessage = "未知错误。";
break;
}
Label1.Text = string.Format("您的成语添加请求未被执行,原因为:{0}", errorMessage);
e.Cancel = true;//取消插入数据
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -