SignalR: How to change a value (timer) from different page? - asp.net

I thought maybe you could push me to the right direction. I'm struggling with a simple issue (hard for me). I have a SignalR countdown page working fine. Now I'm writing a kind of administration page (.aspx) (with start/stop and reset timer functionality), but I'm not able to change the timer/countdown value.
My point here is: I want to change it from a different page.
Many thanks in advance,
N.

store the value as 'global' in cookie or session or localstorage or clientside sqlite or anything depending on the types of client in page1, if page2 wants to change the value in page1, simply modify these global variables. e.g. When page1 starts a timer, check the new value from the global variables first!

Related

reload current page in asp.net

I have a session variable that changes some things about how a page looks. I have a button that changes the value of this session variable. But ... the onClick event happens after page load, so by the time I update the session variable based on the button click, it's too late, the page has already been loaded.
Theoretically I could put all the logic about changing the display into a function and call it from page load, and then call it again from the onclick after the variable as been updated. But this is impractical: there are many user controls that check the value, used on many different pages in different combinations. I would have to hard-code the list of user controls on each page, and if someone added a new user control to a particular page, they'd have to remember to update this function, which is lame.
Is there a way to force a page reload? (I can use response.redirect back to myself and it works. If all else fails I guess this is what I'll do. But it means an extra round trip to the server, which is clumsy.)
Is there a way to process the onclick before the page load?
Some other magic solution?
If you have to change the look and feel of a page based on a specific value which can change, then you should have dedicated functions that set up the look and feel in a single unified place, and then you call those functions in every case where a value that affects the look and feel is called.
Examples:
private void SetDivVisibility()
{
// display logic here based on variables
}
private void MyControl_Click(...)
{
myvalue = blah;
SetDivVisibility();
}
It helps to bear in mind that the actual rendering of the page is last thing that happens, after both page load AND event processing.
Theoretically I could put all the logic about changing the display into a function and call it from page load
That's how you should do it. Cleanup your logic and markup - refactor and keep it DRY. That should help.
I can use response.redirect back to myself
That's the other option. Yes, a round trip is nasty.
you may put your code of styling your page in a void called by the page_load normally and called again from buttonclick
or call response.redirect to same url
or even onClick is client side use window.location.href
A design with a layout predicated on the existence of a session variable which won't exist until after it's been render is a huge design error. I like to call it the "Chicken or the Egg" syndrome. (yes, you can quote me.. ;)
I'd argue that your controls shouldn't get their layout completed in the on render. Instead, use a method (similar to databinding) where you can "rebind" the controls with the new session value on demand. This method would show/hide things based on the updated values.

VB.net starting a new session

While Ive seen plenty of examples in PHP, can't seem to find one in VB, I want to know if this is even possible.
I have a page, a session starts on page load (using global.asax) the user may not move from (or interact with) that page for a long while and I dont want to increase the session timeout. When the session times out after say.. 20 minutes, I want to start a new one, without moving from the page.
I also don't want constant keep-alives
I want to do it via another method (timer, button press...)
Is this even possible?
If you want to extend the session without a constant keep-alive, and prefer the user to click a button, or base it on a timer, then I would advise that you use fire an AJAX request to the server and not expect any type of a response. Simply doing so will keep the session alive. You can fire this request on any of your preferred criteria using javascript/jquery.

How long has a user been on a asp.net page (custom analytics)?

I am trying to come up with a way to measure how long a user has been on a page in my ASP.NET application. I am storing the userid, pagename, pageenteredtime and pagelefttime in a database. Each record has its own unique id as well, called featureuselogid.
At the moment, I can track when a user comes into the page with the page_load function on the server side. I store the userid, pagename and pageenteredtime.
After that im stuck, and need some guidance in the right direction. I need to record the time the user leaves the page. I know in javascript there is a window.onbeforeunload function, which will cover most cases (browser shutdown, links etc).
But how do I pass the featureuselogid to the javascript? If i can do that, I think I can make a webservice call from the javascript and update the record with the pagelefttime.
Am I going down the wrong path?
Cheers in advance.
You will have to run Javascript timer on the client that periodically calls the server. Once the call does not get logged at the expected time, it means that the user has left. Woopra does this, and it works reliably. For instance, sometimes people have a page loaded in a browser in the background for days, and there is no other way of detecting that they are still connected.
Users on laptops pulling the network cable, moving out of the coverage of a Wifi zone, etc etc there are too many scenarios where the onbeforeunload event will not reach a server anymore.
Just put the featureuselogid in a hidden field in the page, with a unique ID that will make it accessible to Javascript, or set a javascript variable.
Having said that, I believe that you are going to be able to most reliably detect Page_Load. You can determine the time between pages by measuring the time between Page_Load events. You won't get the browser closure, but IMO knowing when the user closes the browser is not all that meaningful.
When you render the page in ASP.net, include a javascript tag that assigns a variable to the value of a server side script:
<script language="javascript">
var JS_featureuselogid = <%= featureuselogid %>;
</script>
Later in your javascript code, you can reference the JS_featureuselogid variable and get the value that was injected into it during page construction.

How to Track F5/Refresh in ASP.Net

I am using VS 2005, C# 2, ASP.Net 2.0
I am unable to find out how to track that user pressed F5/Ctrl+F5/ Open a new Window(Ctrl + N) in ASP.Net.
I know that there is a Page.IsPostBack property, which tells that a page is loaded in response to an action taken by user.
I am just curious to know, that why isn't there a property as IsRefresh or Page.IsRefresh in ASP.Net, which will return true,
whenever user takes any of the above actions.
Is there a way to know this?
Actually my problem is that i have a DLL using which all of my aspx pages are inherited, I have to
insert some values in a table whenever the page is opened for the first time that's it, if user just opens the page or do not take any action,
an entry should be inserted into the database, but as far as I have tried, I controlled it anyhow using the Page.IsPostBack property, but I got stuck
in the refresh case, as it is inserting records unconditionally.
Similar to using a function in Global.asax (as others have suggested) you could use a session variable "flag". When the page first loads set a session variable and then just check against it in your page load function:
if (Session("visited") != "true"
//page has not been visited, log visit to DB
Just make sure you set the session flag sometime after the above check during the page load.
It won't be exact (sessions can timeout while a page is active, users can completely leave the site and come back in the same browser and the session stays alive, etc) but for your tracking it is much better than counting every page hit in the DB.
Perhaps you want the Session_Start method in the Global.asax file, which will be triggered once at the start of each user session?
In your Global.asax file, add or edit the method:
void Session_Start(object sender, EventArgs e)
{
}
why isn't there a property as IsRefresh or Page.IsRefresh in ASP.Net
Because ASP.NET cannot possibly know. The browser does not send any information that could allow it to determine whether the page is being requested due to a refresh or normal load. You will need to reconsider your requirements: what is the actual purpose of the database logging?
Session_Start method in Global.asax file is fired every time when a browser session is started. You can use this method to count number of unique users on your website.
Session_End method in Global.asax is fired when a session ends (explicitly or timedout). So you can decrement the count here.
Hope the above to example uses of these methods helps you understand how you can use them.
Because of the stateless nature of HTTP protocol there is no way to tell apart the initial load from the refresh
As has already been said. This isn't possible. A request issued due to a refresh is no different to a request issued the first time the page is loaded.
It sounds to me like you are trying to track page views somehow. This is certainly possible though it will require some work on your part. Your best bet is probably to log the URL of the page. You may also want to include the query string in order to differentiate between page loads for different pieces of data (if this happens in your application). You will also want to log the ID of the current user, and the ID of their session.
You can then make sure that you don't insert two page views for the same user for the same page in the same session, effectively filtering out any reloads of a page.
You do need to be aware that this isn't the same as detecting a refresh, what you are detecting is two page views in the same session, this could be a refresh, or it could be use of the back button, or just reloading from the address bar.
My suggestion would be to create a cookie on very first load, then on Page_Load check to see if the cookie exists. If it does, don't insert the record. You can use Session_End to destroy or create the cookie as someone suggested if that works with your application's architecture.

Why does my session variable appear to empty itself in ASP.NET?

I have user control on a ASP.NET web page, which contains a GridView and a radio button selector.
In the underlying middle tier I have a thread which goes to the database and then raises an event to say "I have some data" my User control handles this event and sets a Session Variable.
This works and I can see the event being handled and the Session variable gets the new data.
However when i go to use this session variable when the selected index of the Radio button selector changes the Session variable reports as "Nothing"
I have ensured that the obvious (i.e. spelling, Sessions switched on etc) are correct.
The GridView and radio button selector are encapsulated in the same Update panel.
Check that if your UpdatePanel - updatemode is set to 'Conditional'? also Child as triggers? I would first start by putting a stop in your page load, see whats happening from there. Do a search for all places where you populate that session variable and put a stop. You may be surprised, I have often found that page lifecycle gets confusing even though I thought I understood it. Alt - post some code and we can step through.
Yeah, sounds almost like a problem with order of operations or not checking for postback on a page load or something?
Like JamesM suggested, running your website in debug mode should really help identify the problem. You can bring up your watch window and set it for the Session variable you're looking for, then set breakpoints all over and check the value at each stop to divide and conquer the code.

Resources