I have two pages called test1.aspx and test2.aspx.
In the test1.aspx there are some hidden fields.
I am first storing some value to all hidden fields and redirecting to another page called test2.aspx. when I am coming back to test1.aspx, I am losing the values of hidden fields.
How can i make the values of hidden field as it is even after coming back from another page. I can't use session or any other server side state management techniques.
Are you using Server.Transfer or Reponse.Redirect to navigate to the next page?
Reponse.Redirect destroys the history of the current page in the web server, while Server.Transfer keep the history.
Try: Server.Transfer("test1.aspx")
Enjoy ;-)
Related
I have a hidden filed in my master page and there are several pages using this master page.
in first page I will change the hidden filed value and it works fine, but when I change the page that hidden filed will be reset. How can I keep its value on page changing?
There are several ways to go about this, you could use cookies, or (how I would do it), you could store that value in a session object instead then you can access it anywhere in your application.
But if you want to keep using a hidden field in a Master Page, this has been answered several times here before.
Here's a good example.
https://stackoverflow.com/a/10348525/3299157
and here is an example showing how to use session objects:
https://stackoverflow.com/a/5282694/3299157
I am confused in how to use ViewState in C#, for example what is the benefit of using:
ViewState["VST"]="Value1";
Lable1.Text= ViewState["VST"].ToString();
Whereas I can use:
string container= "Value1";
Lable1.Text= container;
Your ViewState consists of variables that are kept with the post-backs of your page, because they are sent to the client and the client sends them back with the entire page.
Hence, if you do:
string container= "Value1";
Lable1.Text= container;
Then the users sees the page and hits the submit button, your container string will not exist.
If however you uses the ViewState, ViewState["VST"] will still have the value as it will be "refreshed" when the user submits and sends the page back.
Read more here and also understand the ASP.NET page life cycle.
As per documentation:
View state is used automatically by the ASP.NET page framework to persist information that must be preserved between postbacks. This information includes any non-default values of controls.
You can also use view state to store application data that is specific to a page.
For details see a link:http://msdn.microsoft.com/en-us/library/bb386448(v=vs.100).aspx
If you want to persist the values after postback also than ViewState is the best option.
Every time your application do postbacks current values of your controls are being wiped out. So in order for you to store any values WITHIN THE PAGE you can save them in ViewState. Of course you must set the EnableViewState property first to true. Additional info, if you want to store any value or state while jumping into multiple pages you can use Session instead.
Im having an issue of going back to the previous page. The page i want to go back to had a few radio buttons which you had to select, after this you went to the next page which is the current page which then you can select certain things BUT I want to be able to go back to the previous page and the original selections for that page still be selected.
Anyway i could do this if so how?????
You can do a real basic back button with the help of JavaScript.
window.history.go(-1);
which will take you back to the previous page.
By default in ASP.NET state of controls is stored in ViewState, so it should be the same as user left them.
It is probably some view-state issue.
Go to your codebehind and check PageLoad method. If you are creating or setting radios in PageLoad, you do not want to re-init them every post-back.
if (!IsPostBack){
// your init here
}
Wanted to see if somebody can provide some suggestions/pointers on how to address this issue I am currently facing
.
Scenario is likeā¦
I have a Page say A where search is performed and data is displayed.
On Page A there is an ActionLink which passes an ID of the selected record to the page B.
Before redirecting to page B, I need to store/preserve form data of Page A (I do not want to use session or hidden filed as data is critical).
Once user is done working on Page B, I need to save changes on Page B and redirect back to Page A.
Here Page A should make a Post request based on preserver form data in step 3. Purpose is to display back searched data that was there before redirecting to Page B.
Something like..
View A
// Search textboxes here
// Search data list here with ActionLink column
View B
// more controls here
// Submit button - saves changes on this page.
// after submit button processing need to go back to View A
// and display the same Search data List based on Seach textboxes values
// those were entered by user before coming to this View B.
// Ques: Not sure how to persist data from View A between calls to View B and then View B to View A.
Does anybody has any better approach to achieve this?
For now the solution I have is...
Make an ajax POST request on ActionLink click and save the
formcollection in cache using controller.
Make default ActionLink GET request passing ID and in controller
return View B.
On View B, on submit do ajax POST request to save data on Page B and
return data from cache in ajax success function.
Make another ajax POST request using data retruned in above ajax
success to display View A.
Thanks in advance
If you're relying on full page refreshes then you need to use Session. You didn't give a reason as to why you don't want to use it, but Cache object is totally inappropriate for this purpose as it can be cleared out if the server needs to regain some memory, as well it is shared between users. Session is specifically built for the purpose you're describing - preserving data between full page refreshes.
Alternatively I would look into building your site as a Single Page Application, aka, you don't do full page refreshes between navigation, and can store data in a javascript object on the client. Due to the fact you put a lot of emphasis that the data is critical, the Session might be safer though as your javascript objects will get cleared out if the user accidentally navigates away from the page, where is a server based session object will preserve it for the duration of the session lifecycle.
You can also store the data in a cookie/local storage object (html5), but this is probably overkill for what you're doing.
I have created the webpage default.aspx inherited from Master page. I have used ajax update panel on default.aspx.
There are 3 textboxes on default page. When I click on submit, the error occured on the page and there is link Show Error which redirects to showError.aspx which shows the error details.
On showError.aspx page there is back button which onclick return back to default.aspx. In this process it looses the viewstate of textboxes at default page.
So how can I preserve the viewstate when ajax is used ?
this is typical behavior for webforms. viewstate is valid between postbacks. meaning you are posting to the same page(url). if you leave that page and go to another, then you are abandoning viewstate. the next time you return to that page viewstate is reset. the server doesn't know how you got the page (address bar, html button/link, or using history). it just knows you did arrive.
the short answer is you don't preserve viewstate. doing so defeats the purpose of viewstate. it's only meant to exist between postbacks. There are ways to preserve the user input, but this may require conceptual and design changes to how you are managing the validate and errors.
for example.
1. validate the user input (required, range, regex, etc.
2. validate the business rules (customer is valid, date is after point in time, customer's account is not on hold, etc)
3. process the request.
1 & 2 should catch most problems and return the user to the same page, with fields populated and a list of errors why the request was not successful.
If an error occurs at #3 then it's truly exceptional and should abandon all work. the user shouldn't return to the previous page with state preserved because something really is wrong that you were not expecting.
and all of this is indifferent of what the input is and whether or not you use ajax.