How to get a Page from a class in asp website - c#

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.

Related

ASP.NET Web Forms - How to redirect and pass parameters without problems

I wanted to redirect (by code) to another ASPX page and on that page I wanted to select the appropriate view (in a multi-view and during page load).
My solution was to add a url parameter that was checked on the "OnLoadComplete" event and take appropriate action, but it seems that this url parameter gets copied to all the links on the page. So, the user cannot navigate anywhere else because always this view gets presented.
My second thought was to use a Session variable, but I am afraid that this will be an overkill.
Any ideas/thoughts/suggestions on this matter?
Can I prevent the use of the url parameter in all the page-links?
Is it bad if I use a Session variable for this temporarily?
Is there some other way to do this?

Passing Hidden Fields to another aspx page

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.

Windows Phone 8 Databound App and back button

I've something that I can't get myself understand. I'm making an app with databound template. I put a textbox on mainpage and a button. when i type something in textbox and press on button it navigates to the listing page and that content comes from web and then if i press on back button and make a new search the results from previous search stays there. how can i reset/clear or disable cache of that page?
It would be helpful if you could post your XAML and code-behind, but I will attempt to make a jab at an answer. Where are you referencing the call to get the data from the web? If it is in the constructor of the page, then that is why the previous search stays there. What is probably happening is the first search constructs the secondary page, does your web call, and binds your data to the page. Then when you press the back button, and click it again, the page is already constructed, so it uses the same data.
It is probably wise to call your web service in the OnNavigatedTo override method. From the first page, you can pass parameters to your secondary page (i.e., pass the search term, then pass the search term into your web service).
Here is an example of passing parameters between pages: http://developer.nokia.com/Community/Wiki/Passing_parameters_while_navigating_between_pages_on_Windows_Phone
Also, make sure your Data Context for the second page is appropriately set each time the page is navigated to, since you have a data bound application.
Without code, I can't really help other than giving these things to think about.

Remove content of asp.net session while navigating onto another page

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();

How can I pass textbox value from usercontrol(ascx) to another page (aspx) using Server.Transfer()

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)

Categories

Resources