Advice regarding best use of HTML / Web Controls in UserControl

B

Brett

Preface: I am a complete neophyte to ASP.NET development. Unfortunately
the same holds true with respect to ASP development. I do consider myself
advanced as far as OOP development with the .NET Framework goes.

My question: I want to create a UserControl that will become part of a page
template to keep my site consistent. This particular control is to be a
sidebar menu that displays links to the various sections on my site. So far
it looks like the best route is to design the usercontrol with a multi-row
table that houses links to the other sections in each row. My question, I
guess, is:

Should I
a.) use a System.Web.UI.WebControls.Table and programmatically add the
'link' controls to the rows, or
b.) should I create a plain HTML (not server) table on the UserControl and
add the hyperlinks (via the designer) to each row.

If b.) then should the links be ASP.NET server controls or should I do HTML
controls there as well.

The plot thickens a bit because I want client script to execute when the
user mouses over the links (to highlight them). I understand if I use
server side controls then I have to emit the JavaScript code when the page
loads. This seems like a headache (not to mention a processing burden at
each page load for something that will never change.) However I am having a
lot of trouble trying to add HTML controls and coding the javascript into
the HTML portion of the .ascx. I get an "<id> is undefined" error when the
javascript event executes, which really amazes me since the IDE recognizes
it when I am adding the javascript to the HTML code.

Apparently I can't even figure out the proper way to phrase the question
since I'm not finding a lot of help when I search the net. I hoping you
guys will do a better job knowing what I mean (rather than what I say) than
the search engines do.

Thanks in advance,
Brett
 
S

S.Grevi

Brett, I had the same problems about adding Javascript in a .aspx page.

Why a basically HTML based page wouldn't recognize accordingly a normal
Javascript ? Because is not HTML, was my answer : it has placeholders for
controls to be rendered AS HTML but IS NOT HTML, maybe XHTML ?

Anyway, the phylosophy has to be putted away : the only way I found to have
controls the events of which have to respond to Javascript code is using
<control>.Attributes.Add(<event>,<clientJavascriptFunction>) in the
code-behind.

I'll try to answer to your first question :

IMVHO

a) is a good solution if you change often the links and/or the link list is
taken from a db, an XML or other data sources. Server roundtrip is needed.
b) is a good solution for statics and rarely modified links.

So my answer is : it depends.

I shed more light on your problem, or I didn't get the point ?

Tell me.

Stefano Grevi
 
B

Brett

Stefano, you got my point exactly. I just needed to hear how other
*experienced* ASP.NET developers handled this type of scenario to see if I
was on the right track. Thank you very much for your help.

Brett
 
R

Robert Koritnik

Deciding between A nad B choice is not so much about changes (as S.Grevi
suggested), because your UserControl is rendered each time (and recreates
all controls) you access it, but its more about functionality.

If you just need to link to some aspx pages then I strongly suggest U use
normal HTML and render it. Don't use server controls, because they are
slower and use more resources.

But if you have to I don't know change the state of your application (like
area of work when someone clicks another link), then you have to use at
least LinkButton controls.

So the answer (as I see it) is:
- for totally static part of the control (like tables, cells, rows, divs,
etc.) use plain and simple HTML.
- for all other controls that you need to have their state and handle their
server events, use server controls (either WebControls or HTMLControls -
depends on the requirements)...

That's the best way.
 
S

S.Grevi

You are welcome, Brett.
If 'experienced' is a person with only 9 months of development on .NET
technology...

I thought about the side effects of a User Control wrapping an HTML part :
in this case you'll have always the server roundtrip because you put the
placeholder of this control in the .aspx page and then it will be rendered
as a <TABLE> in the HTML for the client, but the server has to do some work
on this.

If you have to control some server events, as stated by Robert, the HTML +
server control solution in this case is needed, giving to you the
'reusability' of the user control in your project.

If you don't plan to reuse the links table and it's the only control you
need...go HTML.
It could be called and call aspx pages easily and it's faster.

Have a nice day, Brett.

Stefano
 
S

S.Grevi

I agree Robert.

I'd add at the list you did only the possibility to 'reuse' the control, so
a 'composition' of simple HTML controls IMHO could be grouped in a User
Control for this. But in this case the server has to do some work, not so
much but some.

Stefano Grevi
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top