I want to be able to pass data from one aspx page to another using hidden fields.
On pageone.aspx, in the page_load, I am saving data into a hidden field:
ClientScript.RegisterHiddenField("var1", "hello");
Then, the user clicks on a button on pageone.aspx, which redirects to pagetwo.aspx
Response.Redirect(Constant.AdminUser, true);
Then, in the page_load of pagetwo.aspx, I grab pageone's hiddenfield by:
Request.Params["var1"]
But nothing is returned.
For some reason, when I replace Response.Redirect with Server.Transfer, I get data in pagetwo.aspx, which is what I want. But the browser's URL does not change.
I want to be able to pass in data from pageone to pagetwo, without storing the data in the session variable, url, database, cache, etc. I would like to send it using hidden field.
MSDN has a full article on handling cross-page posting.
http://msdn.microsoft.com/en-us/library/ms178139.aspx
TL;DR: You have to register the previous page using the #PreviousPageType declaration, then call against the PreviousPage property.
I do something very similiar and all I did was erasing my Server.Transfer line and keep PostBackUrl on my button.
If you have them both, your URL won't change.
Related
sorry for my bad English.
I have a Default.aspx page in my project containing a textbox and a button. So when the client clicks on the button, the browser calls a method that is in my web service WebService.asmx.cs whit the textbox.Text as parameter.
(e.g NameSpace.WebService.Say("Hi"); in js)
But I need to report the results ("Hi") to my Default page for showing them in an UpdatePanel and I don't know how to get the page.
I tried (Default)HttpContext.Current.CurrentHandler but it was null.
Is there any other way to get that page?
You can't access the Page object while calling a static(webmethod) method. Static methods cant access instance of the Class it is part of.
Options you have are:
1. Save the value in Session. (HttpContext.Current.Session should be accessible) and show it on the next page load, after accessing it from session. This is a roundabout way and you will have to decorate the webmethod with this attribute:[WebMethod(EnableSession = true)]
2. Instead of using webservice, just update the label in Client side itself using Javascript.
I am using a Session["filter"] variable to store the value of a selected dropdown value when a page redirects to itself. But, if any other page is opened then the variable value should be removed. How do I achieve this?
You could use ViewState["filter"] instead which would be specific to that page.
You could say Session["filter"]=string.Emptyon the landing page if the page Redirect page is on the same web site/application.
If it is not then you can clear the session variable using onselectedindexchanged of dropdown event.
In the case of server page inside the application you can also check if page exist like below before clearing a session value
System.Web.Hosting.HostingEnvironment.VirtualPathProvider.FileExists("~/SomePage.aspx");
I have a session variable that I am using on a page. I know that sessions should be used for the purpose
of storing data between different pages of a website, but I have a big dataset to store and I am using a session instead of a view state.
I would like to empty the session when I navigate to another page.
Is there a way I can do it ?
I tried setting the session variable to null on the PageUnload event, but thats not what I want.
I would like to set the session variable to null while the page is navigated to another page.
Please let me know.
You just need to call
Session.remove("nameOfSessionVariable") ;
And regarding to how to handle the "leaving page event"
You will have to write some front-end javascript to do this using
something like window.onbeforeunload(). Then you'd have to make an
AJAX call to tell your back-end that this event is happening. This
isn't foolproof, of course. A browser crash or a forced "quit" would
not fire this event.
As can be seen here C# ASP.NET Page Leaving event?
If you truly want to detect when user leaves the page - you have to do this from the client-side. Handle onbeforeunload event and make an AJAX call to clear session variable.
But this is an overkill. Consider refactoring the code to store in session smaller amount of data and only that is used between different pages.
On the page you have navigated to you can clear a session variable as follows:
Session.Remove("Name");
To abandon the session completely:
Session.Abandon();
I have a button in my aspx page, which in code behind the value of postbackurl gets set. I updated the code to add click event handler and added Response.Redirect code to redirect user to the page once the button is clicked. The only reason i ended up adding click event was because i had to add some extra logic when the button was clicked including redirecting to same url, that was used in postbackurl. The page does get redirected on click however it seems like all the hidden fields from the field gets lost once the form get redirected to the url.
Is there anyway i can submit the form without loosing the hidden data.?
Maybe one way you can solve this problem is to use the server side Transfer() call.
http://msdn.microsoft.com/en-us/library/540y83hx(v=vs.85).aspx
In most cases I've seen what you really want to do is pass the information for the new page using URL parameters. Take all the stuff the new page needs and put it into the url (encrypt if it is security sensitive).
If the only issue you are having is when you want to stay on the same page, this is simple, just have the click event handler return without doing a response.redirect or a transfer.
Post the code if this is not working for you.
I am writing one user control (webpart) in kentico. I want to pass textboxes' value from usercontrol to aspx page using Server.Transfer().
Can it be? If so, how can I do like that?
Best Regards,
Reds
I don't particularly like this method, I prefer to use Sessions to pass data between pages, but if you need to do this here how it's done according to the this page.
Here's TL;DR summary. It requires three scripts/pages:
Form.ascx - this will be the control that contains the text box value.
FormParsingScript.aspx (referenced in the Form.ascx in the Action attribute) - this will perform the actual Server.Transfer "FinalScript.aspx" call
FinalScript.aspx which will display the contents of Response.Form["TextBoxName"] (HTTP POST) or Response.QueryString["TextBoxName"] (HTTP GET)