Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET C# load a .js file and process some data in code behind

Posted on 2016-10-07
5
Medium Priority
?
67 Views
Last Modified: 2016-10-08
We need to call in ASP.NET C# to code behind a .js file or maybe in head section if apply and then
process in code behind a file like this:

<script type="text/javascript" src="vars.js"></script>

Open in new window


Now the real issue is inside the file vars.js because we have some information that changes
dynamically. For example suppose we have something  like this in that .js file:


var name='any text'
var for=false
var login=false
var number=true
var pict=true
var catch=0
var result=false
var detail=false
fillda=new Array()
fillda[0]='Any tex title|right'
fillda[1]='Any text title name 1|../begintofill1/batch|540|405|0|25|0|'
fillda[2]='Any text title name 2|../begintofill2/batch|540|405|0|32|0|'

Open in new window


Now:

1.- How can we load that .js file in asp.net c# and then the rows that say
"var" convert to variables in asp.net c# with the corresponding value from that
text .js file. For example in the example above the var name='any text' in asp.net
could be generated a variable string name = "any text";. and of course that "var"
instances rows we can add or remove more values in other words we can add another
row called "var = address" in the .js file or remove an instance
2.-For the section "fillda[x]" where "x" is any dynamic value like point one could ve
any number of instances and the same like point 1 how can be process the "fillda[x]"
to a new array in the asp.net c# code behind identical to the javascript array in the .js file?

Thank you
0
Comment
Question by:Alex E.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 41834007
write it to a hidden field on the form and then you can read it in the code behind.
<asp:HiddenField ID="hdnField" ClientIDMode="Static" runat="server"></asp:HiddenField>

Open in new window


Then in Javascript:
  $("#hdnField").val(values);

Open in new window

0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41834087
Actually, you can't access ASP.NET controls from jQuery with the ID.  If you bring up a web browser and look at how the control ID is rendered in HTML, you'll see why.  A bunch of other characters are added to the ID.  So, you must reference the ClientID of the control.

$('#<%=hdnField.ClientID%>').val();

Open in new window


So, the proper way to tackle this is to link the js file to the aspx page using proper script tags.  Then use JavaScript / jQuery to retrieve the values you need and copy them to ASP.NET hidden controls, which can be read from code behind.
0
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 41834096
Zephyr:  With ClientIDMode="static" the id will not be changed.
0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41834106
Aha.  good to know.
0
 

Author Comment

by:Alex E.
ID: 41835064
Thank you
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question