Solved

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

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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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