disable and enable button in jquery after server event
i need to disable the button during the click and need to enable after my server side event is done right now my code looks like this
<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 开发者_JAVA百科 to enable my save from disabled to enabled stae
// can we do this in jQuery
}
}
looking for this code from PAST 2 DAYS any solution on this would be great thank you thank you
You can use the RegisterClientScriptBlock method on your Button1_Click
event, to add client scripts from the server-side:
var enableButton = "$('.saveButton').removeAttr('disabled');";
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "EnableButton",
enableButton, true);
I think the core issue here is your understanding of the page lifecycle, you might want to read up on it here http://msdn.microsoft.com/en-us/library/ms178472.aspx.
It looks like you don't necessarily want to post the page back. Consider updating the server using jQuery ajax and in the callback from that, re-enable the button.
Alternatively, this will re-enable the button after 5 seconds (after postback(hopefully!))
$(document).ready(function()
{ $('.saveButton').bind("click", function(e) {
$(this).attr("disabled", "true");
setTimeout(function(){$(this).removeAttr("disabled");}, 5000);
return true;
});
});
Or, at the end of your button click event write into a literal control something like this: "$(document).ready(function(){$('jQueryButtonSelector').removeAttr('disabled');});"
Or do the same using response.write()
I ran into issues with the other answers not calling server side event once disabled, to solve I asynchronously disabled the button.
Client
<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>
Server
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
精彩评论