13

With this code, i try to Close a Window (the way i'm doing it works) but i have also an Onclick event which is ignored!

<script type="text/javascript">


            function GetRadWindow() {
                var oWindow = null;
                if (window.radWindow) oWindow = window.radWindow;
                else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
                return oWindow;
            }

            function CloseDialog() {
                GetRadWindow().close();

            }  

ASPX page:

 <asp:Button ID="Button1" runat="server" Text="Soumettre ce ticket" 
                onclick="Button1_Click"  OnClientClick="CloseDialog();return false;"/>

My application never enters Button1_click event, can anyone help me to find out why ? thanks in advance

EDIT: HTML GENERATED FOR THE BUTTON

<input type="submit" id="Button1" onclick="CloseDialog();" value="Soumettre ce ticket" name="Button1"/>
  • 2
    Try removing the return false; from your onClientClick. This is probably supressing the event for onclick. – dash Mar 08 '12 at 14:44
  • Can you show us what's actually delivered to the client? – greg84 Mar 08 '12 at 14:45
  • Could you update your question with the actual HTML that is generated for `Button1`? – shanabus Mar 08 '12 at 14:45
  • Can you show the event handler for Button1_Click please? Also, as shanabus mentions, add the HTML for the button. – dash Mar 08 '12 at 15:06
  • I just added the HTML for the button ! THX –  Mar 08 '12 at 15:12
  • possible duplicate of [OnclientClick and OnClick is not working at the same time?](http://stackoverflow.com/questions/2155048/onclientclick-and-onclick-is-not-working-at-the-same-time) – Andrew Marshall Mar 09 '12 at 17:27

5 Answers5

17

This article kind of explains the problem. You need to return true in your JS if you want the server event to trigger. Otherwise, you have to return false.

And, it also looks like you will have to add the UseSubmitBehavior = false based on: OnclientClick and OnClick is not working at the same time?

This is especially evident after seeing that your generated HTML only has the CloseDialog() and not the call to Button1_Click. This change will concatenate to the end of your onclick.

<asp:Button ID="Button1" runat="server" Text="Soumettre ce ticket" 
            onclick="Button1_Click"  OnClientClick="CloseDialog();"  
            UseSubmitBehavior="false"/>
Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • I just removed the return false; in Onclient Click and added return true within CloseDialog() function but it's unfortunately not working –  Mar 08 '12 at 14:57
  • Hello, what if the CloseDialog() here contains some ajax treatment that affects the reurn value what is the solution? – Haithem KAROUI Feb 15 '16 at 15:11
  • @KarouiHaythem you might want to open a new ticket referencing this one. I'm not sure off the top of my head and cannot guarantee when I'll get around to answering. – Justin Pihony Feb 17 '16 at 01:26
8

I ran into this problem and using UseSubmitBehavior="false" nearly did the trick. Be sure to have your OnClientClick call set up correctly:

My code was using OnClientClick="return ValidateSearch();" which is incorrect. It should just be

<asp:Button ID="keywordSearch" runat="server" Text="Search" TabIndex="1" 
OnClick="keywordSearch_Click" OnClientClick="if (!ValidateSearch()) { return false;};" />

See the Source here (and tell them thanks!)

If it is set up incorrectly, the OnClick function will not fire.

Gaʀʀʏ
  • 4,372
  • 3
  • 39
  • 59
5

You are returning false in the onclientclick, so the event is returned before the postback. As a result, onclick never fires.

Jean
  • 7,623
  • 6
  • 43
  • 58
Evert
  • 8,161
  • 1
  • 15
  • 17
  • +1 That's bob on! The return false will prevent the rendered submit button from POSTing the ASP.NET form. – greg84 Mar 08 '12 at 14:46
  • Agree on your answer - I commented around the same time you posted your answer so a +1 from me should counter the random downvote :-) – dash Mar 08 '12 at 14:48
  • Thanks a lot for your help but it's not working either when i remove "return false;".... –  Mar 08 '12 at 14:58
0

I just came across the same issue, and I think we're getting confused between server side enable="false" and client-side "disable".

The serverside property to enable a control using control.Enabled = "false"; is not the same as applying an Attribute, like control.Attribute.Add("disabled","disabled");

If you apply the Enabled = false; from the server side, you're actually turning off the control entirely, even if it's shown on the screen! Go on, try and right click the control and select Inspector or FireFly to see it. Nothing shows up. Nothing happens, because the control does not "exist".

Yet if you apply the Attribute property the control is visible to the server and the client, and you're able to inspect it.

What I did is set up the default environment on the ASP.net side by saying the control (asp:Button in my case), has Enabled="true" (or not saying anything, as that's the default anyway).

On the server side, upon PageLoad(), I make my button Visible (in my case I also had it defaulted to visible="false"), and add the appropriate Attribute values.

btnSEND.Visible = true;
btnSEND.Attributes.Add("disabled", "disabled");

That way the button is not enabled, but it's not entirely "invisible" to the client, and by using JavaScript in another area of my program, I control when to enable it or not.

This method also avoids having to use the UseSubmitBehavior="false".

Fandango68
  • 4,461
  • 4
  • 39
  • 74
0

in page_load, you pass the java script function using Add.Atribute method with return true. first it will load the javascript function. then it will load the button click event