I recently switched to ASP.NET Web Forms from Windows Forms and I have an issue with something I would not expect. In good Web Forms I could create a field inside my Form class and assign to it. So if I wanted one control to raise a flag on one event (say user button click) I could declare Boolean and assign to it from my form methods. Then I could check the state of the flag from different methods on different events.
It looks like it's not so much in Web Forms. The value of my fields (or global variables if you will) remains unchanged from the moment I initialized them.
This is probably simple thing but for me it's quite a frustrating problem. I could store my vaues in session but I don't think that it would be a right way to do it. The problem is I need to store a lot of variables since I write code for SQL interface applications. Never had a single problem in Windows Forms and in Web Forms I keep pulling my hair.
Ultimately I need to store objects to reuse them like LINQ to SQL classes objects so I would really appreciate some guidance.
I suggest you review PostBacks, Page life-cycle and how ASP.NET processes client requests . In order to better understand ASP.NET forms, you should take time to read up on the page life-cycle, events and the order in which they are fired. That way you understand why it seems your variables are not being updated.
you should also read about State management like viewstate , session ect...
put your page_load contents that you don't want to execute after button click to the following condition:
if (!IsPostBack)
{
// do something
return;
}
Related
I want to pass data directly from one view to another in the two following situations:
Our users are allowed to dock and undock a toolbar that is persistent thoughout our application. I want this state to be saved when the user navigates to another page. The toolbar is docked and undocked using jQuery.
In some cases, after a successful response from an AJAX call we have a full page refresh or a redirect. We want to display a notification to the user after the refresh / redirect (eg. Your action was successful).
My initial thought was to use cookies, specifically using jquery-cookie, but I was then wondering about using the Session[] variable. Given that our JavaScript will be in an external file I don't know if this is possible and if so does it simply complicate the issue?
Have you looked at Lawnchair?
http://brian.io/lawnchair/
Sounds pretty much like what you need. It is used to persist client side data across pages.
My application required me to store many data elements across views an sessions. Rather than pick a storage mechanism up front (mostly because management couldn't decide) I coded my app using persistence.js.
I was able to pick the precise storage mechanism later with minimal impact to my code.
I faced same type of problem.
after workaround I use a jQuery cookie and store the hidden panel id.
and check the cookie in $(document).ready(function(){}); of jQuery that which panels are hidden and after make their display:none.So after postback the cookies are persist and the panels became hidden.
It solved my problem.
You can do this by simply sending your toolbar state information through hidden fields back and forth ( means from MVC view to controller and than from controller to MVC view ).
I know its kind of stupid question, but I am beginner in C#.
I want to create start page with settings where user can set a values. How I can relay these values to other page ?
Solution that will give me someone will work with IsolatedStorage?
It does not have, but it's good to know for the future.
This question unfortunately needs a tutorial as answer. Bing has many of these:
Windows Phone 7 Jump Start Sessions (two funny guys show it all!!)
Quick Tutorial Page Navigation (involving a settings page!)
Tutorial Galore
Tips from Stackoverflow for getting started
Your task involves basic UI design, page navigation with parameters and/or data persistence.
And you are even allowed to use Google to find these.
If these settings need to be global, then add a static public member variable to the App.xaml.cs file. Set and Get as required from that property.
IsolatedStorage is useful for persisting data when your application closes, but otherwise in Memory persistence via globally accessible properties could be fine for your requirements.
Can I dynamically create controls in Silverlight without a postback to the server (even an asynchronous one). Does silverlight drag-n-drop requires postback?
I'm asking this because I've an asp.net application where I dynamically create/delete lots of controls. So after the postback I'm getting error with view state stating that the control tree doesn't match the view state tree.
Can I avoid such problems in Silverlight?
everything done in a silverlight control/application happen on the client. web service calls if any happen asynchronously. thats on of the advantages of using silverlight
Yes you can add controls dynamically to pages, without a round-trip to the server.
Drag-drop is also executed client-side.
Think of Silverlight as more like a desktop app, that talks to the server only to get/save data.
Adding controls dynamically in Silverlight is as simple as newing the appropriate control class and inserting it in the render tree (e.g. by adding it to a parent control).
Here is an example: http://asd.murven.com/blog/post/2009/10/16/Silverlight-Adding-controls-dynamically.aspx.
However, I would not suggest switching to Silverlight just to kill this bug. Only if you have a real need for a client-like application instead of a real web application. ASP.NET is suitable for dynamically creating controls as well. Please remember to initialize the control on the server during each postback. If this doesn't help, I would suggest you to submit a description of your problem with some code to help us solve it with you.
Br. Morten
The vast majority of what goes on in Silverlight involves no postback. In fact, I'd say Silverlight represents a completely different mindset. Whenever there IS a postback from Silverlight, it will almost always be asynchronous, and there is no "view state" that the server needs to worry about. In my opinion, it makes ASP.NET look like a joke when it comes to writing web applications.
When we use asp.net pages we can navigate from one page to another via more than one method.
This issue becomes more comlicated when you want to keep the history of current user navigation.
Lets say we have a form. The user will start to fill it, by adding values to one or more fields, but on my case he can create a navigation to another page before we posting-back the current page and thats ofcourse couses the controls to loose its values.
How can i keep the controls states through postbacks between asp.net pages.
Should i use the SessionState or maybe cache it for each user?
thanx
You pretty much have the choice of session state, cookies, or home grown state maintenance using a database. I typically get around this by combining all my pages into separate panels within a single .aspx. That way I can use object persistence and viewstate. Rather than navigating from page to page, I merely have to toggle the .visible property of different panels depending on my users' navigation choices.
RO
The answer mostly different in different application types. For example if you plan that your application will have a lot of people storing in session become an expensive operation, but for small applications it work good.
In all cases you can create some mechanism that will store temporary entered data into database, this approach will work everywhere, but it's slowly than in memory cache.
To give correct advice please explain a bit more about your application.
You could use Session variables or the ASP.NET Cache object to store state so that the user can resume filling in the form after navigating away from the page. However, if the issue is that the data entry is a lengthy process that the user may wish to pause and resume at a later time, give them the option to save their current progress to a database. If the user attempts to navigate away from the page, remind them that they may lose their current work and give them the option to save it.
I'm creating a multi-part web form in ASP.NET that uses Panels for the different steps, making only the Panel for the current step visible. On Step 1, I have a drop-down list that uses a Javascript function to reconfigure some of the fields in the same Panel via "onchange". Obviously, since the client-side script is only affecting the DOM, when I go to Step 2 and then back up to Step 1, the fields in Step 1 are back to their orignal configuration even though the same drop-down choice is selected.
What is a good method for storing the visual state of the Panels between steps? I considered calling the drop-down's onchange function on page load, but that seemed clunky. Thanks!
--
Thanks for the quick answers - I think I'll try out the Wizard, but the AJAX solution also sounds like fun.
You might consider an ASP.Net Wizard control for this- it will automate a lot of what you're trying to do.
I suggest you to use the MultiView control, which is let's say semantically more appropiate. And then store this data in ViewState. I have written something similar and it rocks.
I think your best bet is to maintain all of your state in one place, or don't maintain any state at all. The main problem you're having is synchronizing your client-side state with your server-side state.
Try showing/hiding your panels with javascript instead of posting back, if possible. If not, use some ajax to update values on the server-side as soon as they are selected, rather than when you click the next/previous button.
Otherwise, you could use something like ASP.Net Ajax Toolkit Tabs to help with transitions.
Hope that helps!