Solved

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

Posted on 2016-10-07
5
37 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 42

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 42

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now