Mixim
Mixim

Reputation: 972

User web control handle OnClientClick event

I have next user web control:

<script type="text/javascript">
    function Myfunction() {
        alert('Hello');
    }
</script>
    <asp:Panel Id="problemSolving_panel"    runat="server">
    <asp:Button ID="Button1" runat="server" Text="Hello world" OnClientClick="javascript:Myfunction();return false;"/>
    <fieldset class="fieldsetsElement">
        <legend class="legendsElement">
            <input Id="callToAbonent"   runat="server" type="checkbox" onchange ="ChangeEnable(this.checked, 'callToAbonentDiv');"/>
            <label for="callToAbonent">Call to abonent:</label>
        </legend>
        <div Id="callToAbonentDiv">
            <asp:CheckBox   Id="isCallToAbonentFailed"          runat="server" Text="Call to abonent failed"   Enabled="false"/>
            <br />
            <asp:Table runat="server" Enabled="false">
                <asp:TableRow>
                    <asp:TableCell>
                        <asp:TextBox    Id="datesOfCallTrying"              runat="server"                             />
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:Button ID="Button2" runat="server" Text="Hello world" OnClientClick="javascript:Myfunction();return true;"/>
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
            <br />
            <asp:Button     Id="addInfoAboutCall_button"  runat="server" Text="Add info about call" Enabled="false" OnClick="OnAddInfoAboutCall"/>
        </div>
    </fieldset>
</asp:Panel>

When I click to Button1, OnClientClick handle this event, display me 'Hello' without any postback to server, but when I click to Button2 (copy-paste of Button1), OnClientClick not handle click event and server server get postback. I can not understand why it happend, because I donn't need postback. Can anybody tell me what's wrong in my code?

Upvotes: 2

Views: 1016

Answers (2)

Adil
Adil

Reputation: 148180

You are not getting postback from button1 because you are return false from OnClientClick from button1 and you get postback from button2 because you return true from button2.

  • If you return true from javascript handler you will get postback.

  • If you return false from javascript handler you will Not get postback.

The button1 return false

 <asp:Button ID="Button1" runat="server" Text="Hello world" 
 OnClientClick="javascript:Myfunction();return false;"/>

The button2 return true

<asp:Button ID="Button2" runat="server" Text="Hello world" 
OnClientClick="javascript:Myfunction();return true;"/> 

You can return true or false from the event handler function

function Myfunction()                   
{
    //Your code
    if(SomeCondition)
       return true;
    else
       return false;
}

Upvotes: 2

scm8jet
scm8jet

Reputation: 66

Perhaps amend your onclientlcick and function

OnClientClick="javascript:return Myfunction();"

function Myfunction() {
        alert('Hello');
        return false;
    }

Upvotes: 0

Related Questions