asp.net calling code inline not working - asp.net

In my code behind, I can do the following in the onload:
string x = Fmg.Cti.UI.Utilities.Classes.ResourceController.ReadResourceValue("Riblet", "Riblet_Chicklet1_Button_text");
This works without issue.
In my aspx page (I didn't remove the code from the onload), I put this:
<%= Fmg.Cti.UI.Utilities.Classes.ResourceController.ReadResourceValue("Riblet", "Riblet_Chicklet1_Button_text")%>
When I do, I got an error:
error CS0234: The type or namespace name 'Cti' does not exist in the
namespace 'Fmg' (are you missing an assembly reference?)
I had this (or something quite similar) working. I don't know how I broke it.
Thanks again for your help.

You need to register the assemblies you're using in your page as well (just like usings in your code behind). See Do I have to add "<%# Register assembly=" to every page?

Do you have that namespace imported? For asp.net you can do something like the following to make namespaces available for inline coding.
<%# Import Namespace="Fmg.Cti.UI.Utilities.Classes" %>
You can make namespaces generally available to all of your asp.net pages for inline coding by adding the namespaces into the web.config
<system.web>
<pages>
<namespaces>
...
<add namespace="Fmg.Cti.UI.Utilities.Classes" />
...
</namespaces>
</pages>
</system.web>

Related

ASP.NET error: The page Y.ascx cannot use the user control X.ascx

I am getting the error below when trying to build the web site project in Visual Studio 2010:
The page '/WebSite/controls/C2.ascx' cannot use the user control '/WebSite/controls/C1.ascx', because it is registered in web.config and lives in the same directory as the page.
I have 2 web user controls:
controls/C1.ascx
controls/C2.ascx
The controls have been registered in web.config:
<configuration>
<system.web>
<pages>
<controls>
<add src="~/controls/C1.ascx" tagPrefix="my" tagName="C1"/>
<add src="~/controls/C2.ascx" tagPrefix="my" tagName="C2"/>
</controls>
</pages>
</system.web>
</configuration>
C1.ascx contains just a static HTML, C2.ascx is trying to include C1:
C1.ascx contains just some plain static simple HTML. C2.ascx is trying to include C1.ascx:
<%# Control Language="VB" %>
<my:C1 runat="server" />
<p>Hello from C2</p>
When trying to build the project, I am getting the error message at the top. I realise this issue can be fixed by adding another Register directive to C2.ascx...:
<%# Register Src="~/controls/C1.ascx" TagPrefix="ctl" TagName="C1" %>
...but I'm wondering if there's a cleaner solution and why am I getting the error in the first place?
Your only possible solutions are to:
Move the control out of the directory its currently sharing with outer.ascx, or
Re-register the control inside of the outer.ascx like you already mentioned
Re-write them in code as controls in a separate library
I personally think moving is the easiest, if it will work for your solutions. Second would be re-registering, even though annoying. Abstracting them out into a full code library is probably not worth the effort if this is the only reason you are doing it.
You could also put the controls into different folders. But I don't think this is much cleaner or better.
BTW: this behavior is by design, as you can read on this MSDN page (look for the yellow note almost at the end of the page).

Problem with using the MvcReCaptcha library in a view

The MvcReCaptcha library looks very solid, and appears to have some good developer adoption, but when I tried it for the first time today, using the supplied HTML helper, my app won't run. I have the helper in a partial view as follows:
<fieldset>
<legend>3. Verify that you are a human.</legend>
#Html.GenerateCaptcha()
</fieldset>
I have the namespace included in web.config as instructed:
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="MvcReCaptcha.Helpers"/>
</namespaces>
</pages>
(Other namespaces removed for brevity)
And I have my private and public keys defined in appSettings. I can see no way that I deviate from the examples on the CodePlex page except that I am using Razor. Can anyone offer some insight into what I may be doing wrong?
If you are using Razor you need to add the MvcReCaptcha.Helpers namespace to the <namespaces> section of ~/Views/web.config file and not the one in ~/web.config.

Cannot see new asp.net user controls

I am feeling like an idiot right now, but I cannot for the life of me figure out why I cannot seem to call user controls from within my asp.net webpage. I'm still learning asp.net but I can't find any information from searching on google.
I'm trying to load a specific control on the page when the user presses a linkbutton. So I created an empty user control via the right click menu:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
In other words, I have not touched any part of the created user control. Yet attempting to create this web control and add it to the form seems to not work, as it claims that the WebUserControl class does not exist (I have no other controls in my project):
UserControl blah = new WebUserControls();
produces a "The type or namespace is invalid". Why can none of my asp.net webform pages get the control into scope?
The new control must be added to the site's Web.config file.
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="my"
tagName="WebUserControl"
src="~/WebUserControl.ascx"/>
</controls>
</pages>
</system.web>
</configuration>
Use this to place the new control in an .aspx page.
<my:WebUserControl runat="server" ID="MyWebUserControl" />
In addition to registering them one-by-one in web.config, you can also use a <%# Register %> directive in the source of the markup files that reference the control.
Or, you can move your controls into a DLL and include them all by referencing the assembly from your web.config (takes some extra work, though).

ASP.NET error BC30456: 'Title' is not a member of 'ASP.views_controllername_viewname_aspx'

My ASP.NET MVC application fails with this error:
BC30456: 'Title' is not a member of 'ASP.views_controllername_viewname_aspx'.
But, Title doesn't appear anywhere except the first line of my View.
<%# Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of SomeThing)" %>
My strongly-typed view was of a non-existant type. I had changed the object's name.
<%# Page ... Inherits="System.Web.Mvc.ViewPage(Of CorrectThing)" %>
vs.
<%# Page ... Inherits="System.Web.Mvc.ViewPage(Of WrongThing)" %>
I've had this problem in a MVC application that reference entities from another project.
The solution was to add the assembly in web.config
< add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
Thought I post in case someone else runs into the same problem.
I get this all the time, but my solution is not one of those already supplied, so here it is for anyone else getting the same problem.
Check to make sure you are passing your model to the view from the controller method:
Return View(Model)
instead of:
Return View()
I find this causes the BC30456 error when the view is expecting a list of entities, something like:
... Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of Mydata.DataEntity))"
Almost for got the other solution!
Make sure your web.config file has an entry for system.data.entities in system.web, compilation, assemblies. The following example is taken from a MVC2 Framework 4 project:
...
Hope that helps someone!

How to use extensions and utility methods in markup?

Ok. This is probably a really stupid question, but I'm going to ask it anyway...
How can I use extensions and utility methods in my ASP.Net markup? For example, (say) I have a DateTime extension method called "ToExampleString()" (contained in the DateTimeExtensions class in my Common.Extensions project) and I want to use it in my markup in the ListView's ItemTemplate:
<ItemTemplate>
<span><%# ((DateTime)Eval("DateStarted")).ToExampleString() %></span>
</ItemTemplate>
I'm getting the following error:
'System.DateTime' does not contain a definition for 'ToExampleString' and no extension method 'ToExampleString' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
The page simply can't see the extensions method.
Similarly, how do I make my page's markup aware of a utility class:
<span><%# ExampleUtility.ProcessDate(Eval("DateStarted") %></span>
What steps do I need to take to make this stuff work? I assume I'm overlooking something stupidly obvious?
Thanks
You need to import the namespace either at the top of the page as others have said
<%# Import Namespace="Common.Extensions"%>
Or globally in your web.config
<system.web>
<pages>
<namespaces>
<add namespace="Common.Extensions"/>
</namespaces>
</pages>
</system.web>
If you simply need access to methods of a public module (or static class), just import your application's root namespace.
<%# Import Namespace="Common.Extensions" %>
I believe you can do that for all your markups in the web.config.
You have to import the namespace, at the top of the page:
<%# Import Namespace="Common.Extensions"%>
Namespaces?
You should add using/import directive in aspx markup

Resources