How do I do these dynamic links in ASP.NET ?

  • Thread starter Thread starter Alan Silver
  • Start date Start date
A

Alan Silver

Hello,

I Classic ASP, I used to have a standard way of making the top-level
site navigation links. I had an include file which included a Sub like
this ...

Sub MakeLink(TocURL, TocText)
If TocURL = ThisURL Then
Response.Write("<br>" & TocText)
Else
Response.Write("<br><a href=""" & TocURL & """>" & TocText & "</a>")
End If
End Sub

(air code, so ignore typos), where ThisURL had been set to the URL of
the current page. I could then do something like ...

MakeLink "/", "Home"
MakeLink "/contact.asp", "Contact Us"

and so on. This include file could then create a set of links for every
page.

Now, how do I achieve the same end in ASP.NET? I guess I could have a
label in the place where I want the links to be, then have a sub in the
Page_Load event that builds a string containing the HTML. At the end of
the sub, the label.Text could be set to the string.

This seems a little inelegant considering the way ASP.NET is set up to
allow structured coding. Does anyone have a better suggestion for this?

TIA
 
Alan:
The community starter kit has a well-setup breadcrump component (but it's
database driven). You might want to check it out.

The way I've always seen it set up is via a user control which you include
atop each of your page. The user control would then be fed off an XML file
or Database where it would use the current page to figure out what
breadcrumb to build. This is more elegant than the MakeLink "xxx", "yyy"
solution because it makes the user control totally self-sufficient as well
as making it easily modifiable via an XML file (as oppsed to having to
hard-code the links on each page)

You can also get free controls to do this for you:
http://www.loudcarrot.com/webcontrols/navpath.aspx

Normally it's done simply by reading data into some type of collection (say
an arraylist) and binding that to a datagrid...

Karl
 
Alan:
The community starter kit has a well-setup breadcrump component (but it's
database driven). You might want to check it out.

Thanks, I've already got the kit, but haven't made any headway into it
yet. It's very daunting at first, and as I'm still finding my feet with
the whole ASP.NET idea, I decided to leave it for the moment.

I'll go and see if I can find this component. Hopefully it should help
me out.
The way I've always seen it set up is via a user control which you include
atop each of your page. The user control would then be fed off an XML file
or Database where it would use the current page to figure out what
breadcrumb to build. This is more elegant than the MakeLink "xxx", "yyy"
solution because it makes the user control totally self-sufficient as well
as making it easily modifiable via an XML file (as oppsed to having to
hard-code the links on each page)

That's a very good idea. I don't have a problem with having the links in
the include file (as it was in Classic ASP), but I can see advantages in
using an XML file. Self-generating site guides immediately spring to
mind ...
You can also get free controls to do this for you:
http://www.loudcarrot.com/webcontrols/navpath.aspx

Thanks, but I'd like to get to grips with as much of it myself as I can.
I'd like to get experience using just the MS controls and see how much I
can do. You don't really learn as much with other people's.
Normally it's done simply by reading data into some type of collection (say
an arraylist) and binding that to a datagrid...

Thanks for the reply.

One other point that struck me since I posted. The include files used to
include part of the header as well as the top bit of content. This was
convenient as I could have the META tags for author, etc, as well as the
link to the CSS file all in the include file. That way all pages used
the one source, making it easy to change. If I use a custom control to
do this in ASP.NET, can I still include parts of the <head>? If I
understand right, controls have to be in a web form, and you can only
open one of those once you are inside the <body>. If so, how do you get
common parts for the <head>? Do I go back the old include files, or is
there a better way?

Thanks again
 
The community starter kit has a well-setup breadcrump component (but
it's database driven). You might want to check it out.

Karl,

Just been looking for this, and I can't find it. I'm a bit lost in there
to be honest. Please could you tell me what I'm looking for?

Thanks
 
See:

http://www.codeproject.com/aspnet/Localized_Breadcrumbs.asp

http://www.codeproject.com/aspnet/breadcrumbs.asp

http://www.search-this.com/website_design/ASP.NET_breadcrumbs_Print.aspx

for sample code.

Breadcrumb navigation allows you to display the current page's
context within the site structure. The benefit of a breadcrumb is
that it makes obvious the ways in which the pages have been
grouped and allows the user both to navigate between these
pages and understand the site arrangement structure.




Juan T. Llibre
ASP.NET MVP
===========
 
Juan,

Thanks for those, there's plenty to keep me quiet for a while!!
See:

http://www.codeproject.com/aspnet/Localized_Breadcrumbs.asp

http://www.codeproject.com/aspnet/breadcrumbs.asp

http://www.search-this.com/website_design/ASP.NET_breadcrumbs_Print.aspx

for sample code.

Breadcrumb navigation allows you to display the current page's
context within the site structure. The benefit of a breadcrumb is
that it makes obvious the ways in which the pages have been
grouped and allows the user both to navigate between these
pages and understand the site arrangement structure.




Juan T. Llibre
ASP.NET MVP
===========
 
Phil,
Wow, looks like the community portal has really changed since last I looked
at it. I don't remember it being a server control, but you can find their
breadcrumb in
Engine\Framework\Sections\Controls\BreadCrumb.cs.

The problem is that unless you are familiar with how the sections work,
you'll be a little loss.

The free control that I pointed you to has free source code, so again, you
might wannat ake a look at it.

As for your question about the <head></head>...it isn't true that
server/user controls must be placed inside <form></form> Some controls,
namely those which participate in viewstates must. So if your user/server
control has a textbox, linkbutton, button, ... then yes it must be in a
<form></form>...if you are only rendering HTML such as <head>, <title> and
<meta> you should be ok

Cheers,
Karl
 
Phil,
Wow, looks like the community portal has really changed since last I looked
at it. I don't remember it being a server control, but you can find their
breadcrumb in
Engine\Framework\Sections\Controls\BreadCrumb.cs.

Thanks, now I know where to look ;-)
The problem is that unless you are familiar with how the sections work,
you'll be a little loss.

A "little"? Try a lot!! There are so many files there, it's hard to know
where to start.
The free control that I pointed you to has free source code, so again, you
might wannat ake a look at it.

Thanks, I did take a look. I didn't realise at first that they included
source code. That changes things as I can now learn from them.
As for your question about the <head></head>...it isn't true that
server/user controls must be placed inside <form></form> Some controls,
namely those which participate in viewstates must. So if your user/server
control has a textbox, linkbutton, button, ... then yes it must be in a
<form></form>...if you are only rendering HTML such as <head>, <title> and
<meta> you should be ok

I realised this last night when I remembered using a Literal control to
set the <title> of a page. It occurred to me that this wasn't in a form.

OK, so let me phrase the question slightly differently then. In Classic
ASP, I used to create an include file that had part of the <head>
(common bits like some META tags, the link to the stylesheet and so on),
the closing </head> tag, the opening <body> tag, and the parts of the
HTML that were common to all pages (which usually included the
navigational links).

In ASP.NET, I think this might be a problem. My current understanding is
that the best way to do the common code part is to put it in a custom
control, and use that control on the page. The problem would be that the
main page would not have the </head> and <body> tags in it. Wouldn't the
ASP.NET engine take an exception (no pun intended) to that? From what I
have seen (haven't got it clear enough yet), it likes to generate some
of these bits itself. Am I wrong here? Please clarify.

Thanks
 
The way I've always seen it set up is via a user control which you
include atop each of your page. The user control would then be fed off
an XML file or Database where it would use the current page to figure
out what breadcrumb to build.

It occurred to me last night, won't this mean that *every* single page
request will involve a file read or database query? If so, won't that
have a serious performance impact? I know my original suggestion of
having the links inside the custom control doesn't have the flexibility
of the XML file, but it would be very fast.

Any comments?
 
You could place the DataSet in Cache. If you are pulling from an xml file,
you could place a File Dependency to the xml file driving your cached
DataSet.

If it is coming from the a database, you could still cache it. 2.0 will
have a database cache feature built in, but there are some samples out there
for database cache dependency in 1.1

I couldn't find the link I used to write mine, but here are some MSDN links:

http://msdn.microsoft.com/msdnmag/issues/03/04/WickedCode/

http://msdn.microsoft.com/msdnmag/issues/04/07/CuttingEdge/default.aspx

bill
 
You could place the DataSet in Cache. If you are pulling from an xml file,
you could place a File Dependency to the xml file driving your cached
DataSet.

That's clever. Caching is a new one on me (as is everything at the
moment!!). I had a brief look at the pages below, and the second one
seems to have what I need, although I think it will need some careful
reading before I feel confident enough to use it.

However, it does mean that I can get on with looking at using an XML
file for the data and not worrying about killing the server. When I've
got the basic functionality working, I can then look at caching the data
to improve performance.

Thanks for the info and links.
If it is coming from the a database, you could still cache it. 2.0 will
have a database cache feature built in, but there are some samples out there
for database cache dependency in 1.1

I couldn't find the link I used to write mine, but here are some MSDN links:

http://msdn.microsoft.com/msdnmag/issues/03/04/WickedCode/

http://msdn.microsoft.com/msdnmag/issues/04/07/CuttingEdge/default.aspx

bill
 
Alan:
ASP.Net probably won't care, but VS.Net might not like it very much. An
alternative you might not have considered is to use master pages for the
common functionality:

http://www.metabuilders.com/Tools/MasterPages.aspx

Karl,

Thanks for the link. I think I'm suffering from information overload at
the moment!! I had a look at that page, but I think it needs printing
out and reading carefully.

I'm going to have a go at the XML file approach first as that looks very
promising. I might come back to the master pages stuff later.

Thanx again
 

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

Back
Top