Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing a C# variable to included javascript file

Passing a C# variable to included javascript file

Scheduled Pinned Locked Moved C#
javascriptcsharpvisual-studio
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sonar87
    wrote on last edited by
    #1

    I few sites said to (in the js file) put the varible in <%= %> these tags but thats not doing any good. Is there some other part needed to this I'm missing. The javascript I'm using is in a file I included in my VS project.

    N 1 Reply Last reply
    0
    • S Sonar87

      I few sites said to (in the js file) put the varible in <%= %> these tags but thats not doing any good. Is there some other part needed to this I'm missing. The javascript I'm using is in a file I included in my VS project.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      It will only work if it's in the markup for your page not in a seperate javascript file.


      I know the language. I've read a book. - _Madmatt

      S 1 Reply Last reply
      0
      • N Not Active

        It will only work if it's in the markup for your page not in a seperate javascript file.


        I know the language. I've read a book. - _Madmatt

        S Offline
        S Offline
        Sonar87
        wrote on last edited by
        #3

        is there any way at all to get a value from one to the other? Well.. I don't need it to go two ways..

        N 1 Reply Last reply
        0
        • S Sonar87

          is there any way at all to get a value from one to the other? Well.. I don't need it to go two ways..

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          JavaScript file

          function SomeFunction()
          {
          alert(myVar);
          }

          ASPX file

          var myVar = <%= SomeValue %>
          </pre>

          Code behind

          <pre>public string SomeValue{ get; set;}</pre>
          <div class="signature"><hr />I know the language. I've read a book. - _Madmatt</div></x-turndown>

          S 1 Reply Last reply
          0
          • N Not Active

            JavaScript file

            function SomeFunction()
            {
            alert(myVar);
            }

            ASPX file

            var myVar = <%= SomeValue %>
            </pre>

            Code behind

            <pre>public string SomeValue{ get; set;}</pre>
            <div class="signature"><hr />I know the language. I've read a book. - _Madmatt</div></x-turndown>

            S Offline
            S Offline
            Sonar87
            wrote on last edited by
            #5

            Forgot to mention I'm working with a WPF app and the html is included with that. How would all this work and be placed in that?

            N 1 Reply Last reply
            0
            • S Sonar87

              Forgot to mention I'm working with a WPF app and the html is included with that. How would all this work and be placed in that?

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Forgot? This is a critical piece of information that could have saved us botth a lot of time. You can't use the <% %> syntax in this situation. There is no ASP.NET engine involved that will parse and render these tags.


              I know the language. I've read a book. - _Madmatt

              S 1 Reply Last reply
              0
              • N Not Active

                Forgot? This is a critical piece of information that could have saved us botth a lot of time. You can't use the <% %> syntax in this situation. There is no ASP.NET engine involved that will parse and render these tags.


                I know the language. I've read a book. - _Madmatt

                S Offline
                S Offline
                Sonar87
                wrote on last edited by
                #7

                Alright, and sorry. By the time I got to this problem I'd already been up most night. Lesson learned.. don't post the help topics when severely lacking sleep..

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups