Solved

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

Posted on 2016-10-07
5
53 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.
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 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 43

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 40

Expert Comment

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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