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

What are different types of authentication techniques that are used in connection strings to connect .net applications with microsoft sql server?

0 Answers  


What is the question mark in a url?

0 Answers  


I need to download file from web server, without using save as dialogue box. Can anyone help, thanks In advance.

1 Answers  


What is asp.net ajax?

0 Answers  


How do you access individual items of an Master Page

5 Answers   TCS,






how can i deploy a asp.net webapplication.in a company so that all employees may access application on their different computers and share a single database. plz tell me the steps to do it..thanks.

1 Answers  


How do you declare delegates and are delegates and events one and the same and explain how do you declare delegates and invoke them ?

1 Answers   MMTS,


what are the attributes of sitemapnode?

3 Answers   Satyam,


What is the difference between the response.write() and response.output.write() methods?

0 Answers  


Explain the asp.net mvc request life cycle? : asp.net mvc

0 Answers  


Where is ViewState information stored?

18 Answers   DSRC, HCL,


what is the auto option in XML ?

1 Answers  


Categories