Asp Net Disable to Display the Webpage Again Postback

PostBack is the name given to the process of submitting an ASP.Net page to the server for processing. PostBack is done if certain credentials of the page are to be checked against some sources (such as verification of username and countersign using database). This is something that a client machine is not able to accomplish and thus these details have to be 'posted back' to the server.

What is AutoPostBack Holding in ASP.NET

If we create a web Page, which consists of one or more Web Controls that are configured to use AutoPostBack (Every Web controls will have their own AutoPostBack belongings), the ASP.Net adds a special JavaScipt function to the rendered HTML Page. This part is named _doPostBack() . When Called, it triggers a PostBack, sending data dorsum to the web Server.

ASP.Net likewise adds two additional hidden input fields that are used to pass information back to the server. This information consists of ID of the Control that raised the issue and any additional data if needed. These fields will empty initially as shown below,

  1. < input blazon = "subconscious" name = "__EVENTTARGET" id = "__EVENTTARGET" value = "" />
  2. < input type = "hidden" name = "__EVENTARGUMENT" id = "__EVENTARGUMENT" value = "" />

The _doPostBack() function has the responsibility for setting these values with the appropriate information almost the event and the submitting the class. The _doPostBack() role is shown below:

  1. <script language= "text/javascript" >
  2. function  __doPostBack(eventTarget, eventArgument) {
  3. if  (!theForm.onsubmit || (theForm.onsubmit() != false )) {
  4.         theForm.__EVENTTARGET.value = eventTarget;
  5.         theForm.__EVENTARGUMENT.value = eventArgument;
  6.         theForm.submit();
  7.     }
  8. </script>

ASP.Cyberspace generates the _doPostBack() office automatically, provided at least ane control on the page uses automated postbacks.

Whatever Control that has its AutoPostBack Property gear up to true is connected to the _doPostBack() part using the onclick or onchange attributes. These attributes point what action should Browser have in response to the Client-Side javascript events onclick and onchange.

In other words, ASP.Internet automatically changes a client-side javascript event into a server-side ASP.Net event, using the _doPostBack() function equally an intermediary.

Life Cycle of a Web Page:

To work with the ASP.Net Spider web Controls events, we need a solid understanding of the web page life cycle. The post-obit actions volition be taken place when a user changes a control that has the AutoPostBack property ready to truthful :

  1. On the customer side, the JavaScript _doPostBack function is invoked, and the page is resubmitted to the server.
  2. ASP.NET re-creates the Page object using the .aspx file.
  3. ASP.Internet retrieves state information from the subconscious view state field and updates the controls accordingly.
  4. The Page.Load event is fired.
  5. The appropriate change event is fired for the control. (If more than one control has been changed, the social club of alter events is undetermined.)
  6. The Page.PreRender event fires, and the page is rendered (transformed from a set of objects to an HTML page).
  7. Finally, the Page.Unload event is fired.
  8. The new page is sent to the client.

To lookout these events in action, we can create a elementary event tracker awarding. All this application does is write a new entry to a list command every fourth dimension one of the events it'south monitoring occurs. This allows you to meet the club in which events are triggered.

event tracker in ASP.NET

Event Tracker Web Page

I have shown Markup codes and C# Codes below to make this works,

EventTracker.aspx

  1. < %@ Page Linguistic communication = "C#" AutoEventWireup = "true" CodeFile = "EventTracker.aspx.cs" Inherits = "EventTracker"  % >
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. < html xmlns = "http://www.w3.org/1999/xhtml" >
  4. < head runat = "server" >
  5. < title > Untitled Folio </ title >
  6. </ head >
  7. < body >
  8. < form id = "form1" runat = "server" >
  9. < div >
  10. < h1 > Controls being monitored for change events: </ h1 >
  11. < asp:TextBox ID = "txt" runat = "server" AutoPostBack = "true"
  12. OnTextChanged = "CtrlChanged" />
  13. < br /> < br />
  14. < asp:CheckBox ID = "chk" runat = "server" AutoPostBack = "true"
  15. OnCheckedChanged = "CtrlChanged" />
  16. < br /> < br />
  17. < asp:RadioButton ID = "opt1" runat = "server" GroupName = "Sample"
  18. AutoPostBack = "true" OnCheckedChanged = "CtrlChanged" />
  19. < asp:RadioButton ID = "opt2" runat = "server" GroupName = "Sample"
  20. AutoPostBack = "true" OnCheckedChanged = "CtrlChanged" />
  21. < h1 > List of events: </ h1 >
  22. < asp:ListBox ID = "lstEvents" runat = "server" Width = "355px"
  23. Elevation = "150px" /> < br />
  24. < br /> < br /> < br />
  25. </ div >
  26. </ form >
  27. </ body >
  28. </ html >

EventTrakcer.aspx.cs

  1. using  System;
  2. using  System.Collections;
  3. using  Arrangement.Configuration;
  4. using  System.Data;
  5. using  System.Web;
  6. using  Organisation.Web.Security;
  7. using  Organisation.Web.UI;
  8. using  System.Web.UI.HtmlControls;
  9. using  Organisation.Web.UI.WebControls;
  10. using  System.Web.UI.WebControls.WebParts;
  11. public  partial course  EventTracker : Arrangement.Spider web.UI.Page
  12. {
  13. protected void  Page_Load( object  sender, EventArgs due east)
  14.     {
  15.         Log("<< Page_Load >>" );
  16.     }
  17. protected void  Page_PreRender( object  sender, EventArgs e)
  18.     {
  19.         Log("Page_PreRender" );
  20.     }
  21. protected void  CtrlChanged(Object sender, EventArgs e)
  22.     {
  23. string  ctrlName = ((Control)sender).ID;
  24.         Log(ctrlName +" Changed" );
  25.     }
  26. individual void  Log( cord  entry)
  27.     {
  28.         lstEvents.Items.Add together(entry);
  29.         lstEvents.SelectedIndex = lstEvents.Items.Count - one;
  30.     }
  31. }

What the above Code does

The code writes to the ListBox using a private Log() method. The Log() method adds the text and automatically scrolls to the bottom of the listing each time a new entry is added, thereby ensuring that the most contempo entries remain visible.

All the change events are handled by the same method, CtrlChanged().If you look carefully at the .aspx file, you'll notice see that each input command connects its monitored upshot to the CtrlChanged() method. The event handling code in the CtrlChanged() method uses the source parameter to find out what control sent the result, and information technology incorporates that data in the log string.

The folio includes event handlers for the Page.Load and Page.PreRender events. As with all page events, these event handlers are connected by method name. That means to add the event handler for the Page.PreRender outcome, you lot simply need to add together a method named Page_PreRender(), like the one shown hither.

Thanks....

buchanansheyetterly.blogspot.com

Source: https://www.c-sharpcorner.com/uploadfile/2f73dd/what-is-postback-in-Asp-Net/

0 Response to "Asp Net Disable to Display the Webpage Again Postback"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel