Asp.net store value in ViewState

Asp.net store value in ViewState

Ok. You want to store some value in ViewState and get it back. ViewState is hidden field at client side html. When you store any data in it it gets encrypted and you can see it in view source of the page.

Aspx Code file

<form id="form1" runat="server">
  <div>
      &nbsp;
      <asp:TextBox type="number" ID="TextBox1" runat="server"></asp:TextBox><asp:Button
          ID="Button1" runat="server" Text="Store ViewState" OnClick="Button1_Click" />
      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
      <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Get ViewState Value" /></div>
  </form>

Store value in view state and get it.

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  protected void Button1_Click(object sender, EventArgs e)
  {
    ViewState["UserName"] = TextBox1.Text;
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
    TextBox2.Text = (string)ViewState["UserName"];
  }
}

In above example, I have stored the TextBox1 value in ViewState[“UserName”] and on Button Click I get it at TextBox2.

Note

Do not store sensitive data like password or transaction info in ViewState as it’s not secure unless you make it secure.

When page does post back data get to and fro from server to client and cleint to server and back. This way you are consuming bandwidth and network traffic. Go Green

Jquery Dropdown Selected ValueAsp.net access html control textbox element value at code behindJQuery Set Value of Textbox or Label Control In Asp.netAsp.net Interview Questions for 2 years or 3 years experianceRESTORE cannot operate on database because it is configured for database mirroring Use ALTER DATABASE to remove mirroring if you intend to restore the databaseJQuery Hidden Field Set ValueAsp.net How To Write a Cookie and ReadGet Selected Value From Dropdownlist [JQuery]asp net image uploadASP.net monitor server How to get Jquery selected option

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.