会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
deadshot123
继承Button防止表单的重复提交!
继承Button防止表单的重复提交!
源码:
using
System;
using
System.Web.UI.WebControls;
namespace
HMG.WebControls
{
/**/
///
<summary>
///
提交成功后执行关闭还是重定位到本页面
///
</summary>
public
enum
SubmitStyle
{
/**/
///
<summary>
///
重定位
///
</summary>
Redirect,
/**/
///
<summary>
///
关闭
///
</summary>
Close
}
/**/
///
<summary>
///
SubmitOnceButton 的摘要说明。
///
</summary>
public
class
SubmitOnceButton:System.Web.UI.WebControls.Button
{
/**/
///
<summary>
///
默认的构造函数,当没有构造函数的时候不会执行
///
</summary>
public
SubmitOnceButton():
base
()
{
}
/**/
///
<summary>
///
执行成功后重定位的Url
///
</summary>
public
string
Url
{
get
{
if
(ViewState[
"
url
"
]
==
null
)
{
ViewState[
"
url
"
]
=
string
.Empty;
}
return
ViewState[
"
url
"
].ToString();
}
set
{
ViewState[
"
url
"
]
=
value;
}
}
/**/
///
<summary>
///
执行时候显示的临时文本
///
</summary>
public
string
TempText
{
get
{
if
(ViewState[
"
temptext
"
]
==
null
)
{
ViewState[
"
temptext
"
]
=
"
请稍候
"
;
}
return
ViewState[
"
temptext
"
].ToString();
}
set
{
ViewState[
"
temptext
"
]
=
value;
}
}
/**/
///
<summary>
///
执行成功后关闭本页面还是重定位 使用的样式
///
</summary>
private
SubmitStyle hint
=
SubmitStyle.Redirect;
/**/
///
<summary>
///
执行成功后关闭本页面还是重定位
///
</summary>
public
SubmitStyle SubmitSuccessStyle
{
get
{
return
this
.hint;
}
set
{
this
.hint
=
value;
}
}
private
string
GetScriptBlock()
{
const
string
FormatStr
=
"
javascript:this.value='{0}';setTimeout(function Test(){{this.disabled=true;}},0);
"
;
string
temp
=
string
.Empty;
return
string
.Format(FormatStr,
new
object
[]
{
this
.TempText}
)
+
temp;
}
/**/
///
<summary>
///
执行成功后重新定位
///
</summary>
///
<param name="msg">
成功后的提示信息
</param>
public
void
SubmitSuccess(
string
msg)
{
if
(
this
.Url
==
string
.Empty)
{
this
.Url
=
this
.Page.Request.UrlReferrer.ToString();
}
if
(
this
.SubmitSuccessStyle
==
SubmitStyle.Redirect)
{
this
.Page.RegisterStartupScript(
this
.ClientID,
"
<script>alert('
"
+
msg
+
"
');window.location.href='
"
+
this
.Url
+
"
';history.go(1);</script>
"
);
}
else
{
this
.Page.RegisterStartupScript(
this
.ClientID,
"
<script>alert('
"
+
msg
+
"
');window.opener=null;window.close();</script>
"
);
}
}
/**/
///
<summary>
///
给加客户端提示信息
///
</summary>
///
<param name="writer">
输出流
</param>
protected
override
void
AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
this
.Attributes.Add(
"
onclick
"
,
this
.GetScriptBlock());
base
.AddAttributesToRender (writer);
}
}
}
使用时候的单击事件
private
void
SubmitOnceButton1_Click(
object
sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(
500
);
this
.TextBox1.Text
=
"
username
"
;
System.Diagnostics.Debug.WriteLine(
"
excute submitonce!
"
);
this
.SubmitOnceButton1.SubmitSuccess(
"
提交成功!
"
);
//
提交成功的时候执行这个方法即可
}
发表于
2006-12-22 13:36
deadshot123
阅读(
659
) 评论(
1
)
编辑
收藏
举报
刷新页面
返回顶部
公告
导航
博客园
首页
新随笔
新文章
联系
订阅
管理