.ascx files

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

I have just put together our organisations 'template' for our web
applications and have created 7 .ascx files which when dropped into my
template file work perfectly...however, I have a question...

As our team develops several applications I wanted these generic .ascx's to
be able to be used by all - therefore I've placed them in the root of the
domain we work on in a directory called /WebUserControls

I noticed that the first line of the HTML for each is as follows :

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="Application.ascx.vb" _
Inherits="WebApplication1.Application"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

(please excuse the wrap around)

My understanding of the web user controls is limited, but I believe we can
benefit by these in the following areas :

shared code - no need to reproduce the common parts
cached - so that our pages load faster

In the above there is a reference to the 'WebApplication1' - which was the
name of the project I was working on (default one etc) in visual studio - is
this likely to cause a problem for using these controls? ie, their
applications - which wont be called WebApplication1 will not work with
these? If so - how do I get around this so that we can all point to the
..ascx's in the central location and not all take copies of them etc??

Any information would be appreciated - sorry if some of this doesn't make a
lot of sense - I'm not 100% clued up on .Net yet - so plenty of learning as
I go etc...

Regards

Rob
 
Rob Meade said:
In the above there is a reference to the 'WebApplication1' - which was the
name of the project I was working on (default one etc) in visual studio - is
this likely to cause a problem for using these controls? ie, their
applications - which wont be called WebApplication1 will not work with
these? If so - how do I get around this so that we can all point to the
.ascx's in the central location and not all take copies of them etc??

Any information would be appreciated - sorry if some of this doesn't make a
lot of sense - I'm not 100% clued up on .Net yet - so plenty of learning as
I go etc...

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.

R.
 
...
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
 
Rob, instead of using user web controls (ascx) try using user composite
controls or rendered controls. composite controls are easier to code than
rendered controls(in which you build the control from ground up). They are
made of existing .net web controls. When built, it will create an assembly
(a dll file) which you can use in all your applications. In that one dll
file you can keep all your user controls and you can even put them in your
toolbox.

Onur
 
...


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
 
Back
Top