Solved

asp.net controls losing value after asp button click event

Posted on 2009-07-09
6
425 Views
Last Modified: 2012-05-07
I have buit a questionnaire, and have a problem with the values being cleared after the user clicks on asp button.

This is what I have:
I have 1 form 1 page divided into sections ( step1, step2, step3, etc.) each section (step) is a table enclosed in a <div id="step#" runat="server">
In the code behind aspx.vb page on page load I have

If page.ispostback() then
' do nothing
else
step2.visible = false
step3.visbile = false
etc.
etc
At the bottom of each section I have a asp button btn_continue1, btn_continue2, etc. As a user clicks on the continue button depending on step, the last step will hide and the next step will show using step#.visible = true ( in the code behind).

The problem I have is if a user fills out step2, but then hits the back button to step1 and then hits the continue button to step2; the values they previously filled out on step2 are now cleared.

Why is this happening?
0
Comment
Question by:jml12906
6 Comments
 
LVL 4

Expert Comment

by:neil_squires
ID: 24820017
I think this is because of the way the browser works.  When they are on step one and fill out the form, the browser knows what they entered.  When the go to step 2 and fill it out, the browser will know the answeres for page 1 and 2.  However, if the user uses the back arrow, the previous page is loaded, before the values to page 2 were entered.

One solution is to train your users to not use the back button.  Another way would be to set up a different data flow to your page.  Something that comes to mind is the Wizard control.  You can have them step through multiple steps with a familiar look and feel and you can only get to the next page by completing the previous.

Does this help?
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 24820033
If the user is clicking on the browser's Back button, the viewstatealso goes 'Back'. To prevent this from happening, record values to and populate controls from a session variable or alternate mechanism instead of viewstate.
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24820811
HI,

I think it might have something to do with using .visible = false. Try using a css class where the display property is set to none. e.g.:

<style>
.hidden
{
    display: none;
}
</style>

step2.cssclass = hidden
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:jml12906
ID: 24823095
I tried the control.attribute.add("Style", "Display:none") with no luck either..... I think funwithdotnet is right on the money, I started playing with the populating the controls from a session and that seems to work if lets say i have 2 tabs open 1st tab being the questionnaire and 2nd tab being google or whatever. If I close out of the 1st tab ( questionnaire ) but keep the 2nd tab open and then reopen the questionnaire the values are being populated via session.
I am storing and reloading the session as so:
on continue button click:
Dim qa1 As String = rbl_gh1.Text

' Save to session
Session("sqa1") = qa1
and then on step# load

If Page.IsPostBack() Then

rbl_gh1.Text = Session("sqa1")


End If
But I still have the problem on reloading each step section with the values using  session...Any ideas?
 
0
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 500 total points
ID: 24828658
I'm not exactly sure what the issue is, however I included some sample code, just in case.

Some other practical possibilities:
Cookies can also be handy to save values and can persist beyond a session.
A key-value pair collection can be used to store keyed values in a single session variable.

 



' After determining which step to display ...
 

If Step1.Visible Then

' populate Step1 form controls from session variables 

  If Session.Item("step1var1") IsNot Nothing Then

    Step1TextBox1.Text = Session.Item("step1var1") 

  End If

ElseIf Step2.Visible Then

  If Session.Item("step2var1") IsNot Nothing Then

    Step2TextBox1.Text = Session.Item("step1var1") 

  End If

Elseif ...
 

End If
 

' After last step, use the session values to 

' process the data from the questionnaire. 

Open in new window

0
 

Author Closing Comment

by:jml12906
ID: 31601918
Thanks for your help again! Still playing with the session variables to get it right, but I feel you are dead on the money.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 104
Get id from json Data with NewtonSoft 3 34
SSL https .net web site force redirect 3 37
Get Client IP on RDS - VB.NET 15 27
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

17 Experts available now in Live!

Get 1:1 Help Now