在服务器事件后禁用并启用jquery中的按钮

时间:2009-10-31 19:00:28

标签: asp.net jquery

我需要在点击期间禁用该按钮,并且需要在我的服务器端事件完成后启用   现在我的代码看起来像这个

<script src="../Scripts/jquery-1.3.2.js" type="text/javascript"></script>   
<script  type ="text/javascript" >


$(document).ready(function(){
    $('.saveButton').bind("click", function(e) {
        $(this).attr("disabled", "true");
        return true; //causes the client side script to run.
    });
});
</script>
//this  works  finr  for disableing the   button
//how  to enable  the  button  from disabled  to enable  after my server  side  event as  executed

<asp:Button ID="Button1" runat="server"  CssClass="saveButton"  Text="Button" 
        onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server"  CssClass="saveButton"  Text="Button" 
            onclick="Button2_Click" /> 

//Server   side  events
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
    }
    catch()
    {
    }
    finally
    {
        //once  my    control  reaches  here  i need   to enable  my save  from disabled to enabled stae 
        // can we  do this in jQuery
    }
}

从PAST 2 DAYS查找此代码 任何解决方案都会非常感谢你 谢谢

3 个答案:

答案 0 :(得分:3)

您可以在Button1_Click事件中使用RegisterClientScriptBlock方法,从服务器端添加客户端脚本:

var enableButton = "$('.saveButton').removeAttr('disabled');";
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "EnableButton",
                                            enableButton, true); 

答案 1 :(得分:0)

我认为这里的核心问题是您对页面生命周期的理解,您可能希望在此处阅读http://msdn.microsoft.com/en-us/library/ms178472.aspx

看起来您不一定要将页面发回。考虑使用jQuery ajax更新服务器,并在回调中重新启用按钮。

或者,这将在5秒后重新启用按钮(回发后(希望!))

$(document).ready(function()
    { $('.saveButton').bind("click", function(e) {
                        $(this).attr("disabled", "true"); 
                       setTimeout(function(){$(this).removeAttr("disabled");}, 5000);
                        return true; 
                        }); 
    });

或者,在按钮单击事件结束时写入文字控件,如下所示:     “$(文件)。就绪(函数(){$( 'jQueryButtonSelector')removeAttr( '禁用');});”

或使用response.write()

执行相同操作

答案 2 :(得分:0)

我遇到的问题是其他答案没有在禁用后调用服务器端事件,以解决我异步禁用按钮的问题。

<强>客户端

<asp:Button runat="server" ID="submitButton" ValidationGroup="DynamicForm" OnClientClick="disableButton(this);" />
<script>
    function disableButton(sender) { // Disable to prevent duplicate submission. Enabled in code behind
        if (Page_ClientValidate("DynamicForm")) {
            setTimeout(function () { $('#<%= submitButton.ClientID%>').attr("disabled", "true"); }, 0); // Asynchronously disable, otherwise disabled button won't submit first time
        }
     }
</script>

服务器

Private Sub submitbutton_Click(sender As Object, e As EventArgs) Handles submitButton.Click
    ...
    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "EnableButton", "$('#" + submitButton.ClientID + "').removeAttr('disabled');", True)
End Sub