Solved

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

Posted on 2016-10-07
5
34 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 39

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
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 39

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
ID: 41834106
Aha.  good to know.
0
 

Author Comment

by:Alex E.
ID: 41835064
Thank you
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

19 Experts available now in Live!

Get 1:1 Help Now