locked
<%= variable%> not working RRS feed

  • Question

  • User992700523 posted
    Default.aspx :
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <script src="Scripts/jquery.min.js"></script> <script> var x = <%=value %>; alert(x); </script> <input id="Hidden1" type="hidden" runat="server" /> </asp:Content>
    Default.aspx.cs : public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { } public static string value { get { return "test"; } } }
    Browser Console: Uncaught ReferenceError: test is not defined at Default:85

    Kindly Help.

    Thursday, July 27, 2017 10:53 AM

Answers

  • User475983607 posted

    Clearly, you did not follow the example in my previous post or you changed the source code. 

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsIdentity._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
        <script src="Scripts/jquery.min.js"></script>
        <script>
            var x = '<%=value %>';
            alert(x);
        </script>
        <input id="Hidden1" type="hidden" runat="server" />
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebFormsIdentity
    {
        public partial class _Default : Page
        {
    
            public string value { get { return "test"; } }
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }


    The HTML Source, notice the quotes around 'test'.

     <script>
            var x = 'test';
            alert(x);
        </script>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 11:46 AM

All replies

  • User475983607 posted

    You forgot the quotes.

    var x = '<%=value %>';

    However, you might need to rethink the design,

    public static string value { get { return "test"; } }

    A static field is single memory location that all users of the application can see.

    Thursday, July 27, 2017 10:58 AM
  • User992700523 posted

    Thank you for responding, I am working on chart Project. But this is not working in that project too. So i put it as a test subject.

    Secondly. The browser get the values, Here you see the inspect > sources.

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>
    
    </title>
        <script>
            var x = testing;
            alert(x);
        </script>
    </head>
    <body>
        <form method="post" action="./Default.aspx" id="form1">
    <div class="aspNetHidden">
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="rU8CBgeqBVhJf5I242FxE4y9hQyE6YsHFhppTFyXGzm8eshi9GNApcX+Cd66i7G/XxQIKBijlvcqh3j5PbxP8oyUIn6mm6Y90jlmC179DDc=" />
    </div>
    
        <div>
                <script>
                    var te = testing;
                    document.write(te);
                </script>
        </div>
        
    <div class="aspNetHidden">
    
    	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CA0B0334" />
    </div></form>
    
    <!-- Visual Studio Browser Link -->
    <script type="application/json" id="__browserLink_initializationData">
        {"appName":"Chrome","requestId":"db21462daaa24f92992cce07c74658a5"}
    </script>
    <script type="text/javascript" src="http://localhost:13807/e3825aa1eb77481e98fd8c100e245d9d/browserLink" async="async"></script>
    <!-- End Browser Link -->
    
    </body>
    </html>
    

    var te gets the value 'test'. But it is unable to put it on browser. The alert function is not working.

    Thursday, July 27, 2017 11:01 AM
  • User475983607 posted

    Clearly, you did not follow the example in my previous post or you changed the source code. 

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsIdentity._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
        <script src="Scripts/jquery.min.js"></script>
        <script>
            var x = '<%=value %>';
            alert(x);
        </script>
        <input id="Hidden1" type="hidden" runat="server" />
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebFormsIdentity
    {
        public partial class _Default : Page
        {
    
            public string value { get { return "test"; } }
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }


    The HTML Source, notice the quotes around 'test'.

     <script>
            var x = 'test';
            alert(x);
        </script>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 11:46 AM
  • User992700523 posted

    Thank you. It worked.

    Regards,

    Muhammad Naveed Haroon

    Thursday, July 27, 2017 11:52 AM