Avoiding PostBack OnClientClick event in asp.net

2.3k Views Asked by At

I've below asp buttons in radAjax:RadAjaxPanel control which has OnClientClick event -

<radAjax:RadAjaxPanel ID="Panel" runat="server" LoadingPanelID="Loadingpanel6">
        <table>
            <tr>
                <td>
                    <b>Sort by :</b>
                </td>
                <td>
                    <asp:Button ID="btnMostRecent" Text="Most Recent" class="button action sortcomments"
                        runat="server" OnCommand="btnMostRecent_Click" OnClientClick="(document.getElementById('hdnSortOrder')).value='date'" CommandArgument="date" />
                </td>
                <td>
                    <asp:Button ID="btnMostViewed" Text="Most Viewed" class="Comments" runat="server"
                        OnCommand="btnMostViewed_Click" OnClientClick="(document.getElementById('hdnSortOrder')).value='views'" CommandArgument="views" />
                </td>
            </tr>
        </table>
<radAjax:AjaxLoadingPanel ID="Loadingpanel6" runat="server">
        <asp:Image ID="Image1" runat="server" AlternateText="Loading" BorderWidth="0px" ImageUrl="/themes/mercolaarticle/images/loading6.gif" />
    </radAjax:AjaxLoadingPanel>
</radAjax:AjaxLoadingPanel>

When i click on any of the button the page PostBack which i want to avoid.

FYI - Buttons are in RadAjaxPanel.

Any suggestion or help.!! Thanks in advance..:)

4

There are 4 best solutions below

1
On

Return false in OnClientClick i.e. -

OnClientClick="(document.getElementById('hdnSortOrder')).value='views'; return false;"

Be aware that this means post back will never occur on that button.

0
On

If you return false from your client click function then postback will not happen.

like

 <asp:button runat="server" id="btn" OnClientClick="return myFunc();"/>

and script block is

 <script>

 function myFunc()

   {

    return false;

     }

         </script>
0
On

Just use this:

 <asp:button runat="server" id="btn" OnClientClick="myFunc(); return false;"/>

and then:

<script>

 function myFunc(){

    //Do your javascript code here

  }

</script>
0
On

if you want to avoid postback from button control just simply return false from you function.

<asp:button runat="server" id="btnTest" OnClientClick="anyFunction() return false;"/>