Context is not a member of in asp.net usercontrol

2019-07-08 05:41发布

问题:

I just created new usercontrol in an asp.net webapplication project.

Right after creation, the source looks like this:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="SSR_Project.ascx.vb" Inherits="CS_SSR_RAIO.SSR_Project" %>

But as soon as i add some additional code to it, the error on the first line is immedeately:

Context is not a member of 'CS_SSR_RAIO.SSR_Project

I do have other usercontrols who appear to have the exact same declarations, but dont have this error.

Any help would be greatly appreciated!

回答1:

I got this error because it couldn't find the class of my new usercontrol's codebehind. I had this because I'd copied a usercontrol and it was in a Namespace which I'd not added to the Inherits attribute correctly.

Added the namespace prefix and it started working again :)



回答2:

I had this happen to me under the following condition:

With a web site (as opposed to a project) in VS2008:

1) I copied an existing / functioning usercontrol (as in CTRL + Click, Drag, Drop)
2) I modified the copy slightly (attached a listview's datasource to a different stored procedure)


I fixed it as follows:

1) Create a NEW user-control (add new item in VS)
2) Open the .ascx I had originally copied, and this time just copy all of the mark-up, and paste it into the new user-control's .ascx.
3) Same for the .ascx.vb file -- open the one to copy from, copy all of the code, and paste it into the new user-control's .ascs.vb file


If you just look at the old and new files, there's no explaining it -- they appear identical. The difference for me was, copying the file failed, whereas copying the contents of the file succeeded.

I suspect this may have something to do with partial classes, and the fact that you can't access all of the parts when working on a web site. I think when it comes to projects, you can access all of the parts -- and maybe if that were the case, I'd be able to see what was causing the problem.



回答3:

Ensure that the Control inherits from System.Web.UI.UserControl

The code behind (SSR_Project.ascx.vb) should have the following

public partial class SSR_Project : System.Web.UI.UserControl

Sorry that is C# syntax though .. but you get the picture :)

Edit: I think this is the Vb syntax ..

Public MustInherit Class SSR_Project Inherits System.Web.UI.UserControl



回答4:

Thanks for the help, zybroxz and Konstantin D.
In the end, i removed the usercontrol from the solution, added it again and pasted the same code into it, and it all worked well. I did noticed that the designer of the usercontrol was empty the first try, the second time it was correctly filled.

I wouldn't be surprised if something went wrong during creation of the usercontrol and this error seeped thrue into the pdp file and thus was unable to remove...



回答5:

You can access to your Context, but your class must inherit from UserControl

Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.control.context(v=vs.80).aspx



回答6:

Copying an existing control then pasting and editing may result in this problem. Check out the "web form designer generated code" block at the top of the .aspx.vb page. Every control must be listed: i.e. "Protected WithEvents lblX As System.Web.UI.WebControls.Label" add your pasted controls to the list and you're good to go



回答7:

For me, in VB.Net, the problem was this:

If you have this error: error BC30456: "AnyPublicMethodOrProperty" is not a member of 'UserControl'

Is because the user control is declared as generic UserControl and not the exactly inherited class, thus, in the designer parent page, (for example: default.aspx.designer.vb) the control property should be declared with the exactly class name, and not with the inherited UserControl class.

So... try to find this in the designer parent page:

'''<summary>
'''UcYourControlName control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ucYourControlName As Global.System.Web.UI.UserControl

And replace it with this:

'''<summary>
'''UcYourControlName control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ucYourControlName As Global.Wherever.Your.UserControl.Namespace.Is.Located.UcYourControlNameClass