TRICK: methods in ASPX pages with <%%> code blocks

S

Scott Allen

the difference between aspx and aspx.cs
is straightforward

aspx is jit compiled when the page is requested

... so is the assembly in the bin directory - the difference is the
runtime has to generate code for the ASPX page, compile it to MSIL,
and then JIT compile. The bin directory only needs the JIT step.

everytime the page is requested the asp.net runtime
checks the modifydate and size of the file

if it has changed it recompiles the new one

this allows for designers to change aspx and upload
it without recompilation

that is a GOOD THING

however, where ms has made a mistake is in VS.NET
they should have allowed ASPX pages to be compiled
as part of the BUILD, this would let structural errors
be caught without a potentially long winded live test
or worse, a customer having a problem

Agreed. I prefer the compilation model in 2.0, where the ASPX and
code behind are compiled at the same time and into the same assembly.
All this can take place before deployment.

There is virtually no difference between code-behind / inline code
model in 2.0, except for the number of files involved, which is good.
 
G

Guest

If you create user controls you can use the Render method. There you can
write your HTML. If you develop them correctly, you can use properties to set
colors, texts and more.

The '%> some HTML <%' constructions are not really object oriented, but the
user controls are. If you want to use '%> some HTML <%' constructions, you
are not using the object oriented "way of working".

In your example the correct way is to place this in you page.load event. Add
something like this:

TextBox Name = new TextBox();
TextBox.Text = "Some Text";
Page.Controls.Add(TextBox);

The code above creates an object (not some html code) of the type "TextBox",
you set the new object's properties (like text, but also style, register
client side methods,etc) and add this new object to the control collection of
the page. If you use this correctly you'll find that developing websites will
be much easier.

Please buy a book on object oriented development in .Net or search google if
you don't really understand this....
 
J

John Rivers

Thankyou for your friendly post,

could you please give me more specific benefits to this approach than
"not really object oriented"

what actual advantages (and disadvantages) does the approach you are
suggesting actually have in contrast to simply using html literals?

Do you think the advantages outweigh the disadvantages?

You see I think this approach has so many disadvantages it is unusable.

And if you read many of the posts in this newsgroups you will see
people struggling with a level of complexity and unpredictability that
is simply unecessary.

Please think it through fully and decide.
 
G

Guest

Look at the following code:

1: using System;
2: using System.Web.UI;
3: using System.Web.UI.WebControls;
4: using System.ComponentModel;
5: using System.Text;
6:
7: namespace markberck.UI.WebControls
8: {
9: [DefaultProperty("Url"),
10: ToolboxData("<{0}:IFrameControl
runat=server></{0}:IFrameControl>")]
11: public class IFrameControl : System.Web.UI.WebControls.WebControl
12: {
13: private Unit height;
14: private Unit width;
15: private bool border;
16: private string borderStyleString;
17: private string url;
18:
19: #region Properties
20: [Bindable(true), Category("Appearance"), DefaultValue("")]
21: public override Unit Height
22: {
23: get {return height;}
24: set {height = value;}
25: }
26: [Bindable(true), Category("Appearance"), DefaultValue("")]
27: public override Unit Width
28: {
29: get {return width;}
30: set {width = value;}
31: }
32: [Bindable(true), Category("Appearance"),
DefaultValue("false")]
33: public bool Border
34: {
35: get {return border;}
36: set {border = value;}
37: }
38: [Bindable(true), Category("Data"), DefaultValue("")]
39: public string BorderStyleString
40: {
41: get {return borderStyleString;}
42: set {borderStyleString = value;}
43: }
44: [Bindable(true), Category("Data"), DefaultValue("")]
45: public string Url
46: {
47: get {return url;}
48: set {url = value;}
49: }
50: #endregion
51:
52: protected override void Render(HtmlTextWriter output)
53: {
54: if (this.url != null)
55: {
56: StringBuilder sbOutput = new StringBuilder();
57: sbOutput.Append("<iframe");
58: sbOutput.Append(" src=\"" + this.url + "\"");
59: if (!height.IsEmpty)
60: sbOutput.Append(" height=\"" + buildUnit(height)
+ "\"");
61: if (!width.IsEmpty)
62: sbOutput.Append(" width=\"" + buildUnit(width)
+ "\"");
63: if (Border)
64: {
65: if(borderStyleString != null)
66: sbOutput.Append(" style=\"" +
borderStyleString + "\"");
67: else
68: sbOutput.Append(" frameborder=\"1\"");
69: }
70: else
71: {
72: sbOutput.Append(" frameborder=\"0\"");
73: }
74: sbOutput.Append(" id=\"" + this.ClientID + "\"");
75: sbOutput.Append(" name=\"" + this.ClientID + "\"");
76:
77: sbOutput.Append("></iframe>");
78: output.Write(sbOutput.ToString());
79: }
80: }
81:
82: private string buildUnit(Unit u)
83: {
84: switch(u.Type)
85: {
86: case (UnitType.Cm) :
87: return u.Value + "cm";
88: case (UnitType.Em) :
89: return u.Value + "em";
90: case (UnitType.Ex) :
91: return u.Value + "ex";
92: case (UnitType.Inch) :
93: return u.Value + "in";
94: case (UnitType.Mm) :
95: return u.Value + "mm";
96: case (UnitType.Percentage) :
97: return u.Value + "%";
98: case (UnitType.Pica) :
99: return u.Value + "pc";
100: case (UnitType.Pixel) :
101: return u.Value + "px";
102: case (UnitType.Point) :
103: return u.Value + "pt";
104: default:
105: return u.Value.ToString();
106: }
107: }
108: }
109: }

This is a simple user control to create an IFrame object. The advantage of
this solution, is the reusabiliy. If I need an a Iframe on a page, I can set
its properties from code (like border, borderstyle, src, etc). I don't have
to create a method that returns a string, I can read my properties back etc.

If you use you "inline" html, it is much harder to read the properties set
(by another method) or change anything from code.

I agree that this approach looks like a much more difficult way to create an
IFrame, it was much faster in classic ASP where you would create a simple
method for this. But once you get used to the .Net way of working, you will
see it's advantages. The IFrame object I described can now be used by many
different projects, they just need to know the location of the IFrame dll.

another big advantage, inheritance:
For example, If someone misses a property, they can create a new control
which inherits the current control. The new control just has to contain the
new property.
In classic ASP way of working, you will have to create a new method and
create a maintenance issue. You'll have to update all the methods that use
the same code , while I just have to update one control (the controls that
inherit just have to be recompiled)
 
K

Kevin Spencer

Programming power necessarily introduces complexity into the experience of
the developer, just as aircraft power introduces complexity into the
operation of an aircraft.

A paper airplane is easy to fly. Just throw it. But how high can it fly, and
how many people can fly in it?

A single-engine small aircraft is considerably more difficult to fly. It
propels itself, and must contain at least one human being. So, it must be
able to take off and land without injuring the human beings inside it. It
must have fuel inside it. It must have enough power to lift the plane the
engine, the fuel, and the human beings inside it. It must have controls for
navigation, a control interface to enable the pilot to control it, and an
instrument interface to enable the pilot to receive information from it. But
how high can it fly, how fast can it fly, and how many people can fly in it?

A Boeing 747 is considerably more complex that that. It can fly at high
altitudes, over very large distances, carry hundreds of people as well as
their baggage, fly at considerably higher speed than a single-engine small
aircraft, and even fly itself. But what sort of complexity does this add to
the design of the aircraft? At high altitudes, air pressure is much lower,
and air is much thinner. It must have enough power to fly in thin air, and a
system of cabin pressurization to provide enough air to the passengers, as
well as a system to protect passengers when the cabin loses pressure. It is
very large, so it must have enough power to lift an incredibly heavy
aircraft to high altitudes in thin air. It must have several jet engines,
rather than a single propeller engine. These engines must be coordinated to
make the plane fly correctly. In order to fly itself, it needs a computer
that can gather data about the state and location of the aircraft, and
calculus algorithms to calculate the trajectory and the amount of power, as
well as the state of the navigational devices on the aircraft, such as
flaps, rudder, ailerons, etc. It must have backup systems. that can take
over in the event of any system failure. It must have a bathroom for the
human beings inside it, as well as other human necessities that must be met,
such as thirst, hunger, etc. It needs a crew of human beings to fly it. And
each member of the crew must be fully trained to do their job and interact
successfully with the rest of the crew and passengers. And so on.

Back in the day, a computer could perform exactly one instruction at a time.
In fact, a computer can STILL only perform only one instruction at a time.
Back in the day, there were no networks, and no Internet. Computers were
safely contained within their own confines, and had very little memory and
procesing power. Computers are calculators that have electronic switches and
toggles in them. Back in the day, computers were programmed using binary
machine code. Punch cards were used to feed instructions to them. A hole was
a 0. No hole was a 1 (or was it the other way around?).

As computers became more powerful, the number of punch cards and binary
instructions became too large for humans to keep up with. Assembler language
was developed. It provided a human-readable language that was translated by
a computer into machine code, combining whole blocks of machine code into
single assembler language commands. Now people could type instuctions into a
file, and compile that file into machine language that the computer could
execute. At this point, a program interface was still lines of command-line
text.

As computers became even more powerful, programs became larger, and did much
more. Higher level languages such as COBOL, Fortran, Pascal, and C were
developed for the purpose of making programmers more productive. They
combined blocks of assembler code into functions that contained
commonly-used operations, and data types that contained larger blocks of
data in a single container. These functions and data types could then be
combined to create a large variety of applications, applications that had
graphical interfaces, could interact with a variety of peripheral devices,
and make life much easier for users, who could perform tasks in much less
time than before. These high-level languages were first compiled into
Assembler, and then compiled to machine code that the computer understands.
To write such an applicaton using assembler only, or worse yet, machine
language only, would take entirely too much time. Of course, with the advent
of new higher-level languages, developers had to learn them and adapt with
the changes.

Networks were introduced, and with them, the Internet, the largest computer
network in human history, a network of millions of computers across the
globe. And computer power continued to increase, doubling every 2 years
(more or less), according to Moore's law. With the increase of programming
power, programs became much more complex. Multi-tasking operating systems
were developed, which could seem to run more than one program at a time via
time-sharing, and Network protocols were developed to allow computers to
talk with one another and exchange data. The complexity of such programming
made procedural programming so complex that it was extremely difficult to
write, debug, and maintain. Security issues demanded that software have
accessible and inaccessible areas, and that these levels of accessibility be
configurable to an increasingly higher degree of precision.

Object-oriented programming was introduced some time around 15 years ago or
so, which included principles such as Inheritance (which cuts down on
redundant code, making program code leaner and simpler to debug, maintain,
and extend), Encapsulation (which provides the ability to hide or expose
data and functionality on a highly-configurable basis), Polymorphism (which
enables classes to be used for a variety of similar purposes), and
Abstraction (which enables developers to treat aggregates of data and
process as real-world "things" or objects). Rather than thinking of data and
process (the essential components of any program) as what they really are,
programmers could think of them as objects, engines, tools, etc. In essence,
this was a continuation of the process of combination that started with
Aseembler language. Functions and data were combined into classes, which
provided larger "blocks" of functionality to the developer. This made
developers able to write even larger and more powerful applications, the
"Boeing 747" of programming technology.

ASP is procedural in nature, and relatively simple to use. It is also simply
not capable of doing a whole lot. It is the "single-engine small aircraft"
of web developers. Note that single-engine small aircraft are still in use,
and useful today, for very limited purposes, just as ASP is still in use,
and useful today, for very limited purposes. ASP.Net is more powerful by an
exponential factor, faster, and has the ability to do anything that any
compiled programming technology can do. This makes it more complex, and
harder to learn. That is simply the way things are.

If you don't have "the right stuff" to learn it and deal with it, stick to
ASP. It will be around for a long, long time, just like small aircraft. But
realize this: ASP.Net is not designed to be used like ASP, any more than a
Boeing 747 is designed to be used like a single-engine small aircraft. It
doesn't need to be "dumbed down" to a procedural level. It is designed
extremely well for what it is. It doesn't need "fixing." If you don't like
to "hang ten" on the big waves, the swimming pool is still there. But if you
want to ride the curl, don't argue. Listen and learn.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
When you can snatch this pebble from my hand, you will have learned.
 
M

michaelcoleman72

John - You are a hack. Retire from computing and think about going
into another profession... such as farming... oh wait, they user
air-conditioned tractors now instead of horses. You probably wouldn't
like that.

In that case, buy a book on OO programming. Read up on the .Net
framework or the Java Enterprise framework... maybe you will get a
clue.

I'll let you get back to rubbing your sticks together so you can fire
up and cook breakfast.

Regards
Coleman
Senior Developer

"Matches...we don't need no stink'n matches!!!"
 
J

John Rivers

Hmmm,

maybe it isn't clear what i am suggesting:

I want to use the object oriented language features of C#
in conjunction with html literals to create a presentation class
library
which is predictable (ie: it outputs exactly the html that i expect -
not
some indeterminate output decided by microsoft and which changes
based on all sorts of external factors (http request headers etc.)

although i can do this using Response.Write(@"<html") this is
not acceptable as I lose: syntax coloring, code completion and
most importantly the ability to keep html and javascript in its
real format without double escaping.

the reason i am upset is because there is no reason for microsoft
to make it hard for me to do this, if it suits my needs, which it
does.

what i am suggesting has all the benefits of your approach
but with none of the drawbacks.

does somebody, please, understand my point?
 
A

Alvin Bruney - ASP.NET MVP

I couldn't see your original thread, but based on this thread, it isn't
particularly difficult to do. One approach is to write a Httphandler to sit
out front to prepare the context, or tie into the html renderer - a better
idea since what you are interested in is html presentation - to prepare the
outputted hmtl

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
K

Kevin Spencer

There are 2 different and important reasons for avoiding "code blocks" in
the Page Template. The first is that "code blocks" are not object-oriented,
and therefore do not provide any of the advantages of object-orientation
(see my previous post). The second is that mixing code and presentation
content is bad practice, making the application more difficult (and
therefore more expensive) to maintain, upgrade, and extend.

There is absolutely no reason why ASP.Net cannot, as it exists now, produce
any HTML output you desire. It is not difficult to do, and many of us do it.
All you have to do is learn how to use it correctly to achieve this.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top