简体   繁体   中英

How to avoid page refreshing after button click

I've this problem for 3 days and I couldn't find the real answer. I want that if I click a button the code will run and not to refresh all the page. For example:

ASPX code

<dx:ASPxButton ID="ASPxButton1" runat="server" Text="New" OnClick="ASPxButton1_Click"></dx:ASPxButton>
<dx:ASPxButton ID="ASPxButton2" runat="server" Text="Delete"></dx:ASPxButton>

CS code

protected void ASPxButton1_Click(object sender, EventArgs e) //new button
{
    ASPxButton2.Enabled = false;
}

That's basicly what I want to do. Click the new button and make the delete button enabled false without refreshing all the page.

What I tried:

<dx:ASPxButton ID="ASPxButton1" runat="server" Text="New" OnClick="ASPxButton1_Click" onclientClick=" return false;"></dx:ASPxButton>
<asp:button ID="Button1" runat="server" Text="New" OnClick="Button1_Click" onclientClick=" return false;"></asp:button>
<asp:button ID="Button1" runat="server" Text="New" OnClick="Button1_Click"></asp:button>
<asp:button ID="Button1" runat="server" Text="New" OnClientClick="DisableButton(); return false; "></asp:button>

function DisableButton() {
  document.getElementById("<%= ASPxButton2.ClientID %>").click();
}

Tried UpdatePanel too but I use MasterPage and that's why it doesn't work.

You should Use event.preventDefault() Javascript Function

Here is an example.

 function DisableButton(event){ event.preventDefault(); } 
 <a href="www.google.it">This goes to google!</a> <a OnClick="DisableButton(event)" href="http://www.google.it">And this one don't!</a> 

Put AutoPostBack="false" on your ASPxButton1. By default it's true.

<dx:ASPxButton ID="ASPxButton1" AutoPostBack="false" runat="server" Text="New" OnClick="ASPxButton1_Click">
</dx:ASPxButton>
<dx:ASPxButton ID="ASPxButton2" runat="server" Text="Delete"></dx:ASPxButton>

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