简体   繁体   中英

OnClick not working when i use both Onclick & OnClientClick

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"/>

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"/>

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.

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

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" .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM