Why use asp controls instead of HTML inputs?

G

Guest

I am coming at the world from the standpoint of a veteran ASP Classic
developer. I am not clear on the advantages of using the ASP.NET control
over standard HTML control.

With the ASP.NET controls the rendered names are changed and I appear to be
limited in what javascript events I can fire. I also can not seem to figure
out how to setup a onsubmit() event.

With standard HTML I can setup any javascript event I want and the names
don't change. I can also use multiple forms and do onsubmit().

To go along with this I haven't figured out the advantage of the validation
controls. It looks to me like it renders a new javascript library file that
is generated dynamically every time the page fires.

In my ASP classic developement I have a set of library files/functions that
handles validation and then displays a nice validation message as well as
changes the label color for input fields.

Many of my forms make use of a great deal of dhtml to hide/show different
elements on the page as needed. I make use of callbacks so I don't ever have
to do a postback because I don't want the redraw and I don't want to have to
try to setup the page (tabs, show/hide elements, expand/collapse parts of
trees) to do validation.

Can I get this kind of flexibility out of asp.net controls? Or should I
stick with HTML controls and using the good old request.form() on post back?

Lastly, the market that I develope for uses browser from IE5 Mac up throught
IE7. So I can't have solutions that don't work on all browsers. (I might be
able to abandon IE5 Mac, but Safari will have to stay).

I would love any input you all have. I am trying to understand this new
world and don't want to sell any part of it short because it doesn't look
like what I am used to.
Thanks,
Shawn Ramirez
www.dynacal.com
 
A

Aidy

It's basically trying to allow people to code web sites without knowing how
web sites work. RAD gone a little bit crazy.

The major advantage with ASP.net controls is that you don't have to use the
Request.Form much anymore, controls retain their values when the form
submits and you refer to them like WinForm controls. The other main
advantage of doing things the asp.net way is that your aspx page has no code
at all, just the controls, and your code-behind has all the code.

It takes a while to get your head around it and get used to it but you get
there in the end.

If you need to render literal HTML then do so, I use a mix where I use
controls if I can, and if I can't then I can't. Like you say, a site that
needs DIVs hidden/shown will often have you writing out literal HTML to give
the DIV's known IDs etc. Just use the tools to make your project work.

Ditto I don't bother with the validation controls, or TBH 90% of what is new
in 2.0. Most of it is for people to produce sites without knowing how to
code, so don't feel bad if you think you're not using everything it has to
offer. RAD development is not good development.
 
K

Kevin Spencer

ASP.Net has many advantages over Classic ASP, and is much more powerful
overall, but the additional power requires a corresponding amount of work to
master. For one thing, ASP.Net is object-oriented, and that means a lot. If
you're not familiar with OOP, I suggest you read up on it. The advantages of
OOP over procedural programming are many. Basically, OOP extends the idea of
functions and structures in procedural programming, which are based on the
idea of creating reusable "components" that you can employ without having to
re-invent the wheel for every new project. Objects are encapsulations of
both process and state, with extra features built in that simplify
programming overall. But I digress.

It isn't necessary to jump on the ASP.Net bandwagon immediately, but at some
point you will have to adapt to the new object-oriented paradigm. The
procedural paradigm does not have legs any more, and Classic ASP, along with
other procedural technologies will, for most of us, die out in a few short
years.

ASP.Net uses a programming model which is more strict than the Classic ASP
programming model. This has both positive and negative consequences, but the
positives outweigh the negatives by a long shot. On the negative side, you
have to familiarize yourself with the programming model, the Control
LifeCycle, and the dozens of essential classes that are used. You are
generally going to be working at a much higher level of abstraction. Once
you have learned the programming model, and familiarized yourself with the
various Controls, patterns, and practices of ASP.Net, it becomes much
easier. You also have the advantage of working in a medium for which there
are stricter standards, which means that not only will your code be more
extensible and reusable, but it will fit in better with the code of other
ASP.Net developers. This can come in handy when you work with a team or
another contractor, and when you need help in the form of a tutorial or a
pre-built component. The strictness of ASP.Net has all of the same
advantages that the strictness of XHTML has over traditional HTML.

ASP.Net works well in any browser, and has capabilities of adapting the HTML
it renders to specific browsers.

As for flexibility, you can get all the flexibility you need out of ASP.Net.
You can go anywhere you want to go, and do anything you can imagine. You are
not confined to using ASP.Net Controls at all times, and indeed, the ASP.Net
programming model employs "static" HTML as an integral part.

Again, you don't have to suddenly move into it. I would suggest
familiarizing yourself with it over a period of time and making a gradual
transition.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
G

Guest

Thank you for your input.

I understand OOP and used it with Classic, as far as it would allow. Being
able to use true collections is a big reason why I am interested in going to
..Net since I had to make my own collection system work in Classic.

I think you are correct in that it is going to take me some time. I think
this is in part because I could do some things in classic with a very few
lines of code that will take more code in .net (like using the attribute
property).

The big thing for me right now is going to be trying to figure out how to do
validation. I still want to do it client side (99% of it), and i want to be
able to have the browser download the page's javascript validation library 1
time, not a new library file everytime the page loads.

Again, thanks for your help.
Shawn
 
K

Kevin Spencer

You can certainly implement your own client-side validation model, and use
an external .js file to do it, if you wish. If you're using ASP.Net,
however, be sure to take the ASP.Net programming model, including PostBack,
into account in your design.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
M

Mark Rae

You can certainly implement your own client-side validation model, and use
an external .js file to do it, if you wish.

That's certainly what I do - wouldn't go anywhere near the validation
controls...
 
G

Guest

Can you give me an few pointers or articles on how you accomplished this?

Thanks for any help you might be able to offer.
Shawn Ramirez
 
M

Mark Rae

Can you give me an few pointers or articles on how you accomplished this?

1) Create a JavaScript file called, say, validation.js and import it into
your project

2) Set a reference to it in the pages which require it:
<script type="text/javascript" src="../includes/js/validation.js"></script>

3) Add the first validation routine to it - the following one checks for
valid email addresses:

function isEmailAddress(pstrEmailAddress)
{
var astrIllegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
if (pstrEmailAddress.match(astrIllegalChars))
{
return false;
}
return
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(pstrEmailAddress);
}


4) Use it in the pages which need it e.g.

<script type="text/javascript">
<!--
function validateForm()
{
if(document.getElementById('<%=txtEmail.ClientID %>').value.length
== 0)
{
document.getElementById('<%=txtEmail.ClientID %>').focus();
alert("Please enter your email address");
return false;
}
if(!isEmailAddress(document.getElementById('<%=txtEmail.ClientID
%>').value))
{
document.getElementById('<%=txtEmail.ClientID %>').focus();
alert("Invalid email address - please enter in the standard
format e.g.\n\[email protected]");
return false;
}
}
</script>


<form runat="server">
<asp:TextBox ID="txtEmail" runat="server"
<br />
<asp:Button ID="cmdSend" runat="server" Text="Send"
OnClick="cmdSend_Click" OnClientClick="return validateForm();" />
</form>
 
G

Guest

Thanks very much for your help.

Shawn

Mark Rae said:
Can you give me an few pointers or articles on how you accomplished this?

1) Create a JavaScript file called, say, validation.js and import it into
your project

2) Set a reference to it in the pages which require it:
<script type="text/javascript" src="../includes/js/validation.js"></script>

3) Add the first validation routine to it - the following one checks for
valid email addresses:

function isEmailAddress(pstrEmailAddress)
{
var astrIllegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
if (pstrEmailAddress.match(astrIllegalChars))
{
return false;
}
return
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(pstrEmailAddress);
}


4) Use it in the pages which need it e.g.

<script type="text/javascript">
<!--
function validateForm()
{
if(document.getElementById('<%=txtEmail.ClientID %>').value.length
== 0)
{
document.getElementById('<%=txtEmail.ClientID %>').focus();
alert("Please enter your email address");
return false;
}
if(!isEmailAddress(document.getElementById('<%=txtEmail.ClientID
%>').value))
{
document.getElementById('<%=txtEmail.ClientID %>').focus();
alert("Invalid email address - please enter in the standard
format e.g.\n\[email protected]");
return false;
}
}
</script>


<form runat="server">
<asp:TextBox ID="txtEmail" runat="server"
<br />
<asp:Button ID="cmdSend" runat="server" Text="Send"
OnClick="cmdSend_Click" OnClientClick="return validateForm();" />
</form>
 

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,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top