writing text from textbox

Chris Ara
11-19-2006, 06:33 PM
Hello I have a textbox which contents I'm using to overwrite a simple text file on my server. I load the files text into the text box , then edit it , then press a button which runs the code to overwrite the file. The problem is after I edit the text and click the button the streamwriter will write the textbox's text that was originally loaded with the page and disregard the edited version. Possibly is there a way to get the textbox's current text state or what. I don't get why it only writes what the textbox's original contents where.

Thanks for any help
Chris

Eduardo Lorenzo
11-19-2006, 06:37 PM
Hello I have a textbox which contents I'm using to overwrite a simple text file on my server. I load the files text into the text box , then edit it , then press a button which runs the code to overwrite the file. The problem is after I edit the text and click the button the streamwriter will write the textbox's text that was originally loaded with the page and disregard the edited version. Possibly is there a way to get the textbox's current text state or what. I don't get why it only writes what the textbox's original contents where.

Thanks for any help
Chris

your code is probably saving the contents of the textbox in its original state. could you pls sopy and paset the code so we can take a look. And to properties and values of your textbox just for good measure.

MikeJ
11-19-2006, 07:40 PM
Unless you are using AJAX (I doubt you are), you'll have to perform a POST request. You sound like you are using ASP.NET, which is not my strong point, but I do know you can't do this without a POST request. (Let me repeat the phrase POST request some more... POST request!)

wayneph
11-20-2006, 02:58 PM
Actually Mike, if you're using the ASP.NET controls, POST is implied. You really don't have to specify it. Visual Studio normally takes care of it for you.

However, PostBack is very important here. My guess is that you're setting the value during your page load. So when you post back to your page, the Load Function is running again, and overwritting your data.

Try adding If Not IsPostBack Then around the code that populates your controls initially. Remeber that each trip to the server is a separate Load. The Server doesn't maintain an object in memory after it's sent to the browser.

Chris Ara
11-22-2006, 11:55 AM
Here is the page load event

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Dim Reader As New IO.StreamReader(Server.MapPath(".") + "\MessageOfTheDay.txt")
TextBoxEvents.Text = Reader.ReadToEnd
Reader.Close()

End Sub

Simply just reading text from a file and putting it in a textbox

After the text is edited you click the finish button wich writes the textbox text back to the file and transfer back to a main page

Protected Sub ButtonFinish_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim Writer As New IO.StreamWriter(Server.MapPath(".") + "\MessageOfTheDay.txt")
Writer.Write(TextBoxEvents.Text)
Writer.Close()
Server.Transfer("Admin.aspx")

End Sub

The problem I found is that the writer only writes the original text that was in the textbox once the page was loaded. I test this by using settings appends to true in the writer and it just kept appending the original message to the file. You guys see any problems here ?

Thanks again

Eduardo Lorenzo
11-22-2006, 04:26 PM
OK, this is what is happening.

You have placed the

TextBoxEvents.Text = Reader.ReadToEnd

in the page load method. This means, this shall be triggered/performed everytime the page is loaded./refreshed or a postback has happened.

When you click "ButtonFinish", it triggers validation thereby calling a postback which again triggers

Reader.ReadToEnd

BEFORE

Dim Writer As New IO.StreamWriter(Server.MapPath(".") + "\MessageOfTheDay.txt")
Writer.Write(TextBoxEvents.Text)
Writer.Close()
Server.Transfer("Admin.aspx")

in a nutshell:

The textbox was redeclared/re-initiated to its original contents before Write was performed.

I hope this helps.

wayneph
11-25-2006, 08:40 AM
If Not Page.IsPostBack() Then
Dim Reader As New IO.StreamReader(Server.MapPath(".") + "\MessageOfTheDay.txt")
TextBoxEvents.Text = Reader.ReadToEnd
Reader.Close()
End If

Chris Ara
11-26-2006, 08:44 AM
thanks

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum