locked
best way to get values from javascript function in c# code behind RRS feed

  • Question

  • User-153404742 posted

    so I have a file manager where user has option to download, delete and so forth for files.  These are defined in javascript and I need to process the logic for deleting and renaming in the code behind on server side.  What is the best way to do this ?  I want to have a function in c# that is called which has the parameter values passed in from the javascript function.  So example of one is:

    handleDownload = function (id, path) {
            console.info(`download requested for item with id ${id} and source ${path}`);
        };

    From the above functioin, I want to pass in id and path to a function I can create in c# where I can process the logic...so call the following function in c# from the above javascript function.....Do I have to store it in hiddenfield and then pass it?

    Public bool DownloadFile(int id, string path)

    {

    {

    Tuesday, October 6, 2020 3:40 PM

All replies

  • User475983607 posted

    so I have a file manager where user has option to download, delete and so forth for files.  These are defined in javascript and I need to process the logic for deleting and renaming in the code behind on server side.  What is the best way to do this ? 

    AJAX is used to POST JSON formatted objects from the browser to a the web server.   

    What kind of application are you building?  Web Forms, MVC, Web API?  

    Tuesday, October 6, 2020 4:08 PM
  • User-153404742 posted

    web forms

    Tuesday, October 6, 2020 4:23 PM
  • User475983607 posted

    inkaln

    web forms

    If you only need two values, id and path, and you want/need to use standard page life cycle then add two hidden fields.  One for the Id and one for the path.  

    If you want to use AJAX then you need a Web Method.

    If you want to use AJAX and the standard tooling then you can look into the UpdatePanel.

    Unfortunately, you have not provided enough information to provide an accurate solution.  One design element that is certain is an HTTP request is need to get the data from the browser to the server.   All the above options can work. 

    Tuesday, October 6, 2020 4:35 PM