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

I have a form in ASP.NET (C#). Two textboxes and a ASP.NET button. The idea is simple: Press the button, disable this, run the code (in code behind) and redirect to an other page.

Sounds simple, but I can't get it to work. I've tried Javascript, JQuery and .NET itself. Jquery is to complex to build in the website right now. Javascript seemed to work. On the button was a "OnClientClick". This is fired and the function disabled the button, but the code behind was not fired. Disabling the button in the code behind is not an option, because the page is not Ajax or something like that.

My guess is that Javascript comes close. I only need to execute the .net code of the button. How can I do this?


This is what worked half:

C#
protected void Page_Load(object sender, EventArgs e)
{
    btnClick.Attributes.Add("onclick", "this.disabled=true");
}

protected void Test(object sender, EventArgs e)
{

}


Set a breakpoint on void Test and run the freaking project. The button is disabled, but the script never reaches Test.

Anyone know the solution?

Thanks
Posted
Comments
rkthiyagarajan 10-Sep-11 11:18am    
if u want after button press the button should disable and redirect to another page?

just make some changes in javascript-

make a function

<script>
function disableMe(me)
{
   me.disabled=true;
   return true;
}
</script>


and now change the line at page load as

btnClick.Attributes.Add("onclick", "disableMe(this)");


return true; statement is reason to call server side code if u write return false; or remove return statement the server side code will not execute.

--Pankaj
 
Share this answer
 
You really don't need to explicitly disabled it by using Button.Enable true/false. The best ways is to use Page.ClientScript.GetPostBackEventReference

Just for example,
C#
protected void Page_Load(object sender, EventArgs e)
{
Button1.OnClientClick = ClientScript.GetPostBackEventReference(Button1, "") + "; this.value=’Processing…’;this.disabled = true;";
}

Note: Changing the text is totally depends on you. This is how you can achieve your requirement.
You can check out my complete post here Disabling button when performing some operation in ASP.NET[^]
Cheers !!
 
Share this answer
 
v2
Try this one , I hope this may help you

C#
protected void Button1_Click(object sender, EventArgs e)
       {
           Button1.Enabled = false;
           Response.AddHeader("Refresh", "5;Url=Default.aspx");

       }


Happy codeing.........
 
Share this answer
 
Thanks for your replies. I'm really sorry to say that none of the above worked for me. I could press the button mutliple times and it is never disabled.

Any other ideas?
 
Share this answer
 
Comments
rkthiyagarajan 11-Sep-11 7:09am    
Hello , Kenji Elzerman you try this? If you use asp button its work
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Enabled = false;
Response.AddHeader("Refresh", "5;Url=Default.aspx");

}
Ken Elz 11-Sep-11 7:11am    
Yup, I also tried that. Sorry, did not work either.

The strange thing is that hiding the button by CSS works fine. When I use that the button is hidden and the code behind (btn_Click i.e.) is fired.
rkthiyagarajan 11-Sep-11 7:16am    
You do all samples in new workfile then only try your project i hope.
<asp:button id="btnSend" runat="server" onclick="btnSend_Click" onclientclick="this.disabled=true" text="Send Mail" usesubmitbehavior="false" width="80" xmlns:asp="#unknown" />


In above sample make use of OnClientClick="this.disabled=true" and UseSubmitBehavior="false" properties.

This should make button disable on first click. Make it enable where/when ever required.
 
Share this answer
 
v2

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