tags:

views:

1074

answers:

2

i am trying to retain the value of a variable from a previous page. i believe this is the C# way to do this, but i would like the vb.net way can someone please help:

TextBox myTxt = (TextBox)Page.PreviousPage.FindControl("previousPageTextBox");
currentPageTextBox.text = myTxt.Text;

i would like to know how to code this in vb.net

i tried this:

Dim myTxt As TextBox = CType(Page.PreviousPage.FindControl("Textbox1"), TextBox) TextBox1.Text = myTxt.Text

but i got this error msg:

use the new keyword to create an object instance

+1  A: 

This should do it, also make sure you are using Server.Transfer to go between pages.. but I am sure you already know this:

Dim myTxt as TextBox = CType(Page.PreviousPage.FindControl("previousPageTextBox"), TextBox)
currentPageTextBox.text = myTxt.Text
Sean Molam
thanks so much. what is does the "Textbox" stand for? the one after previous pagetextbox
I__
why do i need to do server.transfer as opposed to response.redirect?
I__
Object reference not set to an instance of an object.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
I__
it also says "use new keyword to create an object instance"
I__
+1  A: 
dim txt as TextBox =CType( Page.PreviousPage.FindControl("previousPageTextBox"),
                           TextBox)
currentPageTextBox.Text = txt.Text

PS:- You need to set the previous page in the page where you want to find the control.

<%@ PreviousPageType VirtualPath="~/Test.aspx" %>

Also better way will be to create an function on previous page which returns the text in textbox.

 internal function TextBoxText() as string
       return myTextPage.Text
 end function

and use this on next page like this:

currentPageTextbox.Text = Page.PrevousPage.TextBoxText

let me know if this works because I've not used vb for long long time.

PPS:- It is only available if you user Server.Transfer or go to currentPage using some asp.net contorl like LinkButton from simple href links PreviousPage is null

TheVillageIdiot
"use new keyword to create an object instance"
I__
I think this article will help you more http://msdn.microsoft.com/en-us/library/6c3yckfw%28VS.80%29.aspx
TheVillageIdiot