I have a custom system that I'm currently adding notifications to. The notifications are created after an event and are stored in a database. Notifications are related to a UserID and the notifications are displayed in the site.master
page with the underlying code to retrieve them in the site.master.cs
file.
At the moment notifications are "dismissed" by clicking on a notification and performing the action related to that notification e.g. reading a message but I would like to have a "Dismiss" button that would perform this action on a number of notifications. I feel this is best achieved by triggering an Ajax call using jQuery rather than reload the page. I found this useful post that has given me the basic concept and I've got it working well on my demo site but to take it further I'm sure that I need to pass the UserID as a parameter to a function.
The trouble is that the UserID is stored in the Session
and I'm unsure as to the correct way to approach this. How would I retrieve session data that's server-side with a client-side function? I then considered having the method that is called pull the session data, although I'm unsure if a page that hasn't loaded in the browser has access to this.
I also thought of having the method on the master page, since the notifications display code is in the master page but adding a web method to it makes it tricky because I'm not sure how I'd call it, in the example the URL of the Ajax call is an aspx page:
$.ajax({
type: 'POST',
url: 'PageName.aspx/GetDate', // <- surely 'site.master.cs/Dismiss isn't a good idea?
data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
// Do something interesting here.
}
});
I'm pretty sure I can't add the site.master
as the URL.
I could always put the method on each page but that feels like an inefficient way to do it and would mean I'm repeating code all over the place. I also created an "ajaxHandler.aspx" file with a corresponding .cs file but I'm unsure if that can access the session data & if that's good practice.
PS I haven't added code because I think this is more structural than syntax but I'm happy to post anything that would help.