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 is different authentication mechanisms used in ASP.NET?

0 Answers   Amdocs,


What is the behavior of a Web browser when it receives an invalid element?

0 Answers   MindCracker,


Trigger syntax in sql2000

1 Answers   Wipro,


What permissions do asp.net applications posses by default?

0 Answers  


How can you access the properties and controls of master pages from content pages?

0 Answers  






What is asp.net and how it works?

0 Answers  


What are the features of asp net?

0 Answers  


How to remove themes from certain controls?

2 Answers  


To bind columns manually which tags do you need to add within the asp:datagrid ?

0 Answers   Siebel,


What is css in asp.net?

0 Answers  


Explain the main function of url routing system in asp.net mvc? : asp.net mvc

0 Answers  


Where would you use an ihttpmodule, and what are the limitations of any?

0 Answers  


Categories