[已解决问题] 如何防止UpdatePanel下的按钮多次提交
提问时间: 2008-04-15 14:17
悬赏分:10 浏览:252 次

在博客园搜索了。很多园友都是把按钮设置为不可用的状态。如何实现?特别是刚编译过后以后的程序运行起来比较慢。测试的时候如果多点几次就往数据库多写登陆日志。 像下面的表单如何防止用户多次提交。

              <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:TextBox ID="UserName" runat="server" CssClass="input"></asp:TextBox>
                <asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="input"></asp:TextBox>
                <asp:ImageButton ID="ibtnSave" runat="server" ImageUrl="~/Html/Images/btnlogon_new.gif" OnClick="ibtnSave_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>


提问者:迭戈 - 菜鸟二级

问题补充:我写的这个好像不能实现:
StringBuilder sb = new StringBuilder();
sb.Append(@"Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest); ");
sb.Append(@" function beginRequest(sender, args) ");
sb.Append(" { ");
if (this.ibtnEdit.Visible)
{
sb.AppendFormat(@"document.getElementById('{0}').style.disabled = true;", this.ibtnEdit.ClientID);
}
if (this.ibtnSubmit.Visible)
{
sb.AppendFormat(@"document.getElementById('{0}').style.disabled = true;", this.ibtnSubmit.ClientID);
}
if (this.ibtnReturn.Visible)
{
sb.AppendFormat(@"document.getElementById('{0}').style.disabled = true;", this.ibtnReturn.ClientID);
}
if (this.ibtnSubmitCheck.Visible)
{
sb.AppendFormat(@"document.getElementById('{0}').style.disabled = true;", this.ibtnSubmitCheck.ClientID);
}
sb.Append(" }");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "ImageButton", sb.ToString(), true);


最佳答案
很简单,代码如下:
<script type="text/javascript">
            function doClick(target){
                target.disabled=true;
                $get('<%= btnSave.ClientID %>').click();
            }
        </script>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:TextBox ID="UserName" runat="server" CssClass="input"></asp:TextBox>
                <asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="input"></asp:TextBox>
                <asp:Button ID="ibtnSave" runat="server"  OnClientClick="doClick(this);" Text="Click"/>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSave" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:Button ID="btnSave" runat="server" Width="0" Height="0" onclick="btnSave_Click" />
在btnSave_Click事件里写原来ibtnSave按钮Click事件的代码就可以了
2008/4/15 17:00:45 回答者:小No


提问者对于答案的评价:我3楼补充的代码应该比较实用吧。
评论
2个月前   迭戈 :
谢谢小No的回答。
不过如果我对表单验证不通过的时候。按钮是无效了。当时这是应该是按钮重新设置到有效状态。
有其他的解决方法吗。
2个月前   小No :
你的表单验证不用.NET自带的验证控件,用脚本验证不就可以了
2个月前   迭戈 :
调试成功了:应该是这样的。
HTML:如:
        void DisablePostElement()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(DisablePostElement); ");
            sb.Append(@" function DisablePostElement(sender,args) ");
            sb.Append(" { ");
            if (this.ibtnReturn.Visible)
            {
                sb.AppendFormat(@"ibtnReturn = $get('{0}');", this.ibtnReturn.ClientID);
                sb.Append(" if(ibtnReturn!=null)");
                sb.Append("{");
                sb.AppendFormat(@"ibtnReturn.disabled = true;");
                sb.Append("}");
            }
            if (this.ibtnSubmit.Visible)
            {
                sb.AppendFormat(@"ibtnSubmit = $get('{0}');", this.ibtnSubmit.ClientID);
                sb.Append(" if(ibtnSubmit!=null)");
                sb.Append("{");
                sb.AppendFormat(@"ibtnSubmit.disabled = true;");
                sb.Append("}");
            }
            sb.AppendFormat(@"if(args.get_postBackElement().id=='{0}' ||args.get_postBackElement().id=='{1}' )", this.ibtnSubmit.ClientID, this.ibtnReturn.ClientID);
            sb.Append(" { ");
            sb.AppendFormat(@"document.getElementById('wrapper').style.cursor = 'wait';");
            sb.Append(" }");
            sb.Append(" }");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "ImageButton", sb.ToString(), true);
        }

后台代码:
      protected void Page_Load(object sender, EventArgs e)
        {
            //.....
            DisablePostElement();
        }
        void DisablePostElement()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"Sys.WebForms.PageRequestManag
2个月前   小No :
虽然你的代码可以解决问题,但是我不喜欢在后台代码拼脚本的方式,因为这样代码很难阅读,维护比较麻烦
   您需要登录以后才能回答!
我的问题    我要提问


快到期问题

> 问题排行榜

有不合适内容,建议去除