Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:27:58+00:00 2026-06-05T17:27:58+00:00

Why is that the scriptPath variable is out of scope in the bottom of

  • 0

Why is that the scriptPath variable is out of scope in the bottom of the code?

Shouldn’t it be in scope throughout this page? In MVC, if I mark this on top of the page like

@{
    string scriptPath = "assets/scripts/",
           gkoConfig = "GkoConfig.js";
}

it is available throughout the current View. What am I missing now that I’m back to WebForms for a while?

enter image description here

If I change the code position, It get’s weirder as inside the <head> I no longer have access to teh variable, but I do have, inside the <body> now… :-/

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:28:00+00:00Added an answer on June 5, 2026 at 5:28 pm

    When you declare a variable in a Web Forms .aspx file, you’re actually declaring a local variable inside an auto-generated rendering method. ASP.NET generates separate rendering methods for all tags marked runat="server", so you actually get a separate method for your head element. Now, the variable you declare can only exist in one of these methods – hence the ‘weird’ behavior.

    You can see how this works if you pre-compile your application using aspnet_compiler.exe. You will get compiled DLL files for each of your web forms pages; just open one of those up in Reflector to see the generated code. I wrote a minimal equivalent of your code with the variable declared outside the head tag, and here’s the top-level render method that I got:

    private void __Render__control1(HtmlTextWriter __w, Control parameterContainer)
    {
        string str = "scripts/";
        __w.Write("\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
        parameterContainer.Controls[0].RenderControl(__w);
        __w.Write("\r\n<body>\r\n    ");
        parameterContainer.Controls[1].RenderControl(__w);
        __w.Write("\r\n    <script type=\"text/javascript\" src=\"");
        __w.Write(str);
        __w.Write("jquery-1.4.1.min.js\"></script>\r\n</body>\r\n</html>\r\n");
    }
    

    You see that the variable that I declared (here named str) is scoped to this method, and it’s calling other methods to render the head (and a form element marked runat="server".)

    A quick and dirty solution might be to simply remove the runat="server" from your head tag; however, I’d recommend that you declare a protected variable in your code-behind class for this. Adding a line like this to your code-behind file would work:

    protected string scriptPath, gkoConfig;
    

    You can then use these variables anywhere in your Web Forms code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a page that allows users to view a list of batch files
I have a pretty straight forward uploadified page that allows users to upload files,
I've got uploadify handling some file uploads on my mvc project and that part
I have written an ASP.NET web page with C# behind that runs an existing
I have used following code in IndexController: $view = new Zend_View(array('scriptPath' =>'C:\Users\398853\Documents\NetBeansProjects\PhpProject3\application\views\scripts')); echo $view->render('index.phtml');
The code for index.phtml as follows: <?php foreach ($this->entries as $entry): ?> <?php echo
I've got an ASP.NET MVC application that uses jQuery. To load the js libraries,
I'm using uploadify on a page that allows a user to upload images as
I have an Send.aspx page that has an uploadify control on it. Upload.ashx handles
That is, I'd like to have a tuple of values. The use case on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.