Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

Newbie - keeping state and ValidationSummary + RequiredFieldValidator control

Hi,  

I am pretty new to ASP.NET (VS.NET 2003).  I have 3 pages (aspx) which act as a survey app.  I have to problems

1.  I am losing data on controls when I go back a page.  Initially I found out that I can't have 2 submit buttons for a form, so next button submits form prev button calls onclick="history.go(-1);.  I am assuming it is because of this that I am using data, am I wrong?  What can I do to resolve this problem.  The Next button(server control) preserves data fine.  The Prev button is HTML control

2.  I have several RequiredFieldValidators in each form plus 1 ValidationSummary control.  I don't know how these work but when I fill a textbox and tab to the next (or click on another control) there is an asterick still displayed for the filled textbox and the validationsummary shows error for the filled textbox.  What I would like to achieve is as soon as textbox loses focus do it's validation and if valid don't show any error on the RequiredFieldValidator  or  ValidationSummary.

0
gbzhhu
Asked:
gbzhhu
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
gbzhhuAuthor Commented:
I may sound a bit vague here so please feel free to ask for clarification if needed

thanks
0
 
justme75025Commented:
Are you using a postback and are you binding the data to both submit buttons?  
0
 
osiris247Commented:
The validation controls should not show if the field is filled.  If not you will see the asterisk.  You can also set the control to keep focus if validation fails.

I dont follow your button problem, you say you cant have two asp:buttons on the same form??? Why not?
What data is on your 'previous button' ?

Steve
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrichmonCommented:
By using history.go(-1) you are actually just going back in the browser history.  You are not preserving any data.  It is the same as hitting back on the browser.
0
 
justme75025Commented:
You can put both buttons on the same page, but there can only be one form per page.  I think part of the problem is that you are used to doing forms in the old HTML style, where you need a form tag for each submit because individual form tags calls an individual cgi script that runs the code.  

In .net you are not calling a cgi script to run the code.  Form tags are just a piece of the page needed to run the code within the page (or code behind, because it is intimately tied to the page as if it were an include).  You will need to bind each button to a function.  In .net the button id calls the code, not the form id.  Since I am not sure what your code looks like, here's some pseudo code.  

<%@ Page language="c#"%>
<script runat=server>
void button1_Click(Object sender, EventArgs e)
{
   doSomething;
}
void button2_Click(Object sender, EventArgs e)
{
   doSomething;
}
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
     <HEAD>
         <title>test</title>
     </HEAD>
     <body>
          <form id="Form1" method="post" runat="server">
      <asp:Button id="Button1" runat="server" Text="Button1" onclick="button1_Click"></asp:Button>

      <asp:Button id="Button2" onclick="button2_Click" runat="server" Text="Button2"></asp:Button>
           
         </form>
     </body>
</HTML>

0
 
gbzhhuAuthor Commented:
Ok thank you all for the input.  Sorry I wasn't at work yesterday and so couldn't post a comment.

First of all I am new to all web development.  I am from VB 6 windows background then C# WinForms, so consider me as a newbie all things web based

justme75025
I am not sure what you mean but I have posted all my code

osiris247
The requiredFieldValidators do not show up initially (first page - survey1.aspx), when I click on the next button without filling the fields, they show up which is expected.  Then I fill the fields and click on next button which allows me to go to the next page (survey2.aspx) as expected.  Now, I click on Prev button to go back to the initial page (using javascript).  There the fields are filled but the validators are still there and so does validationsummary control showing errors

justme75025
I replaced the HTML button with a asp:button as you suggested.  I can go back to the previous page.  This has fixed the validator problem but the data I typed in the fields is gone why?


===========================
first page
===========================

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Survey1.aspx.vb" Inherits="servercontrols.dbt.co.uk.Survey1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>Survey1</title>
            <meta content="True" name="vs_snapToGrid">
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:label id="Label1" style="Z-INDEX: 116; LEFT: 24px; POSITION: absolute; TOP: 16px" runat="server"
                        Height="24" Width="97px">First Name</asp:label><INPUT style="Z-INDEX: 117; LEFT: 128px; WIDTH: 160px; POSITION: absolute; TOP: 192px; HEIGHT: 24px"
                        onclick="history.go(-1);" tabIndex="7" type="button" value="Back to home page">
                  <asp:regularexpressionvalidator id="RegularExpressionValidator3" style="Z-INDEX: 113; LEFT: 320px; POSITION: absolute; TOP: 144px"
                        runat="server" ControlToValidate="txtPhone" ValidationExpression="\b\d{11}" ErrorMessage="Your phone must be numeric and 11 digits"
                        EnableViewState="False">*</asp:regularexpressionvalidator><asp:regularexpressionvalidator id="RegularExpressionValidator2" style="Z-INDEX: 112; LEFT: 456px; POSITION: absolute; TOP: 112px"
                        runat="server" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Please enter valid email address" EnableViewState="False">*</asp:regularexpressionvalidator><asp:requiredfieldvalidator id="RequiredFieldValidator2" style="Z-INDEX: 111; LEFT: 408px; POSITION: absolute; TOP: 48px"
                        runat="server" ControlToValidate="txtlName" ErrorMessage="Please fill the last name field" EnableViewState="False">*</asp:requiredfieldvalidator><asp:textbox id="txtPhone" style="Z-INDEX: 109; LEFT: 128px; POSITION: absolute; TOP: 144px"
                        tabIndex="5" runat="server" Width="176px"></asp:textbox><asp:textbox id="txtEmail" style="Z-INDEX: 108; LEFT: 128px; POSITION: absolute; TOP: 112px"
                        tabIndex="4" runat="server" Width="312px"></asp:textbox><asp:textbox id="txtfName" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 16px" tabIndex="1"
                        runat="server" Height="24" Width="256px"></asp:textbox><asp:textbox id="txtAge" style="Z-INDEX: 107; LEFT: 128px; POSITION: absolute; TOP: 80px" tabIndex="3"
                        runat="server" Width="56px"></asp:textbox><asp:textbox id="txtlName" style="Z-INDEX: 106; LEFT: 128px; POSITION: absolute; TOP: 48px" tabIndex="2"
                        runat="server" Width="256px"></asp:textbox><asp:label id="Label7" style="Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 144px" runat="server"
                        Height="24px" Width="97px">Phone</asp:label><asp:label id="Label6" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 112px" runat="server"
                        Height="24px" Width="97px">Email:</asp:label><asp:label id="Label4" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 48px" runat="server"
                        Height="24px" Width="97px">Last Name</asp:label><asp:label id="Label3" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 80px" runat="server"
                        Height="24px" Width="97px">Age</asp:label><asp:requiredfieldvalidator id="RequiredFieldValidator1" style="Z-INDEX: 110; LEFT: 408px; POSITION: absolute; TOP: 24px"
                        runat="server" ControlToValidate="txtfName" ErrorMessage="Please fill the first name field" EnableViewState="False">*</asp:requiredfieldvalidator><asp:button id="btnNext" style="Z-INDEX: 114; LEFT: 368px; POSITION: absolute; TOP: 192px" tabIndex="6"
                        runat="server" Width="72px" Text="Next"></asp:button><asp:validationsummary id="ValidationSummary1" style="Z-INDEX: 115; LEFT: 104px; POSITION: absolute; TOP: 248px"
                        runat="server" Height="176px" Width="488px" EnableViewState="False"></asp:validationsummary>
                  <asp:RangeValidator id="RangeValidator1" style="Z-INDEX: 118; LEFT: 200px; POSITION: absolute; TOP: 80px"
                        runat="server" ControlToValidate="txtAge" ErrorMessage="Age must be between 18 and 120" MaximumValue="120"
                        MinimumValue="18" Type="Integer" EnableViewState="False">*</asp:RangeValidator></form>
      </body>
</HTML>


===========================
second page
===========================

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Survey2.aspx.vb" Inherits="servercontrols.dbt.co.uk.Survey2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>Survey2</title>
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:dropdownlist id="ddnMovies" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 40px" tabIndex="1"
                        runat="server" Width="304px">
                        <asp:ListItem Value="&lt;--Please select--&gt;">&lt;--Please select--&gt;</asp:ListItem>
                        <asp:ListItem Value="Dances with wolves">Dances with wolves</asp:ListItem>
                        <asp:ListItem Value="Mission impossible 2">Mission impossible 2</asp:ListItem>
                        <asp:ListItem Value="Rush hour">Rush hour</asp:ListItem>
                        <asp:ListItem Value="DBT dance routine!">DBT dance routine!</asp:ListItem>
                        <asp:ListItem Value="When harry met sally">When harry met sally</asp:ListItem>
                        <asp:ListItem Value="Shrek">Shrek</asp:ListItem>
                        <asp:ListItem Value="Football factory">Football factory</asp:ListItem>
                        <asp:ListItem Value="Platoon">Platoon</asp:ListItem>
                        <asp:ListItem Value="Rocky 10!">Rocky 10!</asp:ListItem>
                        <asp:ListItem Value="Jackass the movie">Jackass the movie</asp:ListItem>
                  </asp:dropdownlist><asp:button id="btnNext" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 312px" tabIndex="4"
                        runat="server" Width="74" Text="Next" Height="24"></asp:button><asp:label id="Label2" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 88px" runat="server"
                        Width="328px">Which of the following music is your favourite</asp:label><asp:label id="Label1" style="Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 16px" runat="server"
                        Width="328px">Please select your favourite film from the list below</asp:label><asp:radiobuttonlist id="optMusic" style="Z-INDEX: 105; LEFT: 24px; POSITION: absolute; TOP: 120px" tabIndex="2"
                        runat="server" Width="296px" Height="80px">
                        <asp:ListItem Value="Rock">Rock</asp:ListItem>
                        <asp:ListItem Value="Hip hop">Hip hop</asp:ListItem>
                        <asp:ListItem Value="RnB">RnB</asp:ListItem>
                        <asp:ListItem Value="Heavy metal">Heavy metal</asp:ListItem>
                  </asp:radiobuttonlist><asp:checkbox id="CheckBox1" style="Z-INDEX: 106; LEFT: 24px; POSITION: absolute; TOP: 240px"
                        tabIndex="3" runat="server" Width="360px" Text="Check if you would like to receive further information"></asp:checkbox><asp:requiredfieldvalidator id="RequiredFieldValidator1" style="Z-INDEX: 107; LEFT: 352px; POSITION: absolute; TOP: 152px"
                        runat="server" Width="344px" ControlToValidate="optMusic" ErrorMessage="You must select one of the given options"></asp:requiredfieldvalidator><INPUT style="Z-INDEX: 108; LEFT: 40px; WIDTH: 74px; POSITION: absolute; TOP: 312px" onclick="history.go(-1);"
                        tabIndex="5" type="button" value="Prev">
            </form>
      </body>
</HTML>


0
 
gbzhhuAuthor Commented:
Hello!!

Come on folks I only got 1 last issue.  The Prev button click code is

Response.Redirect("Survey1.aspx")

We have been to this page ("Survey1.aspx") just before and filled some textboxes.  Now when we go back to it the textboxes are empty!!!  EnableViewState="True" to page as well as web.config.  It can't be machine.config as other apps are holding state OK.  Please help

Thanks
0
 
osiris247Commented:
That will reload the page....As far as i am aware viewstate is for postbacks only.

Steve
0
 
gbzhhuAuthor Commented:
That is starting to make sense because in databinding we only bind controls first ;page load or when data changes but not in postbacks.  Does this mean that there is no way for server controls to hold state between page transitions.  I am a windows programmer, so bear with me please
0
 
osiris247Commented:
As far as i am aware the only way you could do something like that is to use session variables or similar.

Not sure i follow what you mean <<databinding we only bind controls first ;page load or when data changes but not in postbacks>>  Your data will be saved in viewstate when posting back so you no need to rebind every load.  Beware of the view state on the gridview... i have just fixed a huge performance issue which was the result of a massive viewstate, i am not totally sure on the viewstate thing and the best way to handle it.....I too come from a Windows App background.

Hope this helps.
Steve
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now