How to set the .net Public Property value from Javascript?

Codebehind:

SessionData("UserRole") = "Manager"

SessionData is Public Property SessionData(sessionVariableName As String) As String

Is it possible to set the SessionData("UserRole") value as "Supervisor" from Javascript?

Answers:

Answer

It is not possible to directly set the value of variables on the server side using client side code. However, as some of the commenters suggested, you can use an Ajax request to send data to the server and handle the incoming data with something like a generic handler page. This can be done using the XMLHttpRequest classes packaged with most modern browsers. Alternatively, you could use a library like jQuery which includes an .ajax() method that allows you to send Ajax requests without worrying about the particular browser you are using.

On the server side, you would create a generic handler page (.ashx) that serves as the target url for your Ajax requests. Each of the properties you include in the data object passed using the jQuery.ajax method will be available in the handler page through the HttpContext.Request.Params collection by supplying a key to the Params collection, like context.Request.Params["someAjaxDataPropertyName"].

If you do use this approach, you will obviously want to include authentication and authorization code for each incoming Ajax request on the server side, otherwise any of your users could easily craft their own Ajax request to upgrade their permissions whether you want them to or not.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.