-5

Googled this and tried all responses I've found but cannot find the right answer.

Trying to simply pass code from code-behind (C#) to aspx (more specifically a javascript variable)

Code behind:

public partial class _Default : System.Web.UI.Page
{
    public string greetings = "hello";

    protected void Page_Load(object sender, EventArgs e)
    {

    }


}

ASPX:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My page</title>
    <script>
        var greeting2;
        function GetGreeting() 
        {
            greeting2 = <%=greetings%>;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

I've tried using a get method in the code behind rather than just a public variable but this has the same effect. I've also tried <%=this.greetings%> (with and without = sign). Nothing works. help!

Greg
  • 2,163
  • 1
  • 21
  • 23
user3882319
  • 1
  • 1
  • 2

3 Answers3

1

Change the field to a property:

public string greetings { get; set; }

You should now be able to access it on your aspx page, since the ASPX page basically inherits from the code-behind class.

Derreck Dean
  • 3,708
  • 1
  • 26
  • 45
  • There's nothing wrong with using a field to do this. There's no need to use a property. – Servy Nov 20 '14 at 21:53
  • If that was the case, he would have never had to write this question up, since his code should work just fine. In my experience, I've had to make fields into properties for the page to "see" them properly. – Derreck Dean Nov 20 '14 at 21:56
  • I'm staring right now at a field that I created, am accessing from the markup using identical syntax, and that is displaying the value of the field on the page itself. There is *no* problem with using a field, and no value in changing it to a property. Whatever the problem is, this isn't it. This isn't a data binding issue. Data binding is what requires properties instead of fields. – Servy Nov 20 '14 at 22:00
  • I don't see any reason for a field *not* to work, to be honest, but he submitted this post with the field as public. Check out this: http://stackoverflow.com/a/8883342/607117 – Derreck Dean Nov 20 '14 at 22:02
  • And that answer is wrong. Notice the accepted answer using a field and having it work just fine. It even has several comments stating that it doesn't change a thing. – Servy Nov 20 '14 at 22:03
0

In Page_Load of the _Default.aspx.cs (code behind):

ScriptManager.RegisterStartupScript(this, this.GetType(), "", "GetGreeting(" + greetings + ");", true);

Then in the _Default.aspx Page

<script>
    var greeting2;
    function GetGreeting(greetings) 
    {
        greeting2 = greetings;
    }
</script>
AperioOculus
  • 6,641
  • 4
  • 26
  • 37
-1
public partial class WebForm4 : System.Web.UI.Page
{
    public string greetings = "Hellooooo";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string Method()
    {
        return greetings;
    }
}





 <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<script type="text/javascript">
    var greeting2;
    function GetGreeting() 
    {
         greeting2 = ('<%=Method() %>');
         alert(greeting2);

    }
    window.onload = GetGreeting;

</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>

Mhmt
  • 759
  • 3
  • 22
  • 45
  • 1
    This isn't a meaningful change; there's nothing wrong with using the field instead of the method. – Servy Nov 20 '14 at 21:54