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">
<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>
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.
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