最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery not getting variable value from C# var - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

You 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.

发布评论

评论列表(0)

  1. 暂无评论