Ankit Ahuja
Ankit Ahuja

Reputation: 73

trying to run javascript written by my codebehind

I am trying to run some javascript code that is created by my codebehind document.

<script type="text/javascript">

    function GetMap() {
        //some javascript
    }

    function AssetDescription(var1, var2, var3) {
        //some javascript using var1, var2, and var3
    }

    function test() {
        alert("test position 1");
        var onloadText = document.getElementById("points").value;
        alert(onloadText);
        eval(onloadText);
        alert("after eval");
    }
    window.onload = test;
</script>

<body runat="server">
<input type="hidden" id="points" runat="server" />
</body>

CodeBehind

protected void Page_Load(object sender, EventArgs e) {
    points.Attributes.Add("value", Iterations());
}

public string Iterations() {
    string toReturn = "GetMap(); \n";
    //assetRow is an array of strings. The strings are in the format "var1, var2, var3"
    for (i=0; i<numberOfAssets; i++)
    {
        toReturn = toReturn + "AssetDescription("+assetRow[i]+"); \n";
    }
    return toReturn;
}

Hopefully I have made all of this as clear as possible. This is all the code I believe is relevant to my question. I am open to other ways of setting this up.

I have tried setting the body.onload element to include my javascript from the codebehind directly, but to no avail. I would like to add this code directly in my tags, but I don't think ASPX has that capability, so this is the workaround I have created.

This seems to be the most promising method i have found. I have searched these forums, and most of you say that eval() is a bad idea. Needless to say, it's not even working. But I would love a more robust alternative if any.

The alert("after eval") alert never fires, but alert("test position 1") and alert(onloadText) both fire.

Upvotes: 0

Views: 1024

Answers (1)

Chris
Chris

Reputation: 174

instead of setting it to and html element and using eval to run it, you can created a public variable, and print the public variable on to the page. This will mean the string contents of the variable is going to be printed, which will be the JavaScript to be run.

So front end

<script type="text/javascript">

    function GetMap() {
        //some javascript
    }

    function AssetDescription(var1, var2, var3) {
        //some javascript using var1, var2, and var3
    }

    function test() {
        alert("test position 1");
        <%= generatedScript %>
        alert("after eval");
    }
    window.onload = test;
</script>

<body runat="server">
<input type="text" id="points" runat="server" />
</body>

Code Behind

public string generatedScript = string.empty;

public string Iterations() {
    string generatedScript = "GetMap(); \n";
    //assetRow is an array of strings. The strings are in the format "var1, var2, var3"
    for (i=0; i<numberOfAssets; i++)
    {
        generatedScript = generatedScript + "AssetDescription("+assetRow[i]+"); \n";
    }
}

Should be fine!

Upvotes: 1

Related Questions