Is it possible to read a textbox control from another web form?

P

Paul Hale

Hi,

I have two web forms (lets call them webform1 and webform2) that run
as part of the same web application.

Webform1 contains a text box. I would like to pop up a new browser
window (containing webform2) and read the contents of the text box in
webform1 and display in webform2.

Is it possible to do this without posting the contents in a form or
over http or setting up a cache.

Ie - Something like declaring the text box public in webform1 and
then...

(Code in webform 2)

dim objWebForm1 as webform1)
response.write(objWebForm1.mytextbox.text)

Hopefully you get what im trying to do!

Thanks for any help!

Paul.
 
S

Stephan Bour

One ugly way to do it is to use Response.Redirect and embed the textbox.Text
string into the URL for the webform 2.

A more elegant way is to use Session State. Here is an example that assumes
the ID of your text box is TextBox (code behind in C#).

if (Session["Form1"] == null) {
Form1 = new FormEntry();
Session["Form1"] = Form1;
}
else {
Form1 = (FormEntry) Session["Form1"];
}

public class FormEntry {
private String mTextBox = "";

public String TextBox {
get {
return mTextBox;
}
set {
mTextBox = value
}
}

Then insert in your form2 HTML:
<%= Form1.TextBox %>
 
K

Kevin Spencer

Is it possible to do this without posting the contents in a form or
over http or setting up a cache.

It is possible. It requires a number of things to be true. when a JavaScript
in an HTML document uses the window.open() method to open a new browser
instance, the function returns a handle to the window opened. This enables
the HTML documents in both windows to "talk to" each other. In the child
window, the parent window is accessible as "opener." In the parent window,
the child window is accessible as the handle created when opening the
window.

So, for example, if the first window has a textbox in it with the name "foo"
the child window can grab it's value thusly:

var s = opener.forms[0].foo.value;

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top