The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Can I do scripting inside an ASPX page?

I am migrating some ASP pages to ASP.NET, but I do not want to use standard method - having a code behind file. I want to retain the flexibity of the scripting.

I wrote a simple .aspx file like the one below:

<%@ language="JScript" %>
<%
class MyObject
{
  ...
}

%>

When I launched the page, it displays an errorr saying that I can not put class declaration. Is it possible to do scripting in ASP.NET?
Burner
Saturday, August 12, 2006
 
 
You don't need to have a codebehind file.  You can have the server side code embedded directly in the aspx page, like classic ASP.  I'm not sure why you think this is more flexible than using a codebehind.
Jason Send private email
Saturday, August 12, 2006
 
 
It' faster to do changes, since a change might affect the whole app, it's not interesting to have to jump back and forth between a bunch of files.
It's a nice theory, but it's not working, to have code behind and hope that it's the only file one will have to change and that changes will be limited to that file.
Most of the time, when design is involved, it becomes an annoying case of jumping back and forth between x number of files, updating them all to fit.
I might not be able to explain it properly, but I live it every day.
Mikael Bergkvist Send private email
Sunday, August 13, 2006
 
 
Your problem is that a .aspx file must contain a class that inherits from the page class. You can't just put any old class definition in there. Standard class definitions go in regular class files. So you don't need to have a code-behind file. But what is in the aspx file must be a valid page class.
johnny bravado
Sunday, August 13, 2006
 
 
You can do it. Try
<script runat="server">
class MyClass
{
}
</script>
smalltalk Send private email
Monday, August 14, 2006
 
 
Be aware that ASPX files are limited to a single programming language. So if you have a C# project, your ASPX "scripts" need to be in C#, not in JScript or VBScript or whatever you are porting them from.

As for why you got the particular error you got - the ASPX model is very different from the ASP model. In ASP, everything was interpreted: it just ran the .asp file from top to bottom. The languages you used in asp were very dynamic and let you do just about everything anywhere.

ASPX actually compiles the page before running any of it. This means that the "script" blocks need to be syntactically correct for the point of the page you're working in.

Your problem came in because you had it in a <% %> block. This code gets placed in the body of the compiled Render() method. Class declarations aren't allowed inside a method.

The <script runat="server"> block works because this tells ASPX to place the script block into it's own section directly within the class declaration. Nested classes ARE allowed, so this works.

I personally think codebehind works a lot better for all this stuff.
Chris Tavares Send private email
Monday, August 14, 2006
 
 
If you are using Visual Studio, you can simply right-click on whatever it is you want changed and select refactor.
I have done this quite a bit and have found it to be a very effective way of making changes.  You can even tell Visual Studio to search through string literals and refactor changes there too!!
John Fitzpatrick Send private email
Wednesday, August 16, 2006
 
 
Mikael > If you need to edit many different files to change something that should be global, then you're application is badly designed.

OP > it is beyond me why you wouldn't want to use codebehind. Obviously, you do not understand it properly. I suggest some reading before you embark along the path you have chosen. If you mentioned to me in an interview that you did not like codebehind because you "preferred the flexibility of scripting" that would be an immediate no-hire.
codebehind warrior
Wednesday, August 16, 2006
 
 
"Mikael > If you need to edit many different files to change something that should be global, then you're application is badly designed."


Huh, I dont get this one?
Not everything can be global..
Mikael Bergkvist Send private email
Wednesday, August 16, 2006
 
 
Mikael, your problem doesn't make sense.  What change could you be frequently making that requires jumping back and forth between several files?
FullName
Thursday, August 17, 2006
 
 
Changing or evolving the app?
Mikael Bergkvist Send private email
Thursday, August 17, 2006
 
 
Just one quick point in relation to someone who earlier said:
"Be aware that ASPX files are limited to a single programming language. So if you have a C# project, your ASPX "scripts" need to be in C#, not in JScript or VBScript or whatever you are porting them from."

Actually, in ASP.NET 2.0 you can use C# in one ASPX page, VB in another, J# in another, and so on. Where you cannot mix and match languages is in the /App_Code folder: all of your classes there need to be of the same language.
Sea-Cat
Friday, August 18, 2006
 
 
I think you should stick to ASP if you cant get used to the code behind model.
We have a common code base with our windows app and our web app, because we use codebehind.  Its a real step forward.
I still like ASP though - <a href="http://www.worldwidekitchen.com">My New Cookery site</a>
BigJim Send private email
Thursday, August 31, 2006
 
 
oops messed up the URL its  www.worldwidekitchen.com
BigJim Send private email
Thursday, August 31, 2006
 
 
BigJim Send private email
Thursday, August 31, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz