In article <OW**************@TK2MSFTNGP11.phx.gbl>,
ro**********@NOSPAMubht.swest.nhs.uk says...
"Richard K Bethell" wrote ...
Yeah, unfortunately, unless your entire web site is one big asp.net
project, you need to make a local copy of the ascx files. It's not quite the same
thing in scope as an include file in that sense (although ascx are
language neutral.) I've tried to get around this too. We rely on good versioning
controls to try and prevent regression problems, etc.
Oh that completely blows! Not your reply - just the lack of reusability
which is all I ever see mentioned about .Net!
This is really a pain in the arse for us now - as it means that our entire
template file which I was hoping to enable all of our developers to simply
grab and it would work fine means that now when they copy it across there is
more chance of bits and pieces being changed, either on purpose or by
accident, and that it will be still a case of having to change EVERY
application if we just want to change one item in the template....
Fek... :o(
Regards
Rob
Hi Rob,
What I have done is created a C# Assembly with a "Shell" Class that
opens template files. I can use this once class on every page, even
through sub directories and it works perfect. I use it on just about
every site I have in ASP.Net.
Below is some snippets form the class I have writte (minus namespace
info):
[ParseChildren(false)]
public class Shell : System.Web.UI.WebControls.WebControl
{
string _title = "";
BaseClasses _base = new BaseClasses(); // This is a class
I use for all my functions that I have to have across almost every
control I develop. The most used one is the GetCtlPrefix() which I have
included after this class
public string Title
{
get
{
return _title;
}
set
{
_title = value;
}
}
System.Web.UI.WebControls.Literal c_title;
protected override void Render(HtmlTextWriter output)
{
Control _ts = new Control();
_ts = Page.LoadControl(_base.GetCtlPrefix() +
"controls/topshell.ascx");
// This will find the <ASP:literal> control in the
topshell.ascx file and change it's value
c_title = (System.Web.UI.WebControls.Literal)
_ts.FindControl("title");
c_title.Text = _title;
_ts.RenderControl(output);
Control _bs = new Control();
_bs = Page.LoadControl(_base.GetCtlPrefix() +
"controls/bottomshell.ascx");
_bs.RenderControl(output);
}
}
public class BaseClasses
{
public string GetCtlPrefix()
{
string _script =
HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();
char[] _scriptProc = _script.ToCharArray();
int dir = 0;
string _ctlPrefix = "";
for (dir = 1; dir < _script.Length; dir++)
{
if(_scriptProc[dir].ToString() == "/")
{
_ctlPrefix += "../";
}
}
return _ctlPrefix;
}
}
Using the combination of these two classes will enable you to create a
custom control that can be accessed by any page in you application. To
set your app up, you will need to do a couple of things first:
1. Make a 'controls' directory in your main application directory to put
the .ascx file in. I had code all of my ascx files in Dreamweaver or
some other text editor with no code behind features. I personally find
that it seems to work better that way. You won't have to worry about
these files being open to the public because IIS will not serve those
files directly.
2. Create the topshell.ascx and bottomshell.ascx files and put them in
the controls directory. To make the Title part work you will want to
include this in your topshell.ascx:
<head>
<title><asp:Literal id="title" runat="server" /></title>
</head>
The class will look for this when rendering the topshell.ascx file.
3. Create your aspx pages with the new "Shell" server control:
<%@ Register TagPrefix="Ctrl" Namespace="YourControlsNamespace"
Assembly="YourControlsAssemblyName" %>
<Ctrl:Shell Title="The title of this page" runat="server">
You can now put any content you want in between these tags,
including other ASP.Net or custom server tags. THe key to this is the
"[ParseChildren(false)]" section above the class.
</Ctrl:Shell>
In addition to the title, you can also add your meta content. I use
ascx files for all of my menus and have a section in mine where I list
the menus and what order I want them displayed and they are put there.
Please note! This will only really work if you handcode your pages. If
you use the VS.Net desinger or WebMatrix, this will not work correctly
without modifiying the html source to make sure everything lines up.
But the beauty of this system is that as long as the topshell.ascx files
and bottomshell.ascx files are present, you can use this in any
application simply by copying the control dll to the bin directory.
Like I said, I use this on almost every site I have done in ASP.Net and
it works like a charm.
If you need to use VS.Net I would probably wait for version 2 with the
master pages feature. You could use this system, but it would be a
pain.
Let me know if you have any questions or have problems implementing it.
Dane Morgridge
Web/Software Developer
_____________________________
DTM Technologies
http://www.dtmtechnologies.net