Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > newbie problem: simple form

Reply
Thread Tools

newbie problem: simple form

 
 
Nate Hekman
Guest
Posts: n/a
 
      09-23-2004
I'm comfortable with .NET in the non-web world, but new to ASP.NET. I'm
writing a very simple form where you enter your email address, click Submit,
and we add it to a text file. But I want to prepopulate the textbox with a
value from the query string so I can direct someone to
http://www.mysite.com/subscribe.aspx?email=(E-Mail Removed) for example, and the
form would already have http://www.velocityreviews.com/forums/(E-Mail Removed) in the textbox. Simple enough, this
works:

private void Page_Load(object sender, System.EventArgs e)
{
textEmail.Text = Request.QueryString["email"];
}

Then when they click Submit, I want to write the contents of textEmail.Text
to a file. Also simple:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
StreamWriter writer = File.AppendText(@"c:\path\addresslist.txt");
writer.WriteLine(textEmail.Text);
writer.Close();
}

But when the user clicks Submit, the page reloads, textEmail.Text is always
set back to what's in the query string, and that's the value that gets
written to the file. If they got to the page and decided to type in a
different email address, the value they type in is ignored because as soon
as they hit Submit it gets switched back to the QueryString value.

There must be a common way to fix this problem. What am I missing?

Thanks in advance for your help.


Nate Hekman
Calgary, Alberta, Canada


 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      09-23-2004
Your Page_Load sets txtEmail.Text every time the page loads. So when the
page is submitted, it just replaces whatever was in that textbox. You need
to check for IsPostback property and only set the textbox if its false.

"Nate Hekman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm comfortable with .NET in the non-web world, but new to ASP.NET. I'm
> writing a very simple form where you enter your email address, click

Submit,
> and we add it to a text file. But I want to prepopulate the textbox with

a
> value from the query string so I can direct someone to
> http://www.mysite.com/subscribe.aspx?email=(E-Mail Removed) for example, and

the
> form would already have (E-Mail Removed) in the textbox. Simple enough, this
> works:
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> textEmail.Text = Request.QueryString["email"];
> }
>
> Then when they click Submit, I want to write the contents of

textEmail.Text
> to a file. Also simple:
>
> private void btnSubmit_Click(object sender, System.EventArgs e)
> {
> StreamWriter writer = File.AppendText(@"c:\path\addresslist.txt");
> writer.WriteLine(textEmail.Text);
> writer.Close();
> }
>
> But when the user clicks Submit, the page reloads, textEmail.Text is

always
> set back to what's in the query string, and that's the value that gets
> written to the file. If they got to the page and decided to type in a
> different email address, the value they type in is ignored because as soon
> as they hit Submit it gets switched back to the QueryString value.
>
> There must be a common way to fix this problem. What am I missing?
>
> Thanks in advance for your help.
>
>
> Nate Hekman
> Calgary, Alberta, Canada
>
>



 
Reply With Quote
 
 
 
 
Alan Ferrandiz [MCT]
Guest
Posts: n/a
 
      09-23-2004
Web Forms have a different lifecycle than Windows Forms... everytime you post your Web Form it is destroyed and created again. So every value you initially set for your form will be set again. What you need is to set that value only the first time the page loads and not in subsequent posts. Use the IsPostBack property of the Page Object for that purpose

Hope this helps

Alan Ferrandiz [MCT]



"Nate Hekman" <(E-Mail Removed)> escribió en el mensaje news:(E-Mail Removed)...
> I'm comfortable with .NET in the non-web world, but new to ASP.NET. I'm
> writing a very simple form where you enter your email address, click Submit,
> and we add it to a text file. But I want to prepopulate the textbox with a
> value from the query string so I can direct someone to
> http://www.mysite.com/subscribe.aspx?email=(E-Mail Removed) for example, and the
> form would already have (E-Mail Removed) in the textbox. Simple enough, this
> works:
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> textEmail.Text = Request.QueryString["email"];
> }
>
> Then when they click Submit, I want to write the contents of textEmail.Text
> to a file. Also simple:
>
> private void btnSubmit_Click(object sender, System.EventArgs e)
> {
> StreamWriter writer = File.AppendText(@"c:\path\addresslist.txt");
> writer.WriteLine(textEmail.Text);
> writer.Close();
> }
>
> But when the user clicks Submit, the page reloads, textEmail.Text is always
> set back to what's in the query string, and that's the value that gets
> written to the file. If they got to the page and decided to type in a
> different email address, the value they type in is ignored because as soon
> as they hit Submit it gets switched back to the QueryString value.
>
> There must be a common way to fix this problem. What am I missing?
>
> Thanks in advance for your help.
>
>
> Nate Hekman
> Calgary, Alberta, Canada
>
>

 
Reply With Quote
 
Nate Hekman
Guest
Posts: n/a
 
      09-23-2004
Thank you! I knew there had to be something like that!

"Marina" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Your Page_Load sets txtEmail.Text every time the page loads. So when the
> page is submitted, it just replaces whatever was in that textbox. You
> need
> to check for IsPostback property and only set the textbox if its false.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve form field value if form is EncType=multipart/form-dataForm? Li Zhang ASP .Net 4 02-27-2009 01:23 AM
html newbie needing help with simple form slinky HTML 2 05-30-2007 10:48 PM
Newbie: simple asp security form with CAPTCHA blubberpuss@gmail.com ASP General 5 02-14-2006 02:57 PM
<form>...</form> - how to supress blank space after </form> in IE? rob c Javascript 4 12-30-2005 06:10 PM
Newbie - simple cookie for form Robin Goodfellow Javascript 8 11-14-2003 07:19 PM



Advertisments