2

I want when user click on button,disable it and after do work enable it from code behind

i use below code but disable it and then enable it,because page load again and doesn't call button event click from code behind

ASPX

<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
    <ContentTemplate>
        <div>
            <asp:Button runat="server" ID="btnReg" OnClick="reg_Click" OnClientClick="disableButton();" Text="Click On Me..." />
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnReg" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

JavaScript

function disableButton() 
{
    document.getElementById("<%=btnReg.ClientID%>").disabled = true;
}

Code behind

protected void reg_Click(object sender, EventArgs e)
{
    //do work
    btnReg.Enabled = true;
}

1 Answer 1

2

Disable your button using javascript as shown:

document.getElementById("<%=btnReg.ClientID%>").disabled = true;

Enable it from codebehind as shown:

//after doing some logic 

btnReg.Enabled = true;
UpdatePanel.Update();

For info read this

$(function() {    
  function doosomething()
  {
    document.getElementById("<%=btnReg.ClientID%>").enabled= true;
  }
};

Call this function as soon as you disable the button from your codebehind as shown:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "doosomething();", true);
Sign up to request clarification or add additional context in comments.

7 Comments

I did it,like question (set javascript function to OnClientClick and code behind to Onclick),but when i click on button behind code doesn't call, why?
now, my problem is not that button doesn't enable or disable, my problem is when i disable button with javascript after that click event doesn't call and then i can't do work that this button should
why not call a function after you disable the button using javascript function from your codebehind as updated in my answer...
onClick="reg_Click" , the reg_Click doesn't call,please test your answer
Place a breakpoint at reg_click event and find where is the problem going on ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.