Link to home
Start Free TrialLog in
Avatar of sherbug1015
sherbug1015Flag for United States of America

asked on

No postback on button click

Does anyone know how to stop a postback when a button is clicked.

I just want the button to show another panel and not postback to the entire page.



<asp:LinkButton ID="lnkSaveAs" skinid="empty" Text="Save As" runat="server" autopostback="false" visible="false" >
</asp:LinkButton>

This is the code behind:

Protected Sub lnkSaveAs_Click(sender As Object, e As System.EventArgs) Handles lnkSaveAs.Click
        Me.panelsaveas.Visible = True
    End Sub

I am getting a postback to the entire page.  

Thanks in advance for your help.
Avatar of Deepak Lakkad
Deepak Lakkad
Flag of India image

if u r handling visiblity inside code-behind, then it will postback. to avoid this, u may have to use ajax.

i would recommend using javascript to handle visibility of the panel.(if your aim is only to show/hide the panel)

<asp:LinkButton ID="lnkSaveAs" skinid="empty" Text="Save As" runat="server" autopostback="false" visible="false" OnClientClick="showPanel();" >
</asp:LinkButton>

<script type="text/javascript">
    var pnlId = '#<%= panelsaveas.ClientID %>';

    function ShowPanel(elm) {
//if you have jquery reference , use the following
        if ($(pnlId).is("visible"))
            $(pnlId).hide();
        else
            $(pnlId).show();
   
//if you r not using jquery, use the following js
if(pnlId.style.display == 'none')
 pnlId.style.display = 'block';
else
pnlId.style.display = 'none';
}
</script>

Open in new window

If you no need any postback to happen then you can have script to done this

     <script>
function ShowPanel() {

    document.getElementById('<%=Panel1.ClientID %>').style.display = 'block'
    return false;
}
     </script>

Open in new window


<asp:LinkButton ID="LinkButton1" runat="server" [b]OnClientClick ="return ShowPanel();"[/b]>LinkButton</asp:LinkButton>
        <asp:Panel ID="Panel1" runat="server" style="display:none">
        
        <p> Hi Test </p>
        
    </asp:Panel>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roopesh Reddy
Roopesh Reddy
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's with unexpected click on bold icon in the editor..Except that u can use as such

 <script>
function ShowPanel() {

    document.getElementById('<%=Panel1.ClientID %>').style.display = 'block'
    return false;
}
     </script>

Open in new window


<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick ="return ShowPanel();">LinkButton</asp:LinkButton>
        <asp:Panel ID="Panel1" runat="server" style="display:none">
        
        <p> Hi Test </p>
        
    </asp:Panel>

Open in new window

Avatar of sherbug1015

ASKER

Thank you