newbie question about inline code

T

tony collier

hello all

i have created a simple page :


<%@ Page language="c#" %>
<%@ Import Namespace="System" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {

....

string name="tony";

.....

}

</script>

....

<body>

....
<% if (name!=null){Response.Write("<table><tr><td>tony</td></tr>
</table>");} %>


....
</body>
</html>


when i try to compile this i get an error stating that there is no
defintion for 'name'. Why can't my inline code access a variable in the
page load script? I fear i have seriously misunderstood something......
 
M

Marina

This is really related to just scope of variables in general. A variable
declared in one function, cannot be accessed anywhere outside that function.
 
A

Anders Both

Change:
<script runat="server">
void Page_Load(Object sender, EventArgs e) {

...

string name="tony";
Into

<script runat="server">
string name="tony";
void Page_Load(Object sender, EventArgs e) {

...

It´s all about scope.

Also you should keep your void Page_Load in the code-behind aspx.cs file.

If you are not familiar with Object Orientered Programming, you would be
better off to start a few weeks with only programming c#, e.g. making small
console application.

If someone are good in C# and OOP he don´t really need to "learn asp.net",
he just do it.

Good Luck.

Anders Both, Denmark.
 
T

tony collier

Change:


It´s all about scope.

Also you should keep your void Page_Load in the code-behind aspx.cs
file.

If you are not familiar with Object Orientered Programming, you would
be better off to start a few weeks with only programming c#, e.g.
making small console application.

If someone are good in C# and OOP he don´t really need to "learn
asp.net", he just do it.

Good Luck.

Anders Both, Denmark.

thanks to both of you. I have spent last 3 months learning c#! i
somehow managed to forget all about scope. doh!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top