Post data to different ASP.NET WebForm - c#

In my ASP.NET WebForms application, I have a WebForm that contains an UpdatePanel and multiple views used for a wizard like interface.
At the end of the wizard, the user has an option of moving to another page by clicking a button. This new web page needs about 5 values from controls in the previous page.
What is the simplest way to do this? (Edit: ONLY using an HTTP POST with data - this is a requirement as I would use database/session otherwise)
I tried using cross-page posting with no luck, possibly because of my update panel and multiple views?
I tried using Server.Transfer, but this also breaks because of the update panel.
Important:
Data has to be sent via HTTP POST - The data can't be stored anywhere
The scenario can't be changed. I can't put everything on the same page

The simplest way to do this is by putting those values in the session object.

You could make a class that describes the data that you need to display on the redirected page. Instatiate a new instance of that at the time the user is filling out the wizard data, populate the new classes' object with the information you need, then add it to the session in the button_Click event before page redirection. On the page you are redirected to, grab the Session object, put it into a variable and extract the data you need.

I recommend you combine all the relevant pages into one; hiding panels that are not in play. ASP.NET will maintain the values of all the controls for you from post to post. The Viewstate was designed for sceneries like you describe. To keep to Viewstate size to a minimum, make sure you fill lookup values for drop-down controls in their "Init" methods.
You don't want to use the session state. The last thing you want is for the users to loose their data from previous pages because they took too long to answer.

If they're moving to another page in the solution, you have a few options.
ViewState - The ViewState is sent with the page delivery. It resides in the HTML, but is encrypted so no one can see the information. Depending on the size of the information, your page size could get rather large.
Session - This puts the information client-side via cookies.
Query String - Using the URI. This should only be used if it's non-sensitive information and if you don't want a user to be able to link back to the same action again.

Related

When might you choose to use ViewState over the Session?

I have a doubt why we use ViewState because Session can keep its state throughout the application. Then why is the need for ViewState which can keep the state only in a single Page?
First Thing first is to know what is
View State
Is information of a particular page in webforms. It is stored in hidden field. It is used to maintain that the page remembers what he did on it the last time.
Session
Is information that is related to a specific session.i.e. certain browser
now coming to your question
When to use and not use is a cluster F......
as all have their specific pros and cons specially ViewSate is more a con I guess since the MVC was introduced.
One can use viewstate to store values to remember when its page have a post back as every time, when a page is in a post back stage it removes all the values of user controls .i.e. like Label and TextBox in asp.net . So to keep the value you've toEnableViewState property to true
Session on the other hand is used when you wish to move from page to page, to keep some specific value for all the pages. For example: one keeps UserId in session so he can visit all the page which need some kind ofauthorization or authentication
Update
As you've changed you question while I was type:
In that case
Sessions is for specific time whereas Viewstate does not as Viewstate is a temporary storage mechanism. Controls that use viewstate have their state rendered into the html page as hidden input.
Hope this will help you in any way, and don't worry about vote down you'll get use to of it
If the user opens several instances of the application in different tabs of the browser, the data stored in Session will be shared by all of them (unless something special is done to avoid that). One instance modifying a value in a Session variable will overwrite the same variable saved by the other instances, causing a mixup of the data. Using ViewState ensures that each instance is not affected by the others.

JQuery array in MVC

This is a very general question. I am using MVC on the back in and JQuery for the front end. I have a select list of values for the user to choose from. I want the user to select the values on at a time by clicking the add button. But this truly does not save them this only stores them in an array. Then when the user clicks the save button i would pass the array to MVC controller method to do the database saves. My problem is that my array is not keeping values. i declare it global but if the page get refreshed or i call a method to filer the list by keyword value the array looses the contents that it had. How to i have an array in JQuery that keeps it value even after a post back?
Normally you shouldn't store unsaved data in javascript array and do postbacks. Obviously if the page is refreshed the data is being lost. So you should choose from two options:
1) You do it on the clientside. So you still have your javascript array, but you don't refresh your page until you are ready to save the data. In this case all the filtering and other stuff you mentioned should be done via AJAX calls from the clientside.
2) You do it on the serverside. So when you have the javascript array and you want to do a serverside postback with complete reload of the page, then you need to push your array to the serverside, but not save it into the database until you are ready. You can temporarily store it Session for example or other collections that persist data between different post backs.
This is high level explanation, I hope you can dig more.
EDIT:
James suggested another valid option of using browser storage of cookies. However storage is not supported in some older versions of different browsers. And I don't feel like this is the task you should use cookies for. It feels like work around to bad architecture.

How to update labels in the same asp.net page by C#

I wrote a page which user can input a name and get some infos from the database. In the .cs file I got the texts from the database and assigned them to the labels and in the debugging mode, the labels did change their texts. BUT I don't know how to update them in the same page. I used some ways to update the page, but it updated the whole page and display nothing in the labels.
How can I achieve this function?
I google it and found the AJAX is a good way, but it's an emergency i have no time to learn AJAX?
does someone have good idea to help me solve it?
Thanks a lot!
What you're talking about is a postback. When the page posts back to the server, the page is refreshed.
If you want to set the labels and avoid losing the data with a postback, you can do so through an ajax call in your JavaScript code, you can set hidden fields or you can set the values in the Session object (not the best idea). There are numerous ways around this; you just have to pick one.
Do some reading on ajax (it's not as hard as you think). You can call the server through ajax, which will get the data from the Db and return it to your JavaScript as JSON. You can then use that to fill your labels.
You may want to look into an UpdatePanel as well. They aren't the fastest solution available, but they are very easy to implement.

Advanced method for retrieving data from the database

In my site, I'm using update panel in the master page. Half of my web page will retrieve the data from the database in dynamic. As, my update panel is in master page(with ajax loader), it is taking much time for every event. Is there any other advanced method to get the data from the database instead of using update panel.. Or any other idea instead of this?
I think you need to profile to see where the bottleneck is; I expect you have unnecessary code going on in your aspx that isn't really needed for the UpdatePanel.
Personally, I wouldn't use UpdatePanel now; I'd use a simple (but separate) page (or route, if using MVC) that just does the code needed for this work, and use jQuery to load it.
I think you want to load your web page from database table which contains html. If it is so then the best idea would be to create controls for headers/ footers or repeated sections and use caching which would reduce the load time of your web pages.
You could easily cache your controls. You can check here http://msdn.microsoft.com/en-us/library/aa478965.aspx
Happy coding

Can i 'hold' POST data in ASP.NET, so i can verify with Captcha?

I have a complex page with maybe a dozen POST element and a file upload (non ajax ATM).
I have a form with a description, if it causes akismet to find it as spam i would like the user to be informed and either hit back on their browser to try again or to hold call POST data so the user can fill in a reCaptcha to bypass the spam marking.
How do i hold the POST data? i have no idea how to redirect the user to this captcha page and to keep all the post data. It would be preferable if it was generic and i didnt need to copy the post and get data by hand.
One solution could be to create hidden form fields with all received data. This is easy to do it generically, just iterate through all $_POST elements and create a new hidden field for each one.
Obviously you must create a valid form.
Once this new page is loaded you should redirect to desired location.

Categories

Resources