aspx fill form data disappears when submit

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.
Who is Participating?
MlandaTConnect With a Mentor Commented:
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.
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.
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


Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

mphillip85Author Commented:
i am using <input type="text" />  and what can I use on the form to validate before I press the submit?
mphillip85Author Commented:
my dropdownlist's , both of them, do not clear only the input boxes clear after the submit has been clicked.
mphillip85Author Commented:
how can i just re-populate the input text boxes since I have the data saved in a variable(s).

for validation, have a look at these notes:
mphillip85Author Commented:
needed runar="serer" and using a textbox instead of a inputbpx
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.