Add Attribute (System.Attribute variety) to .aspx page - not the code-behind - asp.net

I am creating a custom Attribute (extending System.Attribute). I know I can put it on another class easily enough by doing the following.
[MattsAttribute]
public class SomeClassWhichIsACodeBehind {
However, I need to be able to test this attribute easily, and putting it in the code-behind would cause a lot of extra effort to get it deployed to an environment which would respond to the behavior of attribute.
What I would like to do: declaratively apply this attribute to the .aspx page itself (which is really just another class that inherits from the code-behind). Is this possible? If so, what is the proper syntax for doing this?

Check out this post from ScottGu:
http://weblogs.asp.net/scottgu/archive/2005/08/02/421405.aspx
You'll have to extend Page, but it looks about the closest you can come to doing what you want without putting something in a CodeBehind.

Related

Using the ASP.NET ClientServerManager method or property to get name of control

Does the ASP.NET ClientServerManager provide some method or property to return the name of an ASP.NET control in the generated html page that could be used to write the javascript (using RegisterClientScriptBlock) in the code-behind? The actual generated control names can be quite long and unknown (I am also using master pages). I would like a generic way to write the javascript text and have the actual names of the controls be added to the javascript string. I would expect some method that I pass in the name of the control and it returns the actual html control name. I have searched in the documentation of the ClientServerManager and could not find anything.
Control.ClientID is rendered as html tag ID, so you can use that property.
If you are worried about control names, check out ClientIdMode where you can specify this and make it much simpler.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Would this help your issue?

How to Consume a Custom Server Control

I seem to be having a lot of trouble with this.
I want to create an ASP.NET control that implements some core logic, and then I want to be able to derive several controls from that, which each implement their own specialized logic.
I started with a User Control, but I couldn't find a way to derive from it. I tried setting the Inherits attribute in the derived control but, no matter what I did, the derived control just didn't seem able to recognize the base control.
So then I tried a custom server side control by using a regular class that inherits from Control. (Note that all my rendering is done from code.) But I can't seem to find any way to get a page to recognize the control. I've tried different syntax in the #Register directive but either it tells me the src attribute is missing or it just can't find the control. (Note that I prefer not to create a separate assembly if I don't have to.) I have no idea what to put as the assembly if the control is from the current assembly.
Can anyone make suggestions on this? Any examples that would work for my configuration, or perhaps a different approach entirely?
Note that I am not currently using page/control code-behind. All my page scripting is stored in the same file as my markup.
In fact, despite various error messages about missing attributes in the #Register directive, I found it works just fine if I reduce the number of attributes in this directive to just tagprefix and namespace.

Programmatic accessing a control from base to derived?

In a recent post, I expressed a need to access the properties for the body using declarative syntax, see
Contentplaceholder for replacing attributes?
I thought the first suggestion solved my problem. But, the syntax confuses the editor which is not acceptable.
My hypothesis for a workable solution is to make the change in code. To derive a class from System.Web.UI.Page with extended functionality. However, I want for the designer to be able to still use declarative syntax to set the body tag.
In other words,
I do not want to have to change any code in my aspx web pages except that they derive from base
I want to be able to set these properties using declarative syntax, merely by adding a tag in the derived page
I'm not immediately sure how to go about doing this because it doesn't exactly fit the OOP paradigm and I'm not sure where the changes need to be made.
Initial hypothesis,
I can use a findcontrol in the base to see if the placeholder has been added. But, not sure exactly at what point in the page processing lifecycle that I can use this findcontrol. I need to set the body before it renders but also be able to grab something from the declarative code.
I hope what I'm trying to accomplish is clear(?) Basically, I want to be able to edit a user defined tag in my aspx page that will change the class for the body in a master page.
Why not create a custom server control (called something like BodyAttributeManager) that does not render anything, but just looks at its attributes and programatically adds them to the body element on PreRender?
Nested tags as mentioned in that example isn't going to work. ASP.NET does not support that. #jball's suggestion is a good idea; having a class outside of the body that can programmably affect the body's settings from code (which is the only real solution here) would give you what you are looking for.
HTH.

asp.net designer how to get the designed page type

I want to find out inside a webcontrol the real type of the page that is designed in Visual Studio 2008.
I can obtain the WebFormsRootDesigner, and somehow i know it is possible to get the
file path of the aspx page.
I would like rather to get the ProjectItem for that page, because it would be an overkill to have the control parse the file, but i cannot find a way to do this.
And from the projectitem of an aspx page i have no clue how to get the page class...
I want my webcontrol to behave differently at designtime depending on the page type.
Thanks
That's a really bad design. It's always bad to have the "inner" behave differently based on the "outer", or the "child" to be based on the "parent".
Instead, have the page tell the control how to behave, by setting a property. Different pages will tell the same control to behave in different ways. This way, if you add a new page, it can still choose to use one of the existing behaviors.

asp.net: Inheriting from controls that use embedded resources and me.GetType()

I'm having some trouble trying to inherit a control that uses an embedded resource.
the trouble is - this control uses me.GetType() instead of GetType(ControlName).
now when I try to use the derived control, it looks for the resources in the derived control's assembly, instead of the base control assembly, and obviously - doesn't find them.
how can I fix this?
Since the call is in the base control's definition, there isn't much you can do if you can't override the method where its called; you could duplicate the resource file if possible, or try to override that method where the resource is being called. It's hard when you don't have control over the base class.
It's also hard to advise when I don't know what the code looks like; if you could post the signature of the part of the class in question and refer to this in your question, that would help.
HTH.

Resources