An ASP.NET page should not put the entire page back in Submit

I have an ASP.NET page with a submit button. When I click the button, my code starts and then the page reloads / refreshes ... the whole page goes back.

What should I use or do to prevent page reloading? AJAX? How to do it?

Code examples would be great!

+4
source share
3 answers

Check the code below with AJAX and without AJAX

Case 1 with AJAX

In this we added the following controls

1. Script Manager 2. Ajax Update Panel with Trigger and Content Template 3. Button 

In this case, you will notice that when you click the button, the controls outside the Update Panel will not be updated. This means that the controls inside the Update Panel will be updated when the button is clicked. This means that the full page will not be refreshed. I hope this fulfills your requirement ....


Exit

The date outside the update panel will not be updated, and the date inside the update panel will only be updated because it is located inside the update panel, so the full page will not be updated.


Source

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%=DateTime.Now %> <br /> <asp:ScriptManager ID="scr" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="Upd" runat="server" UpdateMode="Conditional"> <ContentTemplate> <%=DateTime.Now %> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="btn" EventName="Click" /> </Triggers> </asp:UpdatePanel> <asp:Button ID="btn" runat="server" Text="Click Me" /> </div> </form> </body> </html> 

Case 2 without AJAX

In the example below, the date will also be updated, but this time page will be completely updated due to the lack of Update Panel <% @Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default4.aspx.cs" Inherits = "Default4"%>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%=DateTime.Now %> <asp:Button ID="btn" runat="server" Text="Click Me" /> </div> </form> </body> </html> 

I hope this article helps you understand the quick basics.

+3
source

If you do not want to reload the page, you should take a look at the UpdatePanel control: http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx

+2
source

If you want to avoid re-running the code, you can do page_load in your event

 If(!page.IsPostBack()) { your code here } 

and ajax to avoid reloading in jquery with an example here

or with asp ajax panel

+1
source

Source: https://habr.com/ru/post/1397100/


All Articles