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

后端 未结 1 548
旧巷少年郎
旧巷少年郎 2021-01-26 13:39

Codebehind:

SessionData(\"UserRole\") = \"Manager\"

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

1条回答
  •  生来不讨喜
    2021-01-26 14:09

    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.

    0 讨论(0)
提交回复
热议问题