Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP.NET variable scope

ASP.NET variable scope

Scheduled Pinned Locked Moved ASP.NET
questioncsharpasp-netdatabasedesign
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sroberts82
    wrote on last edited by
    #1

    Hi, this is probably really straightforward but I only started using asp.net a few days ago and I cant figure this out. Basically i have my page.aspx and page.aspx.cs. There is a form on page.aspx. I generate the form based on data from my database and so the number of fields are variable and I want to know this variable in my aspx.cs page. I have a piece of code where all this gets done on the aspx page <% stuff stuff stuff int numberOfFields = whatever; %> How can I access this variable on my aspx.cs page? Also, maybe someone might explain why this attempt didnt work: I tried in my apsx.cs page defining like the following: public partial class Page : System.Web.UI.Page { public int numberOfFields; page load methods etc .... .... } and setting this in the aspx page <% this.numberOfFields = whatever; %> However, whenever I submited the form and clicked the submit button, in the event handler I tried to see the variable and it wasnt there. Its like the whole class got reinitialised. Why was that? Thanks a million in advance, Stephen

    D M 2 Replies Last reply
    0
    • S sroberts82

      Hi, this is probably really straightforward but I only started using asp.net a few days ago and I cant figure this out. Basically i have my page.aspx and page.aspx.cs. There is a form on page.aspx. I generate the form based on data from my database and so the number of fields are variable and I want to know this variable in my aspx.cs page. I have a piece of code where all this gets done on the aspx page <% stuff stuff stuff int numberOfFields = whatever; %> How can I access this variable on my aspx.cs page? Also, maybe someone might explain why this attempt didnt work: I tried in my apsx.cs page defining like the following: public partial class Page : System.Web.UI.Page { public int numberOfFields; page load methods etc .... .... } and setting this in the aspx page <% this.numberOfFields = whatever; %> However, whenever I submited the form and clicked the submit button, in the event handler I tried to see the variable and it wasnt there. Its like the whole class got reinitialised. Why was that? Thanks a million in advance, Stephen

      D Offline
      D Offline
      Daniel Santillanes
      wrote on last edited by
      #2

      Hi roberts, I was wondering why you need some code in the aspx page? possibly the best solution is to move all your code in the aspx to codebehind? just a thought. May be we can help you acomplish that? daniero

      1 Reply Last reply
      0
      • S sroberts82

        Hi, this is probably really straightforward but I only started using asp.net a few days ago and I cant figure this out. Basically i have my page.aspx and page.aspx.cs. There is a form on page.aspx. I generate the form based on data from my database and so the number of fields are variable and I want to know this variable in my aspx.cs page. I have a piece of code where all this gets done on the aspx page <% stuff stuff stuff int numberOfFields = whatever; %> How can I access this variable on my aspx.cs page? Also, maybe someone might explain why this attempt didnt work: I tried in my apsx.cs page defining like the following: public partial class Page : System.Web.UI.Page { public int numberOfFields; page load methods etc .... .... } and setting this in the aspx page <% this.numberOfFields = whatever; %> However, whenever I submited the form and clicked the submit button, in the event handler I tried to see the variable and it wasnt there. Its like the whole class got reinitialised. Why was that? Thanks a million in advance, Stephen

        M Offline
        M Offline
        minhpc_bk
        wrote on last edited by
        #3

        sroberts82 wrote:

        <% stuff stuff stuff int numberOfFields = whatever; %> How can I access this variable on my aspx.cs page?

        I guess that you migrate from the world of the classic ASP. In the ASP.NET, the inlide code with the <% ... %> executes when the Page renders, and in fact this code is inserted into a method defined in a dynamic class that the ASP.NET dynamically generate at runtime to represent the web page. This dynamic class inherits from the base class whose part is defined in the code-behind class file. So the scope of the variable defined in the inline code block is the method that the inline code is added to, and you cannot access this variable in the code-behind.

        sroberts82 wrote:

        However, whenever I submited the form and clicked the submit button, in the event handler I tried to see the variable and it wasnt there. Its like the whole class got reinitialised. Why was that?

        The inline code executes in the Render phase while the event handler of the button's Click event happens before this phase in the page life cycle[^]. So you cannot see the assigned value of the variable in the event handler. Btw, in an ASP.NET application you should avoid using the code render blocks with the <% ... %> or the <%= ... %> since it messes up your code, instead you should try using the code-behind file, the idea of the code-behind is to seperate the processing code and the layout of the web page. If you use a single file, you should use the script block with the attribute runat="server" to contains the server side code. For more information, you can see some documents below: ASP.NET Web Page Code Model [^] Code Render Blocks[^]

        S 1 Reply Last reply
        0
        • M minhpc_bk

          sroberts82 wrote:

          <% stuff stuff stuff int numberOfFields = whatever; %> How can I access this variable on my aspx.cs page?

          I guess that you migrate from the world of the classic ASP. In the ASP.NET, the inlide code with the <% ... %> executes when the Page renders, and in fact this code is inserted into a method defined in a dynamic class that the ASP.NET dynamically generate at runtime to represent the web page. This dynamic class inherits from the base class whose part is defined in the code-behind class file. So the scope of the variable defined in the inline code block is the method that the inline code is added to, and you cannot access this variable in the code-behind.

          sroberts82 wrote:

          However, whenever I submited the form and clicked the submit button, in the event handler I tried to see the variable and it wasnt there. Its like the whole class got reinitialised. Why was that?

          The inline code executes in the Render phase while the event handler of the button's Click event happens before this phase in the page life cycle[^]. So you cannot see the assigned value of the variable in the event handler. Btw, in an ASP.NET application you should avoid using the code render blocks with the <% ... %> or the <%= ... %> since it messes up your code, instead you should try using the code-behind file, the idea of the code-behind is to seperate the processing code and the layout of the web page. If you use a single file, you should use the script block with the attribute runat="server" to contains the server side code. For more information, you can see some documents below: ASP.NET Web Page Code Model [^] Code Render Blocks[^]

          S Offline
          S Offline
          sroberts82
          wrote on last edited by
          #4

          Hi lads, thanks a million for your responses. What Ive just done is stored it in the session and got it back that way. I do have another question though :). Whatever this returned variable is, call it x. I want to have x fields on my form to upload files to the webserver. I tried using Response.Write("
          "); And the things all show up on the page grand, but they dont seem to submit the files. They are not available on the request object on the server side. Any ideas why? or is there a better way to do this? Ive had a good hunt on the web, but still cant figure it out. Thank you again Stephen

          M 1 Reply Last reply
          0
          • S sroberts82

            Hi lads, thanks a million for your responses. What Ive just done is stored it in the session and got it back that way. I do have another question though :). Whatever this returned variable is, call it x. I want to have x fields on my form to upload files to the webserver. I tried using Response.Write("
            "); And the things all show up on the page grand, but they dont seem to submit the files. They are not available on the request object on the server side. Any ideas why? or is there a better way to do this? Ive had a good hunt on the web, but still cant figure it out. Thank you again Stephen

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            Hi there, What you are doing is simply adding the markup to the Response object, if you want to upload the file to the server, you can have a look at the Html input file control. To make it available at the server side, you either declare it in the web page .aspx or programmatically add it in code-behind to the page. You can get started with the link below: HtmlInputFile Class[^]

            S 1 Reply Last reply
            0
            • M minhpc_bk

              Hi there, What you are doing is simply adding the markup to the Response object, if you want to upload the file to the server, you can have a look at the Html input file control. To make it available at the server side, you either declare it in the web page .aspx or programmatically add it in code-behind to the page. You can get started with the link below: HtmlInputFile Class[^]

              S Offline
              S Offline
              sroberts82
              wrote on last edited by
              #6

              Thanks for that. How then could I add html to the aspx page from a method in the code behind file?

              M 1 Reply Last reply
              0
              • S sroberts82

                Thanks for that. How then could I add html to the aspx page from a method in the code behind file?

                M Offline
                M Offline
                minhpc_bk
                wrote on last edited by
                #7

                You simly use a Literal control or Label control in the web page .aspx, then assign the html markup to the Text property of those control in code-behind. You can see the link below to see how to choose a right control: ASP.NET Server Controls by Function[^]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups