changing the viewstate value

Posted by Mahesh_Chs under Interview Questions on 2/16/2013 | Points: 10 | Views : 3750 | Status : [Member] | Replies : 5
hi

If View State value is "X" and I have changed it to "Y" in Page_Load then what will be the final value of View State?




Responses

Posted by: Santhi on: 2/19/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Can you explain this with a scenario?
Actually, i tried a scenario like this.
I have two controls such as label control and a button control.
I will set the value for the viewstate in pageload as y
and on the button click i will change the value as x.
Now, i am using a label to display the value of the viewstate.
On the button click i get the label value as x..

Thanks & Regards,
V. Santhi

Thanks & Regards,
Santhi .V

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sireesha on: 2/19/2013 [Member] Starter | Points: 25

Up
0
Down
hi

i have declared viewstate value to x in source part like <input type="hidden" name="_VIEWSTATE" id="_VIEWSTATE" value="x" />
and in the pageload i set value to y like
ViewState["Viewstate"] = "y";
Label1.Text = ViewState["Viewstate"].ToString(); and the
final value of View State is "y"


A.Sireesha

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sireesha on: 2/19/2013 [Member] Starter | Points: 25

Up
0
Down
protected void Page_Load(object sender, EventArgs e)
{
ViewState["Viewstate"] = "y";
Label1.Text = ViewState["Viewstate"].ToString();

}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Viewstate"] = "x";

Label1.Text = ViewState["Viewstate"].ToString();

}
first in the page load i will get viewstate value as "y" and after button click i will get value as "x"

A.Sireesha

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: saichaitunet-23643 on: 9/5/2013 [Member] Starter | Points: 25

Up
0
Down
protected void Page_Load(object sender, EventArgs e)
{
if(ViewState["id"] !=null)
{
Response.Write("old value "+ViewState["id"].ToString() );
}
ViewState["id"] = "Y";
Label1.Text ="current value "+ ViewState["id"].ToString();

}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["id"] = "X";
Label1.Text = "current value " + ViewState["id"].ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
//dont do anything
}

First in page load observe the result and then click button 1 and button 2 and see the result.
//output
current value Y
old value Y current value X // Button 1 click
old value X current value Y // Button 2 click


Chaitanya

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/5/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
The value of viewstate will be "Y"

See the below link: http://www.c-sharpcorner.com/UploadFile/8911c4/page-life-cycle-with-examples-in-Asp-Net/

Happy Coding

Mahesh_Chs, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response