Dynamic partial - viewdata null reference error - asp.net

I am running a site using MVC 2 and .net 4.0.
I recently added a new partial view to the site that looks like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<% if (!(bool)ViewData["Master_ShowGuide"] )
{ %>
<%= Html.NavigationButton("Guide", "button", "infoIcon", Url.Content("~/Help/ Guide.pdf"), new { title = "Click here to view an in depth user guide." }, new { target="_blank" })%>
<%} %>
What I didn't notice at the time was the addition of the <dynamic> part to this view, where previously my partials just looked like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
This partial was displayed using:
<% Html.RenderPartial("SubLinks"); %>
on my master page.
This appeared to work ok on my development environment, but when deployed to the server, I now run into issues where my ViewData["Master_ShowGuide"] is null. I know that this is not the case, as it is used in other places on the master page just fine.
Can anyone explain why this might be the case - I have 'fixed' the issue by removing the <dynamic> type from the partial definition, but I'm curious as to the cause.
If it's any help, my site runs in an environment with other sites that are running MVC 3.
As a slight addendum, I would note:
The user does not see this error, it just gets reported back to me (through ELMAH).
This code works ok on my test site, but fails with the null reference error on my pre-production deployment site (which is on the same server as the test site and currently running the same code).
I'm a little perplexed.
An answer was found to this issue (as to the root cause of why it is happening), but I'm still not sure why removing dynamic from the partial view declaration would also 'fix' it. If anybody can explain, I'd be interested to hear it.

Found out what the issue was, going to record it for posterity.
Had a url to an image, being generated in a different partial view that was being output using Html.RenderAction. This url was incorrect, resulting in a 404 error being returned.
This wasn't noticed on my dev system.
On live, with custom errors enabled in the web.config, this 404 error tried to return a 'filenotfound' page that we have. Unfortunately, this error page was using the same master page that was causing the problem in the first place, and it errored out, as did the main 'error occurred' page that you were eventually redirected to.
This resulted in a 500 server error being returned, and this seems to have disrupted the viewdata for the subsequent partial being rendered.
Hooray for firebug, as this helped me to track this down.
Hope this helps you out if you find yourself here at some point.

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.

Visual Studio 2010 BreakPoints not hit on one page but hit on the other

I have a project I inherited on only one of the pages the break points are not being hit. They are not hollow, they are solid and look as if they are going to be hit when I run the code, but when the page loads (one of the break points) it does not break. Now I added a break point to a different page (onLoad) and it does it. I have never come across this before, does anyone one have any insight on this crazy visual studio behavior?
I have tried to clean and rebuild the project and still not working.
aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Screener.aspx.cs" Inherits="Screener.Screener" %>
.cs page:
namespace Screener
{
public partial class Screener : System.Web.UI.Page
One thing I just noticed, the code behind page is 1633 lines long, should that matter?
I just deleted the old page and re-created it and still nothing.
There is another way to force a breakpoint (in code)
Add the System.Diagnostic namespace to your code behind
add an additional line of code Debugger.Break();
This will force the debugger to break (if running) otherwise a dialog box will appear asking to debug. Should identify if this is an issue with visual studio caching something or the code that you wish to debug is not being called.
Do not forget to remove from code when done.
http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx

Syntax error caused by <%# Page Language="c#"%> ? ASP.Net

Is this invalid to put in an aspx file? I have some static aspx pages and I want to add a bit of C# to one of them. How can I do this?
I figured just adding
<%# Page Language="c#"%>
and then using <% %> to put a bit of C# goodness in there, but it says Syntax Error. with a blue wavy line over this code.
This happens whenever you change the Language attribute of the Page directive. Simply close the file in Visual Studio, and reopen it. The error will go away.
It's not entirely clear from your question, but are you adding multiple Page directives? You can only have one per .aspx file.
Otherwise what you've got there is valid. If your page works fine and VS is still showing it as an error, quit out of VS entirely and restart it.

NIGHTMARE (Night of the Living Dead - WAP conversion): Endless Loop of Reference/Reciprocal errors (Impossible Puzzle to Solve?)

Please read this carefully in its entirety and think about it before replying. It might be a simple fix but I highly doubt it. You have to understand the scenario here and conflicts I'm explaining fully to be able to help me.
So I have copied over our web site code to a WAP project. I got rid of any reference errors. Believe me I've checked every single error. Now here comes my problem and hypothesis (it can't be solved):
We have a master page which has a bunch of public properties in it.
Almost all our .aspx.cs code is referencing some sort of property from Master and using it.
However, on the flip side, our master page cannot recognize any of my user or custom control tags that I've registered in the Master page. The register tags are fine. These were all referencing just fine together in a Web Site Project (both the master page was referencing its references to some user and custom controls and any .aspx.cs was referencing any master page public properties jsut fine).
So, if you think about this, this could be an endless loop that can never get resolved in this WAP conversion. Why? Because the way I see it, if my Master page can't reference the few user controls or custom controls in it, my master page will not compile. If the reason that my master page cannot compile and reference these user and custom controls is because there are "other errors" and those other errors being that .aspx.cs files error out because they get errors referncing the master page public properties then we have a real problem. It's like both are erroring out because both relate to each other and have problems that cannot get resolve because they're related! Do you catch my drift?
Let me give an example / analogy to put this into perspective.
MyMasterPage.aspx
(example code in it)
<%# Register Src="~/WebControls/User/Navbar.ascx" TagName="Navbar" TagPrefix="ac" %>
<%# Register TagPrefix="am" Namespace="[ourglobalnamespace].Controls.Custom" %>
<tr>
<td valign=top runat="server" id="tdNavBar"><ac:Navbar runat="server" id="navbar"></ac:Navbar></td> (this references a user control and is blowing up)
</tr>
...
<tr>
<td><am:NavigationPath runat="server" id="navPath" Seperator="»" /></td> (this references a custom control and is blowing up)
</tr>
Right now I get errors saying it doesn't recognize either of the controls above for whatever reason I cannot understand
MyMasterPage.aspx.cs
public BL.Store Store
{
get { return this.store; }
}
public BL.Product Product
{
get { return this.product; }
}
just showing some public properties we're exposing through the Master Page's code behind. These properties are referenced and used in .aspx.cs code behind classes throughout our code.
Example .aspx .cs using the Master Page Properties:
protected override void OnInit(EventArgs e)
{
int productID = Master.ProductID;
product = Master.Product;
protected override void OnInit(EventArgs e)
{
int productID = Master.ProductID;
product = Master.Product;
...
So imagine I now have 1500+ errors mainly due to 2 reasons and they are not becuase of reference errors (I fixed all that):
1) Master Page cannot compile
2) Code-behind for .aspx pages blow up because Master page cannot compile therefore any references to Master Page properties cause the code behind to blow up
how the hell would I be able to resolve this if both errors practically rely on each other to be fixed? It's impossible!
3) Designer files cannot be generated for those remaining files that have these problems. So I am not able to right click the Master Page and those user controls in order to convert to web application so that it will successfully create the .designer.cs files for them
so we really have 3 problems (3rd being an after effect fo 1&2) that I have no idea how to resolve here all stemming from the Master page blowing up but could be blowing up because the code-behind pages are blowing up that reference the properties of the master page that is also blowing up! Do you catch my drift?
I hope it's something stupid simple that I've overlooked but I've spent a couple days on this and I see nothing else that will allow me to resolve this mess.
Someone shoot me to get rid of this conversion pain. If anyone can help me resolve this I swear I will send you a fing case of Beer!
In my experience, most of the pain from converting a web site to a WAP is due to namespace mismatches. Check that your master page and pages that use it are in the same namespace. And verify that the ,aspx files reference the correct namespace for the code behind files in the Inherits attribute.
Perhaps consider this to help troubleshoot.
Create a new test project.
Copy your Master page into that.
Solve any reference problems there.
Get a successful build with just the Master
Add Existing Item for your webcontrols.
Add Existing Item for one of your content pages.
In my experience, there's normally a "true" error buried somewhere within the pile. They cn be quite hard to find though!
If you really think you've got circular build problems, try excluding items from the project until you get something that will build, then slowly add them back in. You may have to comment out methods or method bodies if you've got a circular reference between some classes.
Edit to add:
Circular references aren't allowed between projects, but within a project you can have them (sounds like that's what you've got). They're fairly common, and the build process can normally cope.
You also claim to have 1500 errors - I thought that Visual Studio stops counting at about 200? Wherever the limit is, you've probably hit it. Perhaps your "true" error is not showing up in the Error List.

Page_Load Is not Hit in SubFolder Default.aspx after Redirect to that Page

I have a page that sites in a first level folder from my root called default.aspx.
I'm redirected to that page like as so:
I have an initial root Default.aspx that holds a login button
User clicks the login button and it redirects to Facebook Login
After logging in, Facebook redirects back to my first level Default.aspx (sits inside root\firstlevelfolder\Default.aspx)
The page_load is not being hit. This is a .NET 2.0 solution in VS 2008. The AutoEventWireup is set to true in the page directive.
Not sure why and have not seen this error before. Does it have something to do with redirecting to a non root-level .aspx page? This is probably something fundamental but I"m not sure what it is.
breakpoint in the first line of code
in my page_load
You're using a breakpoint. Double-check that Visual Studio is in debug mode and your browser is pointing to localhost:1234 (where 1234 is some random port number assigned by the VS web server). Also, try doing a simple Response.Write("Hello World") in Page_Load(), and see if anything prints out on the top of the page.
You said to try the trace in the root.
No. Try it in the page that you think that Page_Load isn't firing on.
Also, on StackOverflow, it is much easier for others to follow the conversation, and it makes for a cleaner post if you can reply to questions using the "add comment" feature. The reason I'm posting another answer is just in case you're not aware of the feature and you miss the comment. See the SO Community FAQ item regarding comments.
Put a Trace="True" in your <%# Page %> line in the "first level" Default.aspx. The part that you would want to look at is the second section - "Trace Information". See if there is a "Begin Load" item in that list.
Also, is your page inheriting from the System.Web.UI.Page object, or do you have a custome base page?
public partial class _Default : System.Web.UI.Page //or do you have a different object here?
{
...
}
Does Page_Load get hit if AutoEventWireup is false?
Does the page that you are redirecting to work if you hit it directly from your browser without an intermediate page that redirects you there? I don't think the cause of your problem has to do with the fact that the page is in a subdirectory.
If the trace is showing that Begin Load is firing, then what methodology are you using to determine that Page Load doesn't work?

Resources