the jquery work perfectly when I use the snippets in .aspx including this
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++"
];
$( "#tags" ).autoplete({
source: availableTags
});
});
</script>
then I wrote following code in my .cs file
protected void Page_Load(object sender, EventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
var val = from q in db.ques_tbls select q.qTitle;
db.SubmitChanges();
}
after adding this I changed one line in the script in .aspx like this
var availableTags = <%=val%>;
I ended up with this error. . Compiler Error Message: CS0103: The name 'val' does not exist in the current context
Source Error:
Line 12: <script type="text/javascript">
Line 13: $(function () {
Line 14: var availableTags = <%=val %>;
Line 15: function split(val) {
Line 16: return val.split(/,\s*/);
the jquery work perfectly when I use the snippets in .aspx including this
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++"
];
$( "#tags" ).autoplete({
source: availableTags
});
});
</script>
then I wrote following code in my .cs file
protected void Page_Load(object sender, EventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
var val = from q in db.ques_tbls select q.qTitle;
db.SubmitChanges();
}
after adding this I changed one line in the script in .aspx like this
var availableTags = <%=val%>;
I ended up with this error. . Compiler Error Message: CS0103: The name 'val' does not exist in the current context
Source Error:
Line 12: <script type="text/javascript">
Line 13: $(function () {
Line 14: var availableTags = <%=val %>;
Line 15: function split(val) {
Line 16: return val.split(/,\s*/);
Share
Improve this question
edited Jan 25, 2012 at 0:24
user1074474
asked Jan 25, 2012 at 0:17
user1074474user1074474
4713 gold badges7 silver badges18 bronze badges
1
- my goal is to assign "availableTags =" with CS variables – user1074474 Commented Jan 25, 2012 at 0:24
2 Answers
Reset to default 6You declared val a local variable to your Page_Load method
var val = from q in db.ques_tbls select q.qTitle;
It must exist at the class level for the aspx page to use it. Create a member or property to store the value.
Just create a function in your C# code which returns the title.
protected string returnTitle() {
DataClassesDataContext db = new DataClassesDataContext();
var val = from q in db.ques_tbls select q.qTitle;
db.SubmitChanges();
return val.ToString();
}
and JS..
<script>
var availableTags = "<%= returnTitle() %>";
console.log(availableTags);
</script>
That will return availableTags as a string though, you may way to tweak it to return an array if that's what you need.