By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

Using TextBox.Text

P: n/a
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();

}


Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.WebContr ols.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);

}

}

"Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in message
news:3f********@news.microsoft.com...
Well, that's interesting. Where is the code (below) located (in other
words, what event handler is it in)? Can you post a simple repro?

Thanks,
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.
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:np********************@news01.bloor.is.net.ca ble.rogers.com...
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();

}


Nov 17 '05 #2

P: n/a
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.
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:zm********************@news01.bloor.is.net.ca ble.rogers.com...
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.WebContr ols.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);

}

}

"Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in message
news:3f********@news.microsoft.com...
Well, that's interesting. Where is the code (below) located (in other
words, what event handler is it in)? Can you post a simple repro?

Thanks,
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.
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:np********************@news01.bloor.is.net.ca ble.rogers.com...
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();

}



Nov 17 '05 #3

P: n/a
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...
"MS News (MS ILM)" <sq**********@hotmail.com> wrote in message
news:uA**************@tk2msftngp13.phx.gbl...
do you do anything like that in your pageload?

if ( ! Page.IsPostBack ) // First time load or Not if vb.net ... 'then'
{

}
else
{

}
it looks like you are loading the same thing over and over again even when
you post back
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:t4*********************@news02.bloor.is.net.c able.rogers.com...
The Load event populates the textbox with data in a text file, which the
user can then change, and when the submit button is pressed the changes

are
applied and saved back to the text file. However what is happening is

that
the text gets loaded fine in OnLoad, but if I delete everything from the

box
then hit submit...the data justs gets placed back in the box as if no
changes were saved.
"Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in message
news:3f******@news.microsoft.com...
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.
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:zm********************@news01.bloor.is.net.ca ble.rogers.com...
> 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.WebContr ols.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);
>
> }
>
> }
>
>
>
> "Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in message > news:3f********@news.microsoft.com...
> > Well, that's interesting. Where is the code (below) located (in other > > words, what event handler is it in)? Can you post a simple repro?
> >
> > Thanks,
> > 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. > >
> >
> > "Steve S" <hi***@rogersNOSPAM.com> wrote in message
> > news:np********************@news01.bloor.is.net.ca ble.rogers.com... > > > 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();
> > >
> > > }
> > >
> > >
> > >
> > >
> >
> >
>
>



Nov 17 '05 #4

P: n/a
Ahhh, I C. Thanks

Steve

"Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in message
news:3f********@news.microsoft.com...
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.
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:H3*********************@news02.bloor.is.net.c able.rogers.com...
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...
"MS News (MS ILM)" <sq**********@hotmail.com> wrote in message
news:uA**************@tk2msftngp13.phx.gbl...
do you do anything like that in your pageload?

if ( ! Page.IsPostBack ) // First time load or Not if vb.net ... 'then' {

}
else
{

}
it looks like you are loading the same thing over and over again even when you post back
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:t4*********************@news02.bloor.is.net.c able.rogers.com...
> The Load event populates the textbox with data in a text file, which the > user can then change, and when the submit button is pressed the changes are
> applied and saved back to the text file. However what is happening is that
> the text gets loaded fine in OnLoad, but if I delete everything from the box
> then hit submit...the data justs gets placed back in the box as if no > changes were saved.
>
>
> "Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in message > news:3f******@news.microsoft.com...
> > 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. > >
> >
> > "Steve S" <hi***@rogersNOSPAM.com> wrote in message
> > news:zm********************@news01.bloor.is.net.ca ble.rogers.com... > > > 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.WebContr ols.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);
> > >
> > > }
> > >
> > > }
> > >
> > >
> > >
> > > "Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in

message
> > > news:3f********@news.microsoft.com...
> > > > Well, that's interesting. Where is the code (below) located (in other
> > > > words, what event handler is it in)? Can you post a simple repro? > > > >
> > > > Thanks,
> > > > 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.
> > > >
> > > >
> > > > "Steve S" <hi***@rogersNOSPAM.com> wrote in message
> > > >

news:np********************@news01.bloor.is.net.ca ble.rogers.com...
> > > > > 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();
> > > > >
> > > > > }
> > > > >
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 17 '05 #5

P: n/a
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.
--------------------
From: "Steve S" <hi***@rogersNOSPAM.com>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Re: Using TextBox.Text

Ahhh, I C. Thanks

Steve

"Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in message
news:3f********@news.microsoft.com...
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.
"Steve S" <hi***@rogersNOSPAM.com> wrote in message
news:H3*********************@news02.bloor.is.net.c able.rogers.com...
> 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 whatsin > the textbox? If thats the case, can it be done somehow locally w/out

having
> to go back to the server...
>
>
> "MS News (MS ILM)" <sq**********@hotmail.com> wrote in message
> news:uA**************@tk2msftngp13.phx.gbl...
> > do you do anything like that in your pageload?
> >
> > if ( ! Page.IsPostBack ) // First time load or Not if vb.net ...'then' > > {
> >
> > }
> > else
> > {
> >
> > }
> > it looks like you are loading the same thing over and over again even

when
> > you post back
> >
> >
> > "Steve S" <hi***@rogersNOSPAM.com> wrote in message
> > news:t4*********************@news02.bloor.is.net.c able.rogers.com...
> > > The Load event populates the textbox with data in a text file, which the
> > > user can then change, and when the submit button is pressed the

changes
> > are
> > > applied and saved back to the text file. However what is happeningis > > that
> > > the text gets loaded fine in OnLoad, but if I delete everything
from
the
> > box
> > > then hit submit...the data justs gets placed back in the box as ifno > > > changes were saved.
> > >
> > >
> > > "Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote inmessage > > > news:3f******@news.microsoft.com...
> > > > Are you pre-populating TextBox1 with something? In your firstpost > 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 confersno > > > rights.
> > > > Please reply to newsgroups only, so that others may benefit.Thanks. > > > >
> > > >
> > > > "Steve S" <hi***@rogersNOSPAM.com> wrote in message
> > > >news:zm********************@news01.bloor.is.net.c able.rogers.com... > > > > > It's in the click event of the button. Below is the basicidea... > > > > >
> > > > >
> > > > > private void OnLoad(object sender, System.EventArgs e)
> > > > >
> > > > > {
> > > > >
> > > > > if(sender.GetType()==typeof(System.Web.UI.WebContr ols.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);
> > > > >
> > > > > }
> > > > >
> > > > > }
> > > > >
> > > > >
> > > > >
> > > > > "Jim Blizzard [MSFT]" <ji******@online.microsoft.com> wrote in
> message
> > > > > news:3f********@news.microsoft.com...
> > > > > > Well, that's interesting. Where is the code (below) located(in > > other
> > > > > > words, what event handler is it in)? Can you post a simple

repro?
> > > > > >
> > > > > > Thanks,
> > > > > > 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, andconfers > no
> > > > > rights.
> > > > > > Please reply to newsgroups only, so that others may benefit.
> Thanks.
> > > > > >
> > > > > >
> > > > > > "Steve S" <hi***@rogersNOSPAM.com> wrote in message
> > > > > >
> news:np********************@news01.bloor.is.net.ca ble.rogers.com...
> > > > > > > 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();
> > > > > > >
> > > > > > > }
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>




Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.