How to use frame on ASP.NET page


How do you correct use frame on a page, so I have a left frame and a right frame, when I click the links on the page presented in the left frame, it loads the according page in the right frame? On top of this, I need to have a master page on all the right frame's pages.

How do I do this? or is there another way to achieve the same effect?


Best Solution

Yup. Frames are evil. You shouldn't really use them.

They cause problems, but in a (very)few edge cases they can be useful and cheaper in terms of development time, they still show up in generated api documentation quite a lot.

But anyway, seeing as how you asked how to use them, here we go

First up, it depends on whether you want to use a frameset, or just put some iframes into a page, iframes maybe easier, but will describe a frameset. Some references below. If you dig around the wayback machine on, you will get to see some examples, also Sun's online java docs used to be in framesets, but have not looked at them for years.

Basically, the contents of each frame are individual pages, and the frames themselves must be named, within the file which contains the frameset, which might look a little like this:

<frameset cols="25%,75%">
  <frame name="_left" src="nav.aspx" />
  <frame name="_right" src="foo.aspx" />

So, for the sake of the excercise, give the left frame attribute name="__left" and name="__right" for the right.

Important bits about links
Any links inside your right frame that need to target that frame should have target= "_self", and any that need to escape the frame and set the location of the parent page, should have target="_top".

The links in your left frame will need to have the attribute target="_right", and that should load the appropriate document into the right frame when the link is clicked.

The rest of it is pretty much normal, treat the contents of your right hand frame as a normal page, make a master page as normal, all the normal html, head, body tags etc. There is nothing really different about frames in aspnet or php or anything else, its just html.

There you have it, there may be a few things I have missed, because they're not something I use very often these days, but sometimes, when accessibility and all that don't matter, they can be a quick and dirty fix. e.g some obscure admin page on a web service site, that will get visited 12 times a year by a geek who understands what is going on.

So, they are evil, but that shouldn't stop you learning about them, you will get to really understand why they are evil, and form your own opinion as to when and when not to use them.