newbie problem: simple form

Discussion in 'ASP .Net' started by Nate Hekman, Sep 23, 2004.

  1. Nate Hekman

    Nate Hekman Guest

    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= for example, and the
    form would already have 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
    Nate Hekman, Sep 23, 2004
    #1
    1. Advertising

  2. Nate Hekman

    Marina Guest

    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" <> wrote in message
    news:...
    > 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= for example, and

    the
    > form would already have 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
    >
    >
    Marina, Sep 23, 2004
    #2
    1. Advertising

  3. 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" <> escribió en el mensaje news:...
    > 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= for example, and the
    > form would already have 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
    >
    >
    Alan Ferrandiz [MCT], Sep 23, 2004
    #3
  4. Nate Hekman

    Nate Hekman Guest

    Thank you! I knew there had to be something like that! :)

    "Marina" <> wrote in message
    news:...
    > 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, Sep 23, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Li Zhang
    Replies:
    4
    Views:
    6,042
    softip
    Feb 27, 2009
  2. slinky
    Replies:
    2
    Views:
    442
    dorayme
    May 30, 2007
  3. Replies:
    5
    Views:
    309
    Dave Anderson
    Feb 14, 2006
  4. Robin Goodfellow

    Newbie - simple cookie for form

    Robin Goodfellow, Nov 13, 2003, in forum: Javascript
    Replies:
    8
    Views:
    146
    Michael Winter
    Nov 14, 2003
  5. rob c
    Replies:
    4
    Views:
    309
    McKirahan
    Dec 30, 2005
Loading...

Share This Page