There
was one question always occurs, can we access viewstate one page to
other page? And answer is Yes,
“Is
it possible to access the ViewState variable of one page on another page?”
We
can access the viewstate variables across pages. This is only possible
if Cross Page Posting or Server.transfer is used to redirect the
user to other page. If Response.redirect is used, then ViewState
cannot be accessed across pages.
Here
I have two pages Default.aspx and Default2.aspx.I have one Textbox and button
control in first page (Default.aspx), after submit button it will be redirect
to other page (Default2.aspx) were u get first page viewstate
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Style="height: 26px" />
</div>
</form>
</body>
</html>
Code Behind
protected void Page_Load(object
sender, EventArgs e)
{
}
public StateBag
ReturnViewState()
{
return
ViewState;
}
protected void Button1_Click(object
sender, EventArgs e)
{
ViewState["test"]
= TextBox1.Text.ToString();
Server.Transfer("Default2.aspx");
}
Defualt2.aspx
protected void Page_Load(object
sender, EventArgs e)
{
StateBag objStateBag = null;
if
(PreviousPage != null)
{
Object objPreviousPage =
(Object)PreviousPage;
System.Reflection.MethodInfo objMethod
= objPreviousPage.GetType().GetMethod("ReturnViewState");
objStateBag =
(StateBag)objMethod.Invoke(objPreviousPage, null);
}
Response.Write("First
Page Textbox value : " + objStateBag["test"].ToString());
}
Suppose
we entered “Viewstate testing” in first page text box,
Output
First
Page Textbox value :
Viewstate testing
Download code here
Read all the information that i've given in above article. It'll give u the whole idea about it.
ReplyDeleteBest Devops Training in pune
Data science training in Bangalore
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteYour good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
ReplyDeletePython Online training
python Training in Chennai
Python training in Bangalore
This is very good content you share on this blog. it's very informative and provide me future related information.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Hats off to your presence of mind...I really enjoyed reading your blog. I really appreciate your information which you shared with us.
ReplyDeleteLooking for Training Institute in Bangalore , India. Softgen Infotech is the best one to offers 85+ computer training courses including IT software course in Bangalore, India. Also it provides placement assistance service in Bangalore for IT.
Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me. is on boom these days and offering services like these is going to help you run your business perfectly. Thanks thanks for ur efforts
ReplyDeleteData Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeleteAmazing web journal. I appreciated perusing your articles. This is really an incredible perused for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!
ReplyDeletedata scientist training and placement
Extraordinary post I should state and a debt of gratitude is in order for the data. Instruction is unquestionably a clingy subject. Be that as it may, is still among the main subjects within recent memory. I value your post and anticipate more.
ReplyDeleteYour work is very good and I appreciate you and hopping for some more informative posts
ReplyDeletedata scientist certification malaysia
This post is very simple to read and appreciate without leaving any details out. Great work!
ReplyDeletedata scientist training in hyderabad