"including" a global for all .aspx pages

D

David

is there some way to make a file (.cs) containing some common C# code
classes being loaded on every .aspx page in my web without me having to
declare it every time?
Something in the config file perhaps?

since I have these classes that will be used on every page, I want them to
be like a "cloud hovering over my web" whatever page the user is browsing...
 
D

David

as an example I want to have a code which adds stuff to the <head> control
without having to think about it in my .aspx pages...
 
T

TE

Hello David,

not sure I understand your problem...

"classes being loaded(...)without me having to declare it
every time"
- you could use public "shared" methods in your assembly.
you do not need to instantiate but can simply call them
(stateless).

"as an example I want to have a code which adds stuff to
the <head> control
without having to think about it in my .aspx pages..."
- check this article
http://www.devtrain.de/artikel_774.aspx (sorry it`s german
language, but the code examples seem to be understandable -
if not just tell me)

if you really do not want to touch the aspx pages and want
your "dynamic" content...
- maybe check httphandlers or httpmodules (never tried
this on my own...)

msdn section on httphandlers:
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/cpguide/html/cpconhttphandlers.asp )

msdn-mag article on httpmodules:
http://msdn.microsoft.com/msdnmag/issues/02/05/asp/default.
aspx


Hope this helps!

Regards,

Tom
 
D

David

I am coding with a text editor (not VS) so I'm not using assemblies...

I think the solution will be to use src="initpages.cs" in the @page
directive and have a Page_Load() event handler in that .cs file that would
append neccesary header tags etc...

currently working on it, if the idea is ridicilous maybe you let me know as
a .Net newbie I easily do stupid things :)
 
D

David

darn I cannot find out how to add attributes to the Page header under
Onload(), this is my current try... anyone please?

using System.Web.UI;
public class Initialiser : Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Control.Page.Attributes.Add("name", "David");
}
}
 
T

TE

Hi David,

what exactly are you trying to achieve?

Do you want to set META-Tags from one specific source
using inline-code (no code behind) ?

If so -

just store your MetaTag Informations as string in an
ApplicationVariable and write them
using "response.write")...

in global.asax add (application_onStart):

Application.Add("MetaInformation", "<META
NAME=""Author"" CONTENT=""David"">")

In your aspx file add (at the place you want the output -
so maybe in the <HEAD> Section...):
<% response.write(application.item
("MetaInformation")) %>

Hope this helps you!

Tom
 
D

David

thx Tom, good point but actually I'd like to do it with code behind. I'm
trying to keep code & html as much seperated as possible. There's something
out there I'm not grasping since I can't access the header object from my
code behind, as simple as the code is:

---default.aspx---
<%@ Page Language="C#" Inherits="Initialiser" Src="initialiser.cs" %>
<head id="MyHeader" runat="server"></head>
testing testing


---initialiser.cs---
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class Initialiser : Page
{
private void Page_Load(object sender, System.EventArgs e)
{
MyHeader.Attributes.Add("Title", "dsafadsf");
}
}
 
T

TE

puh - now i understand what you want ;-)

you are trying to do as described in the link, mentioned
before ( http://www.devtrain.de/artikel_774.aspx )

....

your code (HTML):

<head id="myHeader" runat="server"></head>

your code (codebehind) :

PageLoad:

Dim myTag As HtmlGenericControl = New
HtmlGenericControl("meta")
myTag.Attributes.Add("name", "Author")
myTag.Attributes.Add("content", "david")
myHeader.Controls.Add(myTag)


## dont forget ##
you have to add:

Protected myHeader As HtmlGenericControl

to your page controls declaration....


works as desired? hope so!

;-)

Cheers,

Tom
 
D

David

thx again Tom exactly what I'm trying just in c# :)
there's something wrong with my c# syntax...


public class Initialiser : Page
{
protected HtmlGenericControl myHeader = HtmlGenericControl("meta");

private void Page_Load(object sender, System.EventArgs e)
{
// myHeader.Controls....
}
}
 
D

David

this will be my last, I'm getting hilariosly longposted on this topic!
but it seems the namespace is missing;
namespace.HtmlGenericControl myHeader = HtmlGenericControl("meta");


so the final question will be; how can I find the namespace of my aspx
page??
 
T

TE

Hi David,

sorry, don`t really understand - but:

Namespace for the HTMLGenericControl:
System.Web.UI.HtmlControls

"Connection" between your aspx and your codebehind (which
is actually compiled into an "assembly"...) :

<%@ Page Language=... AutoEventWireup=...
Codebehind="Codebehind.aspx.vb"
Inherits="YourNamespace.Codebehind"%>

Hope that solves your problem - if not just send/post your
code ;-)

Good night,

+++Tom
 
D

David

Tom using that for a namespace gives me this error:
Compiler Error Message: CS0118:
'System.Web.UI.HtmlControls.HtmlGenericControl' is a 'type' but is used like
a 'variable'

The code is
<%@ Page Language="C#" Inherits="Initialiser" Src="initialiser.cs" %>
<head id="myHeader" runat="server"></head>
testing testing
---------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class Initialiser : Page
{
protected System.Web.UI.HtmlControls.HtmlGenericControl myHeader =
HtmlGenericControl("meta");

private void Page_Load(object sender, System.EventArgs e)
{
// myHeader.Controls....
}
}
 
J

John Saunders

You want:

<%@ Page Language="C#" Inherits="Initialiser" Src="initialiser.cs" %>
<head id="myHeader" runat="server"></head>
testing testing
---------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class Initialiser : Page
{
protected HtmlGenericControl myHeader;
private void Page_Load(object sender, System.EventArgs e)
{
// myHeader.Controls....
}
}
 
D

David

John;

Parser Error Message: The base class includes the field 'myHeader', but its
type (System.Web.UI.HtmlControls.HtmlGenericControl) is not compatible with
the type of control (System.Web.UI.HtmlControls.HtmlHead).


:(
 
J

John Saunders

David said:
John;

Parser Error Message: The base class includes the field 'myHeader', but its
type (System.Web.UI.HtmlControls.HtmlGenericControl) is not compatible with
the type of control (System.Web.UI.HtmlControls.HtmlHead).

Ok, change it to type HtmlHead.
 

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,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top