ASP.NET variable scope
-
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
-
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
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
-
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
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[^]
-
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[^]
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 -
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 StephenHi 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[^]
-
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[^]
Thanks for that. How then could I add html to the aspx page from a method in the code behind file?
-
Thanks for that. How then could I add html to the aspx page from a method in the code behind file?
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[^]