View Full Version : text grab and outout in asp pages

03-05-2007, 12:45 PM
I am building a website in asp.net and want to know how to take text from a text box on say the home page and output that text in a text box on another page. can anybody help please?

03-05-2007, 04:35 PM
Several options. Depends on your goal.
-- Cross page post back
-- contaxt state bag
-- Session state bag
-- Application state bag
-- cookie

03-06-2007, 03:35 PM
the text thats dispalyed back out in a different text box on a different page is just used to reference a user in the database. what do you suggest is the best option to just take it from one text box on one page and dipslay it in another text box on another page?

03-06-2007, 11:29 PM
Are the two pages connected by a link or by a form submit? Or are they merely two pages on the site, with no particular connection?
Confirm this is .NET and not classic asp too please.

03-07-2007, 11:18 AM
yes sorry it is .net and the pages are connected by a Response.Redirect. how it works is if the login is succesfull, the username is copied from the username text box on the sign in form to a text box on the user menu.

03-07-2007, 03:43 PM
Ah, if it's the username, you may want it in more places later. You can go ahead and store something like that in the Session.
You may want to check out Server.Transfer instead of Response.Redirect as well. You keep all your header info with Transfer and it doesn't ding the browser. Response.Redirect actually sends a 302 Redirect to the browser (which tends to lose cookie info and whatnot and sometimes even the session data if it is set to use cookies instead of the URL).
Transfer stays on the server and you keep cookies and other header data.

03-07-2007, 08:43 PM
thanks for your help so far i appreciate it very much, but i am not a very experienced programmer and was wondering if you could give me the basic building blocks for the session transfer as i do not know where to start, thanks again

03-07-2007, 09:07 PM
Does this help?
It's from my application, actually. I use context state bag here, which might work for you, too. It's like Session, but only works over the Request, so Transfer does pass it.

if (userParams.Count == 0) Context.Items["UserParams"] = null;
else Context.Items["UserParams"] = userParams;
Context.Items["Report"] = report;
ViewState["Report"] = report;

Server.Transfer("ReportRunReport.aspx?report_id=" + report.ReportID);

03-07-2007, 11:39 PM
There are alot of ways to move data across pages. If you are using a low cost hosting company then I would not use sessions. The reason for this is they use web farms. What happens is as a server get loaded up they will move some of the load to other servers. When they move you not if, you will lose all sessions on that server. Most of these hosting companies will even warn you to not use sessions for this reason.

There are querystrings but the problem with querystrings is the user can see what is in the string and can mess with it. Cookies gives you a little more control but there again with the help of free broswer plugins the user can see and play with the cookies.

When I was with the low cost hosting companies I would uses a combination of sessions, cookies, and database. To tell who was who and what they can or can not do. I used the cookies to refresh my sessions if the web farms has droped the session. I would use the session to make sure they were not messing with the cookie. So anyway you can see that keeping state with the server can be a pain.

If all you want to do is move a small amount of text from a textbox on one page to a textbox on another page. You can use a form using the post method or a cookie.

Find yourself a good book or online tutorial. Here are some links to get you started. The first link will show you how the write and call cookies, the next link is a good online free tutorial.

Hope this helps.

03-08-2007, 09:12 PM
thanks for the advice. just to say, my project is only for a degree project and it will not be going live where a number of users will be using it. it is merely for a project presentation where i have to show the program working. thanks for the help, i`l try the tutorilas out now

03-08-2007, 09:43 PM
thanks, i follwed the tutoril and it worked perfectly using the code :

to write a cookie:

Response.Cookies("userName").Value = "" + txtUsername.text
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)

Dim aCookie As New HttpCookie("lastVisit")
aCookie.Value = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddDays(1)

and to read the cookie:

If Not Request.Cookies("userName") Is Nothing Then
Label1.Text = Server.HtmlEncode(Request.Cookies("userName").Value)
End If

If Not Request.Cookies("userName") Is Nothing Then
Dim aCookie As HttpCookie = Request.Cookies("userName")
Label1.Text = Server.HtmlEncode(aCookie.Value)
End If

thanks for all the help