0

I am learning ASP.net, I have a variable in the code behind in c#:

public int hasCar= 1;

and in the aspx file I want to access this variable in javascript function:

function PrintCar( ) {
    var ind = <%=this.hasCar%>
    alert(ind);
}

but I get error:

does not contain a definition for 'hasCar' and no extension method 'hasCar' accepting a first argument of type 'ASP.vids_aspx' could be found (are you missing a using directive or an assembly reference?)

what is wrong?

thank you

3
  • 3
    Where exactly are you defining the PrintCar fn in the code.. Commented Jan 29, 2014 at 8:47
  • Looks like the javascript code's .aspx is not using your codebehind-file as codebehind, or you forgot to build your project. Commented Jan 29, 2014 at 8:52
  • in the end of the body. there i put <script type="text/javascript">code </script> Commented Jan 29, 2014 at 11:24

4 Answers 4

2

This works for me:

ASPX Page:

<body>
    <script type="text/javascript">
        function PrintCar( ) {
            var ind = <%=this.HasCar%>
            alert(ind);
        }
    </script>

    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="PrintCar();"/>
    </form>
</body>

Code Behind

public partial class WebForm1 : System.Web.UI.Page
    {
        public int HasCar { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            HasCar = 1;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I want to offer a complimentary answer rather than simply copying what other people have said. Personally I write JS from my C# rather than putting markup in the aspx.

I use this extension method:

public static class ClientScriptManagerExtensions
    {
        /// <summary>
        /// Registers an object as a variable on the page
        /// </summary>
        public static void RegisterObjectAsVariable(this ClientScriptManager mgr, Type type, string variableName, object objectToEncode)
        {
            mgr.RegisterClientScriptBlock(type,
                string.Concat("ClientScriptManagerExtensions_", variableName),
                string.Concat("var ", variableName, " = ", new JavaScriptSerializer().Serialize(objectToEncode), ";"),
                true);
        }
    }

Then to call I do:

this.Page.ClientScript.RegisterObjectAsVariable(typeof(MyPage), "myVariable", new { myProperty = 123});

This creates a js object on your page:

var myVariable = 
{
 myProperty = 123
};

Which you can access via JS. I find this approach much cleaner and it lets you pass all sorts of complex objects to your code.

Comments

0

Have you the following line at start line of your .aspx page ?

<%@ Page Language="C#" CodeBehind="default.aspx.cs" Inherits="YOURNAMESPACE._default" %>

1 Comment

yes I have this line, and still I try all the solutions here and it's not work. (I didn't try to call the JS from the code behind..cause I want to learn this method first)
0

I have created test project just for your problem.

Try this code

Aspx Code..

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <script type="text/javascript">
        function PrintCar() {
            var ind = <%=this.HasCar%>;
            alert(ind);
        }
    </script>

    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="PrintCar()"/>
    </form>
</body>
</html>

Code behind...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public int HasCar { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            HasCar = 1;
        }
    }
}

1 Comment

I'd be interested to know why this was downvoted - looks ok to me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.