can we transfer data from one page to another page using
viewstate if so how?if not y?

Answers were Sorted based on User's Feedback



can we transfer data from one page to another page using viewstate if so how?if not y?..

Answer / 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

can we transfer data from one page to another page using viewstate if so how?if not y?..

Answer / k

no we can not do it like how we use session.
u store ur value in any contol find that control value from
current page.

TextBox txBox = (TextBox)PreviousPage.FindControl
("txtPreviousTest");
string urTextBox=txBox.Text;

Is This Answer Correct ?    0 Yes 2 No

can we transfer data from one page to another page using viewstate if so how?if not y?..

Answer / kranthi

Yes it is possibel!!!!!!!!!!!!
in pag1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["name"] = txtTest.Text;

Server.Transfer("Default2.aspx");

}

In page2,aspx load event

protected void Page_Load(object sender, EventArgs e)
{
Page Poster = this.PreviousPage;

TextBox txtNewTest = (TextBox)Poster.FindControl
("txtTest");

string sDisplay = txtNewTest.Text.ToString();

Response.Write(sDisplay);



}

Is This Answer Correct ?    0 Yes 2 No

can we transfer data from one page to another page using viewstate if so how?if not y?..

Answer / anish.p.r

no we cant transfer data from one page to another page
using viewstate.
becouse in viewstate we can store only page level
information

Is This Answer Correct ?    3 Yes 6 No

can we transfer data from one page to another page using viewstate if so how?if not y?..

Answer / shravankumar

i feel it's possible to pass values from one page to
another using view state

Just try this :

Hold the values in viewstate which you want to transfer to
another page , in the next page where you want to pass the
values ,

In page_load
Access the values hold in view state

Just try this once :

Is This Answer Correct ?    2 Yes 6 No

can we transfer data from one page to another page using viewstate if so how?if not y?..

Answer / kuldeep khaware

Yes. We can transfer data one page two an other page
through View state.
Server.Transfer("secondpage.aspx view=textbox");

View state is page specific; it contains information about
controls embedded on theparticular page. resolves this by
embedding a hidden input field

" KULDEEP KHAWARE "

Is This Answer Correct ?    3 Yes 13 No

can we transfer data from one page to another page using viewstate if so how?if not y?..

Answer / pankaj

yes.using view state we can pass value of one page to another

Is This Answer Correct ?    52 Yes 82 No

Post New Answer

More ASP.NET Interview Questions

Less than one page, how many windows will you be able to maintain?

0 Answers   Atos Origin,


What is autopost?

0 Answers  


What is an example of an application service provider?

0 Answers  


Which control would you use if you needed to make sure the values in two different controls matched?

4 Answers   Siebel Systems,


What is _dopostback in asp net?

0 Answers  


What setting must be added in the configuration file to deny a particular user from accessing the secured resources?

0 Answers  


What is strong-typing versus weak-typing?

0 Answers  


When was asp.net released?

0 Answers  


What are HTTP handlers in ASP.NET?

0 Answers   MindCracker,


Can you explain the basic use of dataview?

0 Answers  


What is asp.net globalization?

0 Answers  


Distinguish between Server-side and Client-side code?

0 Answers   Siebel,


Categories