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
Can the dictionary object be created in client?s scope ?
Where sessions are stored in asp.net?
Web API uses which library for JSON serialization?
What is custom attribute? How to create?
What can you do with asp.net?
Explain the steps needed to be performed in order to create an animation in xaml?
what is AutoEventWireUp and what is the use of This property explain in details?
What is a page life cycle? What are the events in a page life cycle?
a)COM Callable Wrapper b)Runtime Callable Which one of the above is Win32 API in .Net?
What are the components of ado.net?
What are app services?
What are custom controls?
How to prepare culture-specific formatting in .net.
What is the difference between a cookie and a pixel?
6. Tell us about a time when you failed to meet a deadline. What were the repercussions?