0

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.

Daniel
  • 2,167
  • 5
  • 23
  • 44
  • 2
    "How would I retrieve session data that's server-side with a client-side function?" You wouldn't. Send the rest of the necessary data to the server via ajax (e.g. Notification ID?). When the request arrives at the server, if needed you can get the User ID from the current session variables. And no you can't use the site.master as the URL. Instead of adding a webmethod to every page, set up a separate WCF ajax-enabled webservice (.svc) within your solution, so you can call the same URL from any page. Pretty sure there's a template for this in Visual Studio when you go to Add New Item. – ADyson Jun 02 '17 at 11:22
  • Thanks @ADyson I did find a simpler method that was fairly simple to execute. Adding the line `[System.Web.Services.WebMethod(EnableSession = true)]` allowed me to access session data and then base my Ajax response on that data. – Daniel Jun 03 '17 at 15:14

0 Answers0