Asp Net Disable to Display the Webpage Again Postback

Background

A web application is stateless. That ways that a new example of a page is created every time when nosotros make a request to the server to get the page and subsequently the round trip our page has been lost immediately. It merely happens considering of 1 server, all the controls of the Web Page is created and after the round trip the server destroys all the instances. And then to retain the values of the controls nosotros utilize country management techniques.

State Management Techniques

They are classified into the post-obit 2 categories,

What Is View State And How It Works In ASP.NET

Now I am explaining what View State is.

View State

View State is the method to preserve the Value of the Page and Controls between circular trips. It is a Page-Level State Management technique. View State is turned on past default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.

Now I am showing yous an case of what the problem is when nosotros don't utilize view state.

Step i

Open Visual Studio 2010.

What Is View State And How It Works In ASP.NET

Step 2

Then click on "New Project" > "Spider web" >"ASP.NET Empty Spider web Application".

Stride 3

Now click on Solution Explorer.

What Is View State And How It Works In ASP.NET

Step iv

Now correct-click on the "ADD" > "New Detail" > "Web Form" and add the name of the Web Class just like I did in WebForm6.aspx.

What Is View State And How It Works In ASP.NET

Step 5

Later adding the WebForm6.aspx you will see the following code:

  1. <%@ page language= "C#"  autoeventwireup= "true"  codebehind= "WebForm6.aspx.cs"  inherits= "view_state.WebForm6"  %>
  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.     <championship></championship>
  6. </head>
  7. <trunk>
  8.     <form id="form1"  runat= "server" >
  9.     <div>
  10.         User Name:-<asp:textbox id="TextBox1"  runat= "server" ></asp:textbox>
  11.         <br />
  12.         Password  :-<asp:textbox id="TextBox2"  runat= "server" ></asp:textbox>
  13.         <br />
  14.         <asp:button id="Button1"  runat= "server"  onclick= "Button1_Click"  text= "Submit"  />
  15.         <asp:button id="Button3"  runat= "server"  onclick= "Button3_Click"  text= "Restore"  />
  16.     </div>
  17.     </grade>
  18. </body>
  19. </html>

Now write the code as in the following,

  1. public  string a, b;
  2. protected void  Button1_Click(object sender, EventArgs e)
  3. {
  4.     a = TextBox1.Text;
  5.     b = TextBox2.Text;
  6.     TextBox1.Text = TextBox2.Text = cord.Empty;
  7. }
  8. protected void  Button3_Click(object sender, EventArgs e)
  9. {
  10.     TextBox1.Text = a;
  11.     TextBox2.Text = b;
  12. }

Output

At present the output is,

What Is View State And How It Works In ASP.NET

What Is View State And How It Works In ASP.NET

It only happens because all the controls are classes and on the server all the Command Objects are created and then subsequently the round trip the Page is returned to the customer'due south browser in HTML format and the objects are destroyed at the server.

Afterward the Submit push is clicked the value of user proper name and password is submitted to the server. We cannot restore the value again because afterwards the postback the instance of the command is destroyed and on clicking of the Restore Button the server takes a new asking and the server cannot restore the value of the TextBox.

Features Of View State

These are the main features of view state,

  1. Retains the value of the Control afterwards post-back without using a session.
  2. Stores the value of Pages and Control Properties defined in the page.
  3. Creates a custom View Country Provider that lets y'all store View State Data in a SQL Server Database or in some other data store.

And now I am explaining the stored value in the View State and the remaining steps are the same as the previous.

Now write this code,

  1. protected void  Button1_Click(object sender, EventArgs e)
  2. {
  3.     ViewState["name" ] = TextBox1.Text;
  4.     ViewState["password" ] = TextBox2.Text;
  5.     TextBox1.Text = TextBox2.Text = string.Empty;
  6. }
  7. protected void  Button3_Click(object sender, EventArgs e)
  8. {
  9. if  (ViewState[ "name" ] != naught )
  10.     {
  11.         TextBox1.Text = ViewState["name" ].ToString();
  12.     }
  13. if  (ViewState[ "countersign" ] != aught )
  14.     {
  15.          TextBox2.Text = ViewState["countersign" ].ToString();
  16.     }
  17. }

Output

Now the output is,

What Is View State And How It Works In ASP.NET

What Is View State And How It Works In ASP.NET

After clicking on the Submit Button the value of user proper noun and countersign is submitted in View State and the View Country stores the value of user proper name and password during postal service-back.

After click on the Restore Push button we can get the value once more. The Value must exist retained during postal service-back and the values are stored into a base of operations 64 encoded cord and this information is then put into the View Land Hidden Field.

Data Objects That Can be Stored in View land

  1. String
  2. Boolean Value
  3. Assortment Object
  4. Assortment List Object
  5. Hash Tabular array
  6. Custom type Converters

Advantages of View State

  1. Easy to Implement.
  2. No server resources are required: The View State is contained in a structure inside the page load.
  3. Enhanced security features: It tin be encoded and compressed or Unicode implementation.

Disadvantages of View State

  1. Security Take chances: The Information of View State can be seen in the page output source direct. Yous tin manually encrypt and decrypt the contents of a Hidden Field, just It requires extra coding. If security is a concern then consider using a Server-Based state Mechanism and so that no sensitive information is sent to the client.
  2. Performance: Operation is not good if we utilize a large corporeality of information because View Land is stored in the page itself and storing a large value can cause the page to be ho-hum.
  3. Device limitation: Mobile Devices might non take the retentivity capacity to store a large amount of View State data.
  4. Information technology can shop values for the same page only.

When We Should Use View Country

  1. When the data to be stored is pocket-sized.
  2. Try to avoid secure data.

What Is View State And How It Works In ASP.NET

How to Enable and Disable View Country

You can enable and disable View State for a single control as well as at the page level as well. To turn off View State for a single control, fix the EnableViewState property of that control to false.

  1. TextBox1.EnableViewState= imitation ;

To plough off the View State for an unabridged page, we need to set EnableViewState to false of the folio directive as shown below:

  1. <%Page Linguistic communication= "C#"  EnableViewState= "imitation" ;

For enabling the same, you demand to utilize the same property but set up it to "Truthful".

View Country Security

View State Information is stored in the form of Base 64 encoding only it is not more secure, anyone tin hands suspension information technology. So in that location are the following 2 options,

  1. Using the MAC for Computing the View State Hash Value
    Generally, the larger MAC primal is used to generate a Hash Cardinal. When the key is auto-generated then ASP.Cyberspace uses SHA-1 encoding to create a larger key. Those keys must be the aforementioned for all the server. If the central is not the same and the folio is posted dorsum to a dissimilar server than the i that created the page and so the ASP.NET Page Framework raises an exception. We can enable it by using,
    1. <%Page Language= "C#"  EnableViewState= "true"   EnableViewStateMac= "truthful" ;
  2. Encryption
    Past using MAC Encoding nosotros cannot forestall the viewing of the data so to prevent the viewing, transmit the folio over SSL and encrypt the View State Data. To encrypt the data nosotros have the ViewStateEncryptionMode Property and it has the following 3 options:
    • E'er: Encrypt the data Always.
    • Never: Encrypt the data Never.
    • Auto: Encrypt if  any Control request specially for Encryption

We can enable it by using,

  1. <%Folio Language= "C#"  EnableViewState= "true ViewStateEncryptionMode=" E'er"

callendernere1937.blogspot.com

Source: https://www.c-sharpcorner.com/UploadFile/225740/what-is-view-state-and-how-it-works-in-Asp-Net53/

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