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
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 |
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 |
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 |
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 |
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 |
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 |
Answer / pankaj
yes.using view state we can pass value of one page to another
| Is This Answer Correct ? | 52 Yes | 82 No |
Hi, I am developing an application (quiz engine) using C# in Dot net. My problem is I am designing the selction option using radio button. So, I want to retrive the data from the database to the radiobutton option. And also please tell me the how to compare the correct answer option with Answer selected by the users. If any body knows or have done this before please Help me out. My mail id is get_rome@yahoo.co.in. Table format: Question Id Queston Option1 Option 2 Option 3 Option 4 Correct answer 1 What is ur name? My name is ….. My name …. My name …. My name …. My name is tom
Difference Between GridView And DataList
how to retrieve property settings from xml .config file.
how can i am search the data from database? just like google
What do you mean by authorization?
What is owin authentication?
please give a brief knowledge about these events ? page_render() page_prerender() page_unload page_loadcomplete page_preinit
How to write unmanaged code and how to identify whether the code is managed / unmanaged ?
2 Answers Accenture, BirlaSoft, Cogtest,
For a server control, you need to have same properties like color maxlength, size, and allowed character throughout the application. How do you handle this?
Is it possible to use two versions of assembly at the same time?If possible explain with code?
Why Global.asax is used?
How dataadapter.fill works?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)