473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using a custom attribute in aspx pages

Hi!

I have built a custom attribute that tracks my list of things left to
do in my application:

[ToDo("hardie.ca", "2007-07-16", Comment:="Fix recursion")]
Public Class SomeClass ...

Using reflection, I am able to find all attributes within the App_Code
folder:

Assembly a = Assembly.Load("__code")
string alltypes = "";
Type[] types = a.GetTypes();
foreach (Type t in types) {
MemberInfo inf = t;

object[] attributes;

attributes = inf.GetCustomAttributes(typeof(ToDoAttribute), false);

foreach (object attribute in attributes) {
ToDoAttribute todo = (ToDoAttribute)attribute;
alltypes += "<p>" + t.ToString() + "<br>";
alltypes += "Programmer: " + todo.Programmer + "<br>";
alltypes += "Date: " + todo.LogDate + "<br>";
alltypes += "Comments: " + todo.Comment + "</p>";
}
}
My problem is I would like to decorate the classes of Aspx pages, but
because they don't reside within the App_code folder, I can't get a
reference to their assembly. Is there some way to do this?

Thanks,

Chris

Jul 16 '07 #1
4 1960
If you want to decorate the classes of the ASPX pages, then you should
be able to just place it on the class in your partial class file.
Attributes placed on the class there should be on the class as well when the
ASP.NET engine compiles it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ha******@hotmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
Hi!

I have built a custom attribute that tracks my list of things left to
do in my application:

[ToDo("hardie.ca", "2007-07-16", Comment:="Fix recursion")]
Public Class SomeClass ...

Using reflection, I am able to find all attributes within the App_Code
folder:

Assembly a = Assembly.Load("__code")
string alltypes = "";
Type[] types = a.GetTypes();
foreach (Type t in types) {
MemberInfo inf = t;

object[] attributes;

attributes = inf.GetCustomAttributes(typeof(ToDoAttribute), false);

foreach (object attribute in attributes) {
ToDoAttribute todo = (ToDoAttribute)attribute;
alltypes += "<p>" + t.ToString() + "<br>";
alltypes += "Programmer: " + todo.Programmer + "<br>";
alltypes += "Date: " + todo.LogDate + "<br>";
alltypes += "Comments: " + todo.Comment + "</p>";
}
}
My problem is I would like to decorate the classes of Aspx pages, but
because they don't reside within the App_code folder, I can't get a
reference to their assembly. Is there some way to do this?

Thanks,

Chris

Jul 16 '07 #2
Unfortunately, that doesn't seem to be the case. I've decorated a page
with my attribute like this:

[ToDo("hardie.ca", "2007-07-16", Comment = "Do not hardcode the site
ID!")]
partial class admin_Section : System.Web.UI.Page

The attribute has been coded as follows:

[AttributeUsage((AttributeTargets.Class | AttributeTargets.Method),
AllowMultiple=true)]
public class ToDoAttribute : System.Attribute {

// Private member data
private string _comment;

private string _date;

private string _programmer;

public ToDoAttribute(string programmer, string myDate) {
this._programmer = programmer;
this._date = myDate;
}

public string Comment {
get {
return _comment;
}
set {
_comment = value;
}
}

public string LogDate {
get {
return _date;
}
}

public string Programmer {
get {
return _programmer;
}
}
}
But the page that lists all the attributes only finds them when
they're in app_code. I can see all the assemblies generated by viewing
the output of AppDomain.GetAssemblies, but attribute decorations on
partial classes in the ASPX files aren't being recognized.

protected void Page_Unload(object sender, System.EventArgs e) {
AppDomain currentDomain = AppDomain.CurrentDomain;
Assembly[] assems = currentDomain.GetAssemblies();
string alltypes = "";
Assembly assem;
foreach (assem in assems) {
Type[] types = assem.GetTypes();
foreach (Type t in types) {
MemberInfo inf = t;
object[] attributes;
attributes =
inf.GetCustomAttributes(typeof(ToDoAttribute), false);
foreach (object attribute in attributes) {
alltypes = (alltypes + ass.FullName);
ToDoAttribute todo;
attribute;
ToDoAttribute;
alltypes = (alltypes + ("<p>"
+ (t.ToString() + "<br>")));
alltypes = (alltypes + ("Programmer: "
+ (todo.Programmer + "<br>")));
alltypes = (alltypes + ("Date: "
+ (todo.LogDate + "<br>")));
alltypes = (alltypes + ("Comments: "
+ (todo.Comment + "</p>")));
}
}
}
this.lblLabel.Text = alltypes;
}
Anyone have any ideas why I can't decorate the partial class in aspx
pages?

Chris

Jul 16 '07 #3
I think the problem you may be having is that you are using the Web Site
project model, which creates all kinds of separate little assemblies with
funky names that are hard to track. If you switch to the Web Application
Project model, you get a single assembly for all your pages' codebehind and
it will be much easier to track the little boogers.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

"ha******@hotmail.com" wrote:
Hi!

I have built a custom attribute that tracks my list of things left to
do in my application:

[ToDo("hardie.ca", "2007-07-16", Comment:="Fix recursion")]
Public Class SomeClass ...

Using reflection, I am able to find all attributes within the App_Code
folder:

Assembly a = Assembly.Load("__code")
string alltypes = "";
Type[] types = a.GetTypes();
foreach (Type t in types) {
MemberInfo inf = t;

object[] attributes;

attributes = inf.GetCustomAttributes(typeof(ToDoAttribute), false);

foreach (object attribute in attributes) {
ToDoAttribute todo = (ToDoAttribute)attribute;
alltypes += "<p>" + t.ToString() + "<br>";
alltypes += "Programmer: " + todo.Programmer + "<br>";
alltypes += "Date: " + todo.LogDate + "<br>";
alltypes += "Comments: " + todo.Comment + "</p>";
}
}
My problem is I would like to decorate the classes of Aspx pages, but
because they don't reside within the App_code folder, I can't get a
reference to their assembly. Is there some way to do this?

Thanks,

Chris

Jul 16 '07 #4
To advertise my ignorance, how would I go about do that? If I have a
pre-existing project, can I switch over or do i have to create a new
web app project? I guess I don't understand the difference between a
web app project and a website project. Switching over from Python,
still getting my bearings :)

On Jul 16, 1:40 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.yabbadabbadoo.comwrote:
I think the problem you may be having is that you are using the Web Site
project model, which creates all kinds of separate little assemblies with
funky names that are hard to track. If you switch to the Web Application
Project model, you get a single assembly for all your pages' codebehind and
it will be much easier to track the little boogers.
-- Peter
Jul 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: nospam | last post by:
I got three (3) files (1) Untitled.aspx (2) Untitled.aspx.1.cs (3) Untitled.aspx.2.cs These three files must be used together to make file #1, Untitled.aspx, page work via J.I.T. when the...
0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
7
by: Nilesh | last post by:
I am using background-image attribute in a CSS file and linking the CSS file to aspx page. But strangly, background-image attribute is not working for relative URL. e.g. If I apply following css...
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
7
by: Julian Jelfs | last post by:
Hi, I had an aspx pag in .Net 1.1 with a label on it. As such I had a code behind page with a declaration for that label. When I convert to Asp.Net 2.0 the code behind is converted to a...
4
by: Pat | last post by:
In my Web.config i have :- <customErrors mode="On" defaultRedirect="genericerror.htm"> <error statusCode="404" redirect="pagenotfound.aspx"/> </customErrors to get page not found error but...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.