0

I've spent the better half of the past 4 work days trying to find a solution to this issue, but as a newbie coder, I'm yet to find a solution.

Basically I'm interning in this company, and they tasked me with translating a .NET app (C#) from doing its reading and writing of a webpage from a .dll to a .xml. The page is still opened from the dll, yet all the settings-saving will be done on the .xml, located at ./config.xml. I got it to write the file from the C# end, but when it comes to in-browser, client-side saving, I'm getting some issues. The page looks like this

In the previously mentioned DLL, that is hosted using IIS, I can both use C# to program server-side stuff and html through the use of a C# variable from a proprietary method that is used to render the webpage.

Every checkbox and dropdown (and its content) is dynamically given an HTML ID taking into consideration the divider it lies in and the number of the item, for example "general1", "misc3". To do that, I used XElement to create the base XML, which is read using C# and plugged into the html, and several javascript scripts to read what checkboxes are checked and what values the dropdowns own.

I'm having issues with the saving part, as this is a webpage meant to be opened on a mobile phone and needs to save the xml file onto the server. I've coded it so that, at the end of the page, there's a debug section so I can see the variables and their values, and update the webpage according to the item's contents (basically a way for me to study how getelementbyid and innerhtml work in JS). I then process the webpage data, and print out to a

what the .xml file would look like, as I stored it on a javascript variable. My major issue is finding out how to save that javascript variable into a file. I've read around that I could use php and VB, but I have no clue 1- how to code those and 2- if I can even implement that in this scenario. I've also looked around some javascript ways of doing this, but it often involves the user specifying where to save it LOCALLY, instead of somewhere hardcoded on the server. Any help will be incredibly thanked, as I managed to even baffle my higher-ups, so no one here found a solution yet

TL;DR: Got a client-side variable in javascript from an aspx, need to save it locally through javascript to a hardcoded location, maybe using php if you can get me to wrap my head around how it even works

  • You wouldn't use PHP to accomplish this when you're already in an ASP.NET application. There's no point in having two server side frameworks, you only need one. On the client side, you can't save it to a hardcoded location. Browsers don't allow JavaScript to write willy nilly to the file system: they can present a file to a user for downloading, and then the user gets to choose where to save the file. A little bit of searching on Stack Overflow finds [this question](https://stackoverflow.com/questions/3665115/how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server). – mason May 25 '21 at 17:23
  • 1
    And why is not server side C# code able to look at, see the values on the page, and simply create the xml and save it? It not at all clear why js is being introduced here? – Albert D. Kallal May 25 '21 at 18:14
  • @AlbertD.Kallal Honestly I'd love to be able to answer you, but I feel like my relative lack of experience makes me unable to answer. I'd say its the fact that the page is rendered and uses bootstrap methods, which are incompatible with ASP's way of doing its client-server communication, but it made sense to me at the time, as the way I saw it the C# code was only being used to render the page and not much more – laughincascade May 26 '21 at 08:35

1 Answers1

0

Well, first this this idea that some ".dll" is loading the page or data?

No, you have code behind. It has some referneced assembly and library, and the code behin for that page calls that code. In fact ALL pages work this way.

You have plain jane code behind - if the user enters data on that page, then VERY little reason exists as to why the code behind is not then able to freely use + consume the values and data in those controls. VERY strange this issue is not dealt with.

I mean, have js pass some value to a method in that code behind and save that data or whatever? Sure. (not clear why some class or object structure is not being used here).

I mean, if you want to pass some text to the server, then you can do this:

markup:

        <asp:Button ID="Button2" runat="server" Text="Button" ClientIDMode="Static"/>
        <asp:HiddenField ID="HiddenField1" runat="server" ClientIDMode="Static"/>

        <script>
            function myfuntest() {
                var sometext = "Hello, this is fun"
                $('#HiddenField1').val(sometext)
                $('#button2').click()
            }
        </script>

So in the js code, we shove a value/text into that hidden field. Click the button.

The code behind will now look like this:

protected void Button2_Click(object sender, EventArgs e)
{
Debug.Print("script passed value = " + HiddenField1.Value);
}

So, above now is code behind, and we passed that value.

The PROBLEM here is that you attempting to pass xml. And passing xml is NOT safe when using controls on the form. (you have to turn off validation)

As noted, again: VERY confusing why the value(s) of the controls on that page are NOT picked up by the code behind.

However, if you wanted to, then yes, you can pass a value to code behind.

And you don't even need say a button and hidden field. Just using a jQuery post (ajax call).

So, our code will look like this:

      <script>

            function test1() {  

                var strXML = '<?xml version="1.0" encoding="utf - 8"?><test>Some text</test></xml>'
        
                $.ajax({
                    type: "POST",
                    url: 'SaveXML.aspx/Test',
                    data: JSON.stringify({sXML : strXML }),
                        contentType: "application/json",  
                        datatype: "json"    
                    });  
                }  

        </script>

So I just shoved some xml into a var. But my spider sense VERY VERY much thinks a large detail here about that xml strucutre - and having a class server side that de-serlizes that xml is at play here - or should be used.

but, regardless, the above ajax call and web method will work. So on that web page, we can now have a public function called Test, with one paratmers called xXML.

The code behind stub can thus look like this:

[WebMethod()]
public static void Test(string sXML)
{
Debug.Print("web method - value data = " + sXML);

string strOutFile;
strOutFile = HttpContext.Current.Server.MapPath("/Content/test.xml");

File.WriteAllText(strOutFile, sXML);
}

So the above would write out the passed xml data from the client side, and the xml data you passed would be written out to a file on the web server under the web server folder called "Content" with a file name of test.xml

So it only a wee bit few lines of code. But I think overall, there is no doubt additonal details here, and it not quite at a all clear why the data is being gathered up in to some xml, and then sent to the server. If that data is a "group" of data rendered by asp.net controls, then the repeating data can be directly used for a "for each" and seen, used, and grabbed by code behind in place of a whole bunch of client side code that builds up the xml.

And it also not clear in regards to save the file - you ping pong and alternate the context between saving a file on disk - do you mean client side, or server side? I had to guess - since you can't save such files client side.

But above does show how to pass a simple var to the server side code behind in that web page, and then write out what you passed to a file on disk.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Hey, I'm currently trying your answer, but the second block of code (under the first markup) creates two errors: «CS0106: The modifier 'protected' is not valid for this item» and «CS0103: The name 'HiddenField1' does not exist in the current context». My IDE looks like that with the code inserted: https://imgur.com/a/z4cbEfW Am I doing it wrong? I know that this software runs on a non up-to-date version of .NET and C#, so maybe that's it? Ill answer your questions about why I'm doing things the way I am once I get this working :) – laughincascade May 26 '21 at 08:30
  • In the web form designer, you drag + drop a button on the form from the toolbox. You then double click on that button, and you should now jump from markup to code behind. That should result you being in the code behind editor. We assume this is a aspx web page. I would add a new test page (web form) to the web project, and try this code on a brand new web form page you create. If a simple button and some code behind can't be made to work here then your problems are MUCH beyond this question. You need the ability to drag + drop (add) buttons to that web page, and then have it run code behind – Albert D. Kallal May 26 '21 at 17:05
  • It is possible this is a MVC .net web project and not a asp.net + web forms project. When you are in VS and looking at the mark up for a web page, you can right click, and go view code. When you are in that web page, then right click and go view code. That should put you in the code editor in which you can see ALL of the C# or vb.net code behind that belongs to that web page. Since this seems to be c#, then all that code for the given web page would thus be c#. – Albert D. Kallal May 26 '21 at 17:11
  • When you right click inside of the mark-up, you get "Go to controler", or you see View code. If you see go to controler, then this is a MVC project. If you see View code, then this is a webforms project. There are two types of asp.net web prorjects. the "older" and more legacy type of web project (web forms project), and then what is called MVC project. So, you need to determine which kind of web project. – Albert D. Kallal May 26 '21 at 17:17
  • Yeah apparently this should be a webforms project as I can see the "view code". However, I can't seem to use the toolbox as it always returns that no item is available, or something along those lines. I tried editing the .aspx file corresponding to the page I'm working on and the button did work, however it always reloaded the page. I have a feeling that's something in the source code doing that but I don't think I have access to that part of the code :// – laughincascade May 27 '21 at 08:43
  • While you are in Visual Studio, you can open up any part of the web project from the solution explorer. The result is you see the web page markup. You can then right click, and choose view code, and that will jump you to the code behind. We all assume here that you using Visual Studio to edit and maintain this project. When you edit a web page, you have a markup view, and then a code view that lets you see/edit/write the c# (or vb.net) code behind that web page. You can't modify this project any other way. – Albert D. Kallal May 27 '21 at 18:03
  • The tool box is context sensitive. You have to be viewing the html markup, which then can let you select "design/split/Source". So, while in c# code editor, toolbox will be disabled. While in the markup editor, then tool box will be active. – Albert D. Kallal May 27 '21 at 18:05