When declaring a code-behind method as private (such as an event handler), the compiler ignores it and outputs:
"Compiler Error Message: CS1061: 'ASP.default_aspx' does not contain a definition for 'OnLoginUser' and no extension method 'OnLoginUser' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)"
In practice, OnLoginUsed does exist, and when the identifier is changed to "public\protected" everything works just fine.
The question is why it is impossible to declare such method as private? after all, it's being called internally by other members of the class.
10x!
Your ASPX page is not the same class as your code behind page. It inherits it and therefore it cannot see the private members. This is why they must be protected or public.
Related
I have a function in my ascx.vb page that i need to be called from the ascx page.
However, I get an error that says its "not declared and may be inaccessible due to its protection level".
I am thinking that for some reason the functions of my ascx.vb page cant be seen from my ascx page.
I actually have this code working in aspx pages without a namespace. Anyone know what is causing this error?
The ascx.vb namespace portion reads like:
Namespace StaffLookup
Public Class Main
Inherits PortalModuleBase
Then the function in ascx.vb is like this:
Public Function ProcessPictures() As String
Return "http://info/scripts/personnel/IDVerify/BadgePictures/transparent.jpg"
End Function
The call in ascx is like this:
<img src='<%# ProcessPictures()%>' />
The only way I have been able to reproduce this error is if there is a second class with the same namespace, class name and method name where the second method has private scope.
The compiler is merging these two classes and the private version of the method ends up being the one used, so you end up with the error you are seeing. There will be warning messages on the class name and method name.
Check your code to see if you have another class with this same namespace and class name, and then see if it has a method with the same signature.
Also check the parent classes up the inheritance chain starting with PortalModuleBase. You can stop looking once you get to UserControl.
Like this:
Namespace StaffLookup
Public Class Main
Inherits PortalModuleBase
Public Function ProcessPictures() As String
Return "http://info/scripts/personnel/IDVerify/BadgePictures/transparent.jpg"
End Function
End Class
End Namespace
Namespace StaffLookup
Public Class Main
Private Function ProcessPictures() As String
Return "http://info/scripts/personnel/IDVerify/BadgePictures/transparent.jpg"
End Function
End Class
End Namespace
Come to find out, the answer is here:
Asp.net controls are not accessible in code behind
Even when creating a brand new empty application you have to right click and choose convert to web app. This worked for me
Listed below is the definition of my function (vb).
I am trying to check the value of a checkbox inside the function. I am currently unable to do this without getting an error stating that
"Error 305: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."
Is there anything I can change so that I can get the value to see that if a checkbox is checked or not? The method must be shared, so it works with the existing javascript and the use of pagemethods
Thanks
<System.Web.Services.WebMethod()> _
Public Shared Function Load(inputs here - taken out for stack overflow) As String
edit- the only other solution i can think of is to find a way for the page's javascript onload to run before the server side, this would solve my issue as well if anyone knows how this can be done.
Yes. Do what the message suggests and make the method non-shared, then you'll be able to access the members of the page.
I am a bit of beginner with ASP.net MVC, and built most of my original app while following a book.
I am trying to downgrade a project because of server limitations.
I have resolved a number of errors, but now I am stuck on this on:
If I build it builds ok, but when I press play, I get this error:
Compiler Error Message: CS0246: The type or namespace name 'dynamic' could not be found (are you missing a using directive or an assembly reference?)
It occurs on this line:
[System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
Line 142: public class views_rooms_index_aspx : System.Web.Mvc.ViewPage, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
Line 143:
Line 144: private static bool #__initialized;
Which I believe probably originates from somewhere like this in my View:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
Is System.Web.Mvc.ViewPage something that you can only do in asp.net 4? What do I need to replace this with for ASP.net 3.5?
The dynamic keyword was introduced in .NET 4.0, and will not be available in any version earlier than that.
I believe the default base class for an mvc view in MVC with ASP.NET 3.5 was System.Web.Mvc.ViewPage, without any generic type. If you want to add a strongly typed model (which is recommended whenever you want to pass data from the controller to the view) you should create a view model class, and replace dynamic with the namespace qualified name of your view model.
Example: you want to pass a string name that you got from somewhere in your controller, to the Home/Index view. Do the following:
Create a class HomeIndexViewModel (name doesn't matter, but this is a good one ;) ) in the Models folder of your project, and give it a public string Name {get; set;} property.
In your controller, insantiate this class and set the name. Pass it to the view using the return View(model); overload.
Make your view inherit System.Web.Mvc.ViewPage<YourProject.Models.HomeIndexViewModel>. You can now access the name using Model.Name in the view.
Your Inherits attribute:
Inherits="System.Web.Mvc.ViewPage<dynamic>"
Has the dynamic type as the generic type - change it to the correct ViewModel type.
For example:
Inherits="System.Web.Mvc.ViewPage<ProductViewModel>"
Answers above cover it pretty well.
you might consider using:
http://www.jetbrains.com/resharper/
i have used it to convert my website to web application and it really helps.
I have an ObjectDataSource on a page that is producing the error "Object does not match target type" when its Insert method is invoked. From Googling this message, I believe this the message is deceptive and I'm actually getting a null reference error on the object that the ObjectDataSource is trying to invoke the method on, but I'm darned if I can figure out why.
<asp:ObjectDataSource ID="dsAddComment" runat="server"
DataObjectTypeName="BookCatalogue.InteractionDocuments.UserComment"
SelectMethod="GetNewComment" TypeName="BookCatalogue.AddCommentPresenter"
InsertMethod="AddComment" OnObjectCreating="dsAddComment_ObjectCreating" />
The Type that is being called upon Insert is an AddCommentPresenter. The method AddComment is not static. If I change this to static, I don't get the error and the method is found without problem. When it's not static, the error occurs. That's whyI htink that the underlying problem is that somehow I'm not getting a valid instance of my Presenter class when the AddComment method is invoked.
My AddCommentPresenter class doesn't have a parameterless constructor. This will cause an error normally. To get around it I'm overriding the ObjectCreating event in my page's code-behind and assigning an instance of the Presenter class.
protected void dsAddComment_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = presenter;
}
I can step through my ObjectCreating method and it is a valid, non-null Presenter instance that's being passed into the e.ObjectInstance property.
My AddComment method has the correct signature.
public void AddComment(UserComment newComment)
{
...
}
I've also checked the obvious things, like misspelling the type name on the aspx page, but all is correct there.
Anyone have any ideas? I must say that I find the ObjectDataSource class very difficult to work with....
A colleague found the cause of my problem. The AddCommentPresenter class in my web app was defined in the website's App_Code directory. For some reason, that was causing the error. Move it out of there, into the main directory of the website and the code worked. I can't find any mention in any ObjectDataSource documentation of this being a potential gotcha for the control, but there you go.
I've also been told that it should be possible to keep the class in the App_Code folder, but include the syntax ",__code" at the end of the TypeName. E.g.
TypeName="MyNamespace.MyType,__code"
but personally, I didn't get that working when I tried it. Another post to an ASP.Net forum suggested changing the TypeName to
TypeName="_MyNamespace.MyType"
That didn't work for me, either. I ended up just pulling the class out of the App_Code folder.
I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T> lives in a separate project called DataAccess. According to this post from the MS newsgroups (relevant part copied below):
Internally, the ObjectDataSource is calling Type.GetType(string) to get the
type, so we need to follow the guideline documented in Type.GetType on how
to get type using generics. You can refer to MSDN Library on Type.GetType:
http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx
From the document, you will learn that you need to use backtick (`) to
denotes the type name which is using generics.
Also, here we must specify the assembly name in the type name string.
So, for your question, the answer is to use type name like follows:
TypeName="TestObjectDataSourceAssembly.MyDataHandler`1[System.String],TestObjectDataSourceAssembly"
Okay, makes sense. When I try it, however, the page throws an exception:
<asp:ObjectDataSource ID="MyDataSource" TypeName="MyProject.Repository`1[MyProject.MessageCategory],DataAccess" />
[InvalidOperationException: The type specified in the TypeName property of ObjectDataSource 'MyDataSource' could not be found.]
The curious thing is that this only happens when I'm viewing the page. When I open the "Configure Data Source" dialog from the VS2008 designer, it properly shows me the methods on my generic Repository class. Passing the TypeName string to Type.GetType() while debugging also returns a valid type. So what gives?
Do something like this.
Type type = typeof(Repository<MessageCategory);
string assemblyQualifiedName = type.AssemblyQualifiedName;
get the value of assemblyQualifiedName and paste it into the TypeName field. Note that Type.GetType(string), the value passed in must be
The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.
So, it may work by passing in that string in your code, because that class is in the currently executing assembly (where you are calling it), where as the ObjectDataSource is not.
Most likely the type you are looking for is
MyProject.Repository`1[MyProject.MessageCategory, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null], DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null
I know this is an old post but I have recently had this problem myself. Another solution would be to replace the inheritance with object composition, e.g.
[DataObject]
public class DataAccessObject {
private Repository<MessageCategory> _repository;
// ctor omitted for clarity
// ...
[DataObjectMethod(DataObjectMethodType.Select)]
public MessageCategory Get(int key) {
return _repository.Get(key);
}
}
This way the ObjectDataSource doesn't know about the repository because its hidden within the class. I have a class library in my facade layer that is a perfectly reasonable place to put this code in the project I am working on.
In addition, if you are using Resharper and interfaces, its possible to get Resharper to do the refactoring using Resharpers "Implement using field" function.
Darren,
Many, many thanks for your post. I've been fighting with this all day. Strangely, in my case, I need to double the square brackets, e.g. for your piece of code:
MyProject.Repository`1[[MyProject.MessageCategory, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null]], DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null
Roger