Using TextBox.Text

S

Steve S

Heres what I want to do...User types into a texbox, clicks a button, the
button saves that text to a file. The problem is that when I click the
submit button, any changes made to the textbox are lost, and it reloads what
was previously there.

Any ideas?

try
{

StreamWriter sw = new StreamWriter(sVirtualDir + @"\News.txt",false);

sw.Write(TextBox1.Text);

sw.Flush();

sw.Close();

}
 
S

Steve S

It's in the click event of the button. Below is the basic idea...


private void OnLoad(object sender, System.EventArgs e)

{

if(sender.GetType()==typeof(System.Web.UI.WebControls.TextBox))

{

TextBox temp = (TextBox)sender;

StreamReader sr = new StreamReader(sVirtualDir + @"\News.txt");

temp.Text = sr.ReadToEnd();

sr.Close();

}

}

private void Submit_Click(object sender, System.EventArgs e)

{

try

{

StreamWriter sw = new StreamWriter(sVirtualDir + @"\News.txt",false);

sw.Write(TextBox1.Text);

sw.Flush();

sw.Close();

}

catch(HttpException ex)

{

Response.Write(ex.Message);

}

}
 
J

Jim Blizzard [MSFT]

Are you pre-populating TextBox1 with something? In your first post you
said, "when I click the submit button, any changes made to the textbox are
lost, and it reloads what was previously there."

How / when does the "previous" text get put in TextBox1?

Also, what are you doing with the "temp" textbox in the page_load event
handler?

bliz
--
Jim Blizzard | http://weblogs.asp.net/jblizzard
Sr .NET Developer Specialist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.
 
S

Steve S

Sorry, Im new to ASP, but why does determining whether or not it's the first
run matter? When I click submit, is it not able to locally grab whats in
the textbox? If thats the case, can it be done somehow locally w/out having
to go back to the server...
 
S

Steve S

Ahhh, I C. Thanks

Steve

Jim Blizzard said:
Hi Steve,

You need to check to see if it's a postback. (It's a postback when the user
clicks the submit button). The Page_Load event is always fired before any
Button event handling. As a result, by the time you get to your button
click handler, you've already wiped out the text in the textbox in the page
load.

If it's a postback, don't populate the textbox... it contains the value from
the user.

If it's not a postback, populate the textbox yourself...

See the code snipit from "MS NEWS" below.

Hope this helps!
bliz

--
Jim Blizzard | http://weblogs.asp.net/jblizzard
Sr .NET Developer Specialist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.
 
J

Jim Blizzard [MSFT]

You're welcome. Thanks for using the newsgroups!
--
Jim Blizzard | http://weblogs.asp.net/jblizzard
Sr .NET Developer Evangelist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.


--------------------
 

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

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top