1

I am having problems setting JavaScript variables on the Server Side in ASP.Net Web Forms (Not MVC) using Master Pages.

Basically I want to do something like this:

<script type="text/javascript">
    var z = '<%# Request.QueryString["Env"] %>';
</script>

After I run the code I see: var z=’’;

I also tried:

var z = '<%= Request.QueryString["Env"] %>';

After I run that code I get the following error so I assume that is not correct:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

I have been doing mainly MVC development these past few years so I am not sure what I am doing wrong.

Please let me know.

Thanks

4
  • I have been away from WebForms for a while also but your second snippet looks like what I remember. Can you post a bit more of the surrounding code? Commented Jan 28, 2012 at 14:09
  • Dynamic javascript is a known anti pattern. Don't do this shit. Commented Jan 28, 2012 at 14:34
  • @Raynos: I guess you don't do a lot of MVC development since it is used all over the place. Commented Jan 29, 2012 at 20:04
  • @RichardCorkery I know anti patterns are used all over the place. That's because most developers don't know what they are doing. I'm not surprised anything to do with ASP.NET is full of anti patterns Commented Jan 29, 2012 at 20:21

4 Answers 4

3

I think it is a good idea to place the javascript just before closing the body tag instead of head tag. Then you can use <%= syntax.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for responding. I used this option and it worked. Thanks.
1

any chance that the javascript block you mention is inside a server control ?

for example :

<head runat="server">
  <script type="text/javascript">
    var z = '<%# Request.QueryString["Env"] %>';
  </script>
<head>

if so try changing it to :

 <head runat="server">
    <div runat="server">
      <script type="text/javascript">
        var z = '<%= Request.QueryString["Env"] %>';
      </script>
    </div>
 <head>

reference : http://www.west-wind.com/weblog/posts/2006/May/27/The-Controls-collection-cannot-be-modified-because-the-control-contains-code-blocks-ie-

Comments

0

For sure the <%# %> is for iteration, to render a string you need to use <%= %> how ever because is probably on the header that runs on server is better to avoid that and use a literal like that.

<script type="text/javascript">
    <asp:literal run="server" id="txtJavaDeclarations" EnableViewState="false" />
</script>

on code behind

txtJavaDeclarations.Text 
      = string.Format("var z = '{0}' ;\n", Request.QueryString["Env"]);

Comments

0

I would be tempted to use

ClientScript.RegisterClientScriptBlock(GetType(), "scriptBlockKey", 
        "var z = '" + Request.QueryString["Env"] + "';", true);

That should write in the script block you need with the script tags (final argument set to true).

It's called from the server side so just pop it in Page_Load. It's got the benefit of being easier to debug is things go wrong.

I would be tempted to to a null check on the query string as well with a tenary operator i,e,

  string env = Request.QueryString["Env"] == null ? string.Empty : 
                                    Request.QueryString["Env"].ToString();
    ClientScript.RegisterClientScriptBlock(GetType(), "scriptBlockKey", 
            "var z = '" + env + "';", true);

Just makes the null check a bit more transparent and perhaps gives the opportunity for a default case.

1 Comment

Thanks for responding. I used this option and it worked. However, I decided to go with a different Answer to solve my problem since it was a little less changes. Thanks again.

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.