aspx fill form data disappears when submit

Posted on 2011-04-20
Last Modified: 2012-05-11
I have a submit form that has data filled out but if there is an error in one of the fields or incorrect data when the submit button is pressed, all the data in the form disappears.  I would like to re-populate the form data after the submit when the code catches an error so the customer does not have to re-type all the data over again.  Thhis is an ASPX form with a standard submit button not a form submit button being used.
Question by:mphillip85
    LVL 6

    Expert Comment

    It sounds like it is doing a post back or is reloading the page after the error happens..

    Your best bet is to prevent the error from happening by adding some kind of verification schema.
    LVL 16

    Expert Comment

    Sounds like you may have EnablePostback set to false on your Page, Form or Controls.  Or that the form fields you have used are not actually server-side controls.

    Look through your ASPX page's HTML; if the form controls are akin to
    <input ... />

    Open in new window

    try adding a unique id attribute and adding runat="server", or changing them to their ASP.NET equivelents (e.g.
    <input type="text" />

    Open in new window

    <asp:TextBox />

    Open in new window



    Author Comment

    i am using <input type="text" />  and what can I use on the form to validate before I press the submit?

    Author Comment

    my dropdownlist's , both of them, do not clear only the input boxes clear after the submit has been clicked.

    Author Comment

    how can i just re-populate the input text boxes since I have the data saved in a variable(s).

    LVL 30

    Accepted Solution

    Since you are using ASP.NET, you should probably think of using ASP.NET Server Controls as opposed to <input type="text" /> controls. The server controls have the great advantage that they automatically maintain state i.e. they remember data across "postbacks". Using <input type="text" /> reverts back to the Classic ASP style where you would need to use the Request.Form collection to write the user's entries back into the form. A simple example is this:
    <%@ Page Language="vb" AutoEventWireup="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
    <script runat="server" language="vb">
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If TextBox1.Text.ToUpper <> "TEST" Then
                Label1.Text = "Please enter valid firstname"
                Label1.Text = "Writing to database or something... " & Now
            End If
        End Sub
        <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
        <br />
        <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>

    Open in new window

    Just save that code to an ASPX page, say mytestfile.aspx, put it maybe in your \Inetpub\wwwroot and browse for that in your browser: http://localhost/mytestfile.aspx

    You will see that it will remember the values supplied to the form on the client side. note: enter "test" in firstname for a successful validation.

    hope that helps.
    LVL 30

    Expert Comment

    for validation, have a look at these notes:

    Author Closing Comment

    needed runar="serer" and using a textbox instead of a inputbpx

    Expert Comment

    As a comment I will tell this.
    I had a user where everything disapeared when he submitted the form. After some investigation I found the reason. He copied some text from an email and by doing that he got some linefeeds and maybe some other special characters along with the text (I tried to paste it into Notepad and there I could see it) and that made the form data disapear "out in the blue" when submitted.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now