Calling a method on an injected property (from an ASPX page) - asp.net

I'm teaching myself Spring.NET and to see if I've got everything wired up properly, I'm trying to call a method on an object that I have tried to inject into an ASPX page (I know this isn't probably best practice)
This is in my aspx file (so I'm trying to call the 'orderDescription' getter on the orderService property of this aspx page)
[some html here]
<%= OrderService.orderDescription() %>
[some more html here]
In my Web.config, I've got this in my 'spring, objects' section
<object type="Default.aspx">
<property name="OrderService" ref="orderService"/>
</object>
When I run it, it tells me that I need an object instance before I can call a static method, i.e. it thinks I'm trying to call 'orderDescription' as a static method on OrderService. But Spring is supposed to have injected 'OrderService' as a property of my aspx page, so why can't I call it this way.
I know I'm missing something simple but I can't figure it out. Appreciate any tips
Thanks

Here's a summary of what you need to do to get this working. Originally I was going to say that "Default.aspx" wasn't the name of a class, but I guess spring.net must translate that for you. Did you put the SpringPageHandler configuration in your web.config? Also, do you actually have a property named OrderService defined on the class and of the appropriate type? I would expect that if you did, then you would actually get a NullReferenceException (another small note, you said OrderService.orderDescription() is trying to call the getter on the orderDescription property - but you put parenthesis like it is a method call (if it is just a property it should just be OrderService.orderDescription [no parenthesis])).

Related

ASPX Page 'Could not load type' Error

I have an .aspx page, we’ll call it Data.aspx, and it is going to use an ajax request to retrieve a response from a function called GenerateDocument in another .aspx page in the project, we’ll call this Document.aspx. It submits its ajax request, but the request fails, and I’m trying to decipher if it has to do with my page directives on Document.aspx. The entirety of the content on Document.aspx is this (it is not meant to generate a page, only generate documents based on data passed to it):
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Document.aspx.vb" Inherits="NameSpace.Document" %>
In Document.aspx.vb, we have:
Public Class Document
Inherits Generic.WebPage
<System.Web.Services.WebMethod()> _
Public Shared Function GenerateDocument
‘Process data and return as document
End Function
End Class
(There's more than just this; there's also a page load function, etc, but this is the relevant function I'm trying to hit.)
With this configuration, the error we get is complaining about ‘Could not load type NameSpace.Document’
I didn't think I should have to use a namespace that the class does not seem to be contained within, so I tried just saying Inherits="Document", but with the same error loading type message coming back.
I am building the project after I make fixes and getting no build errors, but if I try and change the CodeBehind to a CodeFile attribute, I get runtime compilation errors instead of type errors (I am tracking this through Fiddler).
This seems like it's probably an obvious issue where the function is just not being hit, but I can't seem to parse it. Any help? Thanks.
I figured out the issue. Thanks to all those who responded!
The problem was that while the files were all in the proper directory, one was not added to the project within the solution, and despite the references being in order (Inherits I left pointing to a namespace the class inherited from, plus the class), the program was not finding them at run time. This became apparent when I tried using CodeFile and got compilation errors and realized that it simply did not know what class to point to.
TL;DR - I added the .aspx file to the project and the whole thing worked fine.

Location of HtmlHelper instantiation for ASP.NET MVC

I am trying to debug a problem where HtmlHelper is null in extension methods. Oddly the debugger claims it's fully populated, however I consistently get null exceptions. I'm trying to figure out where HtmlHelper should being instantiated in an attempt to see where my problem may be.
Where should HtmlHelper be Instantiated?
Update: In particular I am trying to implement the extension found here, http://chriscavanagh.wordpress.com/2009/06/11/mvc-authorizedactionlink/, within a masterpage. The error occurs on the MVC template's HomeController.Index(). There are some 'plugins'/virtualpathing that may be causing the problem (trying to avoid this can of worms), but, code the code is essentially the same as found here: http://www.codeplex.com/unifico. However, I don't want to trouble anyone with the details of all of that.
The HtmlHelper is instantiated internally by ASP.NET MVC - it's not something you should generally need to worry about. Exactly where it gets instantiated depends on where you are using it.
The main place it gets instantiated is ViewPage's InitHelpers() method.
In ViewUserControls it gets created on-demand in the getter of the Html property.
In ViewMasterPages it just uses the ViewPage's Html property.
We've encountered this too (oddly, only on 1 of 3 machines), where we have a master page and the content page uses an HtmlHelper.DropDownList. What worked for us was adding using System.Web.Mvc to the master page's codebehind even though it was already in the content page's codebehind.

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

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.

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.

"using" namespace equivalent in ASP.NET markup

When I'm working with DataBound controls in ASP.NET 2.0 such as a Repeater, I know the fastest way to retrieve a property of a bound object (instead of using Reflection with the Eval() function) is to cast the DataItem object to the type it is and then use that object natively, like the following:
<%#((MyType)Container.DataItem).PropertyOfMyType%>
The problem is, if this type is in a namespace (which is the case 99.99% of the time) then this single statement because a lot longer due to the fact that the ASP page has no concept of class scope so all of my types need to be fully qualified.
<%#((RootNamespace.SubNamespace1.SubNamspace2.SubNamespace3.MyType)Container.DataItem).PropertyOfMyType%>
Is there any kind of using directive or some equivalent I could place somewhere in an ASP.NET page so I don't need to use the full namespace every time?
I believe you can add something like:
<%# Import Namespace="RootNamespace.SubNamespace1" %>
At the top of the page.
What you're looking for is the #Import page directive.

Resources