newbie problem: simple form

N

Nate Hekman

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/[email protected] for example, and the
form would already have (e-mail address 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
 
M

Marina

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.
 
A

Alan Ferrandiz [MCT]

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]
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top