Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2011, 03:44 PM
Authorized User
 
Join Date: Jan 2011
Posts: 89
Thanks: 1
Thanked 0 Times in 0 Posts
Default Saving text box text to a file

I'm really stuck on something that I'm sure is a quick fix, I'm trying to save text from a text box into a text file stored in App_Data. When I hit a button with code to save it to the file, it appears that the text property of the text box remains whatever it was previously before a user has edited it.

Could this be view state persisting what was present on the page load? How can I get it to see the newly entered text to save?

Any thoughts appreciated.
 
Old May 19th, 2011, 03:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Alex,

Probably not view state, but a PostBack issue. Maybe you're assigning the text box a value without checking for Page.IsPostBack

If that doesn't help, can you post the relavant parts of your code?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 19th, 2011, 04:39 PM
Authorized User
 
Join Date: Jan 2011
Posts: 89
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

That didnt seem to change anything, on the page load I fill the text box from the text in the file:

txtMemberNews.Text = File.ReadAllText(NewsFile)

Then on save button I save the text to the file:

Dim rdr As StreamWriter
rdr = New StreamWriter(NewsFile)
rdr.Write(txtMemberNews.Text)
rdr.Close()

Problem is I've found that txtMembers.Text contains the text as it was loaded, not the text after a user has changed it so it just saves the original text back to the file??
 
Old May 19th, 2011, 04:54 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
on the page load I fill the text box from the text in the file:
But if you don't add a check for Page.IsPostback, the Text property is updated again after the user clicked the button, and before the button's Click event fires. This resets the user data, causing the behavior you're describing.

BTW, rather than using StreamWriters and so on, you can also use System.IO.File.WriteAllText(....)

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 20th, 2011, 11:30 AM
Authorized User
 
Join Date: Jan 2011
Posts: 89
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Great thanks, I added If Not Page.IsPostBack when loading the text onto the page and now is behaving. I spent ages thinking the issue was it not being saved to the file, doh!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving data from a text box Fairies2 C# 0 February 15th, 2007 04:38 AM
Text Box .text Property Displays conflicting Valus JPOhlman Visual Basic 2005 Basics 3 August 23rd, 2006 02:39 PM
Extract text from text file & put in dropdown box tsukey Beginning PHP 5 July 20th, 2004 09:49 PM
Saving Rich Text Box Formatting snowy0 VB.NET 2002/2003 Basics 5 February 29th, 2004 02:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.