Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

I have this ASP.net button in ASP.net page:

ASP.NET
<asp:Button ID="btnVerifyEmail" runat="server" Text="Verify Email" OnClientClick="return CheckForRequiredFields()" OnClick="btnVerifyEmail_Click" />


When i press button, it firstly execute Javascript method that if it return false will not go to server else go to server to execute server code,

I want to make the Button disabled before executing server code to prevent User from press on it again, so i added this code to the form load of the page:

C#
btnVerifyEmail.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(btnVerifyEmail, "").ToString());


this code work fine only if i remove the OnClientClick="return CheckForRequiredFields()",

but if i used Javascript method the button not be disabled and user can press it more and more

so what can i do
any idea?
Posted

Obviously Page.ClientScript.GetPostBackEventReference won't work because its on the server, after the button has been clicked and the postback has occcured.

<asp:Button id='btnVerifyEmail' runat='server' Text='Verify Email' onclientclick='return CheckForRequiredFields(this)'/>

function CheckForRequiredFields(btn)
{
  btn.disabled = isValid();
  return !btn.disabled;
}


Whatever you validation function is if it returns true it will set the disabled property to true and return false which will not allow the postback to occur.
 
Share this answer
 
v4
Comments
MrLonely_2 17-Nov-11 23:35pm    
Thanks, but when the isValid() function return true it disable the button and not cause the postback to occur, i test it.
[no name] 17-Nov-11 23:46pm    
Then you must be doing something wrong
MrLonely_2 17-Nov-11 23:52pm    
That is my button:
<asp:Button ID="btnCreateAccount" runat="server" Text="Button" OnClientClick="return CheckForRequiredFields(this)" OnClick="btnCreateAccount_Click" />

and that is Javascript:
<script type="text/javascript">

function CheckForRequiredFields(btn)
{
btn.disabled = isValid();
return btn.disabled;
}

function isValid()
{
// code goes here...
}

</script>

then tell me the error.
[no name] 18-Nov-11 11:43am    
Sorry, my fault I forgot the !

See the updated solution.
If you are returning true then just add a code to disable it, something like this :
JavaScript
document.getelementbyid(clientId of ur button).disabled = true;
 
Share this answer
 
v3
Hi,

I think you could disable your button in client code function like example below:

C#
$get('<%= btnVerifyEmail.ClientID %>').disabled = true;


Please vote if could help...

Regards,
Al
 
Share this answer
 
Comments
MrLonely_2 17-Nov-11 22:52pm    
I already did that before but that cause the button to be disable and not go to server code and not doing postback
Al Moje 17-Nov-11 23:36pm    
I think there's no need to add an attribute for
'Page.ClientScript.GetPostBackEventReference', and just make a condition on your client function by manipulating button either setting it as to be enable or disable.
Al Moje 18-Nov-11 1:13am    
Example:

<script type="text/javascript">

function CheckForRequiredFields() {
var xx = $get('<%= txtAtt.ClientID %>').value;
if (xx == '') {
alert("Attachment is required...");
return false;
}
else {
return true;
}
}
</script>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900