can we transfer data from one page to another page using
viewstate if so how?if not y?
Answer Posted / guest
Yes we can transfer viewstate of page to another page.
Here is the code:
Create two pages one.aspx & two.aspx
in one.aspx page
protected void Page_Load(object sender, EventArgs e)
{
ViewState["page1"] = "page1 viewstate";
Server.Transfer("two.aspx");
}
public StateBag ReturnViewState()
{
return ViewState;
}
As you can see, I have set a ViewState variable in Page Load and transfer the user to two.aspx page using the Server.transfer() method. This page also contains a method ReturnViewState() which actually returns the ViewState of this page to the calling function. The return type of the method is StateBag class.
StateBag class: This class is the primary storage mechanism for all HTML and Web server controls. It stores attribute/value pairs as strings associated with the control. It tracks changes to these attributes only after the OnInit method is executed for a page request, and saves the changes to the page’s or control’s viewstate.
IN two.aspx page
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
if (PreviourPageViewstate() != null)
{
Label1.Text =Convert.ToString(PreviourPageViewstate()["page1"]);
}
}
}
private StateBag PreviourPageViewstate()
{
StateBag returnValue = null;
if (PreviousPage != null)
{
object prePage = (object)PreviousPage;
MethodInfo objMethod = prePage.GetType().GetMethod("ReturnViewState");
return (StateBag)objMethod.Invoke(prePage, null);
}
return returnValue;
}
Whenever we use Server.transfer or Cross Page Posting, We can get the previous page object via PreviousPage property. Using Previous Page, we can find the controls of the previous page. For example, one can access Label control placed in ViewStateContainer Page in current Page.
Looking at the code, I have created a PreviousPageViewState property in this page, which returns the previous page’s ViewState. It first checks whether PreviousPage is null or not, if it’s not null, then it creates an object of the previous page.
Now using Reflection, we can invoke the method of the previous class. Using MethodInfo class, I have invoked the ReturnViewState() method of ViewStateContainer Page.
In Page_Load event, I am able to access the ViewState variable of ViewStateContainer Page. You can access all the viewstate variables set in ViewStateContainer Page.
Run the application & see the effect
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
What is an asp.net web form?
Explain the main function of razor in asp.net? : asp.net mvc
Which method is used to perform all validation at the page level?
What are the advantages of using sql stored procedures instead of adhoc sql queries in an asp.net web application?
How do u deploy your asp.net application?
What are the differences between primary foreign and unique keys?
Explain about the Class view window?
What is State Management in .Net and how many ways are there to maintain a state in .Net? What is view state?
What is Web API Routing?
What is the default authentication mode for asp.net?
What are the various session state management options provided by asp.net?
How would you turn off cookies on one page of your website?
What do you mean by authorization?
What is difference Between Authentication and authorization?
How to disable disable browser's Back button in asp.net (JavaScript)?