Sharepoint question about UserControl on WebPart - asp.net

I added UserControl Webpart on the site and got this error:
error CS0117: 'ASP._60b6ad6d_6998_4413_8d26_f07e4e897ce8_1417418301' does not contain a definition for 'btnPressMe_Click'
It is very simple user control.

Going out on a limb I'd say you have a button in your user control that you double clicked in the visual designer, creating a default event handler for it, and at some point you removed the generated method stub from your code behind.

Or, going out on another limb, you have updated the ASCX with your button and replaced it in the 12 hive but did not build and replace the associated DLL in the GAC or Bin folder.

This might be a long shot, but maybe there's an error in the directives of the user control?
See the Directive Syntax at MSDN
Oh, and here is a link to the MSDN page of the Compiler Error CS0117.

Related

Type or namespace could not be found only on a publish

I have a web solution with Visual Studio 2010 and ASP.Net 4.0. In this solution there is a web site as well as a class library.
I added a new User Control to the site and was able to build everything. I was even able to publish with the new control. The issue arises when I try to reference the class name of that control in a different part of code. When I do this, I still am able to build / rebuild the entire solution. But when I try to publish, I get the error "type or namespace name could not be found."
When we publish, we always UNCHECK the box "Allow this precompiled site to be updatable" and we always CHECK the box "Use fixed naming and single page assemblies."
If I do a publish and UNCHECK the box "Use fixed naming and single page assemblies" it will publish just fine. But for some reason I can not get it to publish with that box CHECKED which is what I need to be able to do.
I have tried:
Cleaning the solution
Restarting Visual Studio
Restarting Computer
Clearing out .net temp files folder
Recreating the control
Putting everything in website into a namespace
Any help or insight would be greatly appreciated.
Thanks.
EDIT:
I figured it out. I was adding this new control dynamically. But I didn't realize it also needed registered on the page that I am using it on. After registering the control, everything worked as expected
When you want to use your web user control dynamically, as you use other .net controls like textbox, label etc. you need to follow these steps.
(1) Add a ClassName property in your control directive as below (in ascx file)
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CustomControl.ascx.cs"
Inherits="CustomControl" ClassName="CustomControl" %>
Note : Make sure the ClassName & Inherits both are same as class name in the CS file.
(2) Add a Reference directive on the page where you want to use your web user control dynamically
<%# Reference Control="~/usercontrol/CustomControl.ascx" %>
Note : Reference directive is different then Register directive.
(3) Now you can add your user control dynamically anywhere on your page like below
ASP.CustomControl cuCtrl = (ASP.CustomControl)LoadControl("~/usercontrol/CustomControl.ascx");
//add dynamically created custom control to an existing panel or any container control on your page
pnlDemo.Controls.Add(cuCtrl);
Note : You can always include the ASP namespace in the using ;)
After this, you should not face any problem publishing even when checking the Allow this precomplied site to be updatable option.
I figured it out. I was adding this new control dynamically. But I didn't realize it also needed registered on the page that I am using it on. After registering the control, everything worked as expected.
I tried that and still have the same problem when publishing, either with reference tag or register tag it doesn't publish. Only way to work by now is to uncheck the "updatable site"

ASP.NET UserControl not defined?

I've just inherited an app that utilizes usercontrols in a couple ways which I'm not too familiar with. The problem I have right now is that when I attempt to publish this code base, I get a few errors which boil down to where some referenced usercontrols are not defined. Here's an example of one line:
Private clientControl As New ASP.usercontrols_clientcontrol_ascx
This is a tab strip usercontrol which references other usercontrols to dynamically create the tabs. Now, on the surface I get what is going on here...but the compiler is not accepting this. This tab strip usercontrol is in the root of the project, and the other usercontrols are in a sub folder.
error BC30002: Type 'ASP.usercontrols_clientcontrol_ascx' is not defined.
I'm sure this is 101 stuff here, but the build works and the publish fails. Any direction would be appreciated.
Try registering your control at the page you are adding it to.
<%# Register src="usercontrols_clientcontrol_ascx" tagname="usercontrols_clientcontrol" tagprefix="uc1" %>
I don't know if this is the right solution, but had the exact same issue and was able to get it to publish by unchecking the
Allow precompiled site to be updatable
option when publishing. This was in Visual Studio 2008.
If you remove the x:name element in the original XAML does it work?

codebehind cannot reference the page controls

I have controls in ascx file but i can't see them in intellisense in .cs file.It was working nice before.
I can see the control names in designer.cs file.
I have deleted the Asp.net temp files in AppData folder but still not working.The other user control files in the app can reference coerrectly to it's page controls. What is the problem here ?
I use VS2008.
Look at the top line of the .ascx page and check out the value of Inherits= and make sure that is has the right namespace.class appropriate for your codebehind. For example if your namespace is ProjectNamespace and your control class is MyControl then it should be ProjectNamespace.MyControl. This can get out of synch if you renamed the ascx file, etc and cause this type of problem.
This happens to me every time I copy a user control from one project to another. The connection between the ascx and the code-behind breaks.
This solution is tedious but it gets around the problem:
Create a new User Control. Visual Studio will correctly connect the ascx file to ascx.vb file.
Copy the ascx and vb code from your original control into the new one.
Delete your original control.
You now have a working control, but it has a different name.
If it is important to retain the name of the original control, repeat the whole process again and copy the second control to a third one with the correct name.
Write to Microsoft and ask them to stop adding bells and whistles we don't need and fix the basic stuff!
I just figured this out for my situation: in the Page parameters of the .aspx file, the 'Codefile' parameter was pointing to the file name: 'LabEdit.aspx.cs'. It should have been pointing to the path: "~/WOPages/LabEdit.aspx.cs". I know I didn't move the codefile or the page file, so this is maybe a problem with VS2008
I have solved the problem, I have uninstalled and reinstalled VS2008 and it is solved.

Getting namespace name not found for ASP.net user control

So I'm having problems when I try to publish the website.
I'm in visual studio 2008 sp1.
I've got a bunch of user controls and on a few pages I'm using them programatically.
I've got a reference on the aspx page
<%# Reference Control="~/UserControls/Foo.ascx" %>
Then on the code behing I use
ASP.usercontrols_foo newFoo control = (ASP.usercontrols_foo)Page.LoadControl("~/UserControls/Foo.ascx");
If I navigate to the page it works fine, but when I goto publish the website I get a compile time error.
Argh, I'm bleeding development hours on this same issue. Does anyone have a solution to this ?
BTW: It builds if you uncheck "Allow this precompiled site to be updatable" (Right-click on the website, choose Property Pages, MSBuild Option)
But I need the site to be updatable.
I had this same problem - actually, in my case I could get it to compile and build my website, but it would blow up spectacularly when doing an automated build.
Try replacing your code as follows:
ASP.usercontrols_foo newFoo control = (ASP.usercontrols_foo)Page.LoadControl("~/UserControls/Foo.ascx");
with
USERCONTROLS_Foo newFoo control = (USERCONTROLS_Foo)Page.LoadControl("~/UserControls/Foo.ascx");
(Capitalization will be based on how you capitalized your directory name/control name - but in either case should highlight appropriately).
Specify a namespace for user control (see Dynamically Load a user control (ascx) in a asp.net website ).
I've found a solution for it. If i'll declare controls in user defined namespace, then I can use controls directly without using ASP or referencing it into aspx page.
It may have something to do with the specific type not being available. Can you change the control reference so the type and cast just use the base Control class:
Control control = Page.LoadControl("~/UserControls/Foo.ascx");
Yes, I can cast it to Control. But then I lose direct access to the methods on the usercontrol.
I know that I can access the methods by reflecting into the control, and I've successfully done that but that's far from ideal to access all the other user controls on the site.
Also, the follow-up error is that it cant find the usercontrols that on the markup
<%# Register src="CategoryRows.ascx" tagname="CategoryRows" tagprefix="abc" %>
<abc:CategoryRows ID="CategoryRows" runat="server" />
Note that I can run the site successfully both locally and on the server if I essentially XCopy the entire site (source code and all). But Publish fails.
Casting the user control may create many problem .my approach is to create a class (say control Class) put all the properties and method you need after casting and inherit this class from System.Web.UI.UserControl .Then in your user cotrol code file instead of System.Web.UI.UserControl user this control class .
now when ever you need casting cast with this class only . it will be light casting as well.
Please check:
Do you have any website.publishproj file?
If it exists, then delete and try again and publish code.

controls in code behind

i was working on a vb.net project and i couldn't find some controls in the code-behind file(*.vb). i was wondering is it because i was working in page_load function so controls are not loaded until after page_control event. however i was able to find them with findcontrol function of formview objective.
Controls inside of templates (such as in your FormView, or in a GridView) are not directly accessible in the code behind. You must use FindControl to get access to those controls.
If the controls are declared in the aspx then they're defined in partial class equivalent for your Page class.
This was introduced along with .Net v2.0 so that messing with the designer wouldn't screw up with your code behind file (which caused quite a few problems in some cases).
You can access the controls from your Page Load event. Sometimes IntelliSense plays tricks on you and doesn't suggest the control. Just type it in. It will work. You can close the aspx page and open it again. Sometimes that fixes it. Or just restart Visual Studio if you're annoyed by it.
However, there are a few considerations if you are interested in accessing control data at certain times during the life cycle of the page.
Server controls have their own life cycle that is similar to the Page life cycle, but the order in which the event is triggered for the controls is as follows:
Init and Unload event for a control occurs before the event is raised for the container (bottom-up).
Load event for a control occurs after the event is raised for the container (top-down).
You can find a more detailed explanation of the Page life cycle events on MSDN.
It's hard to tell what exactly the problem is; it would help if you could post some code here.
I do have two guesses/suggestions:
If you have the problem that brentkeller is describing, what usually fixes this completely for me is deleting the aspx.designer.cs file, then right-clicking on the .aspx file and select "Convert to Web Application". This re-creates the designer file.
The control is inside a template like Jason Berkan suggested. If it's in a LoginView, for example, you would use .FindControl("controlId") on the LoginView.
The controls would be part of a partial class in the same solution. Just find all references for your class name.
I occasionally have trouble with adding a control to a page and the Intellisense not recognizing the control. The compiler also seems to not recognize the control and prevents the project from being compiled. It can be very frustrating and I really haven't figured out why.
Sometimes it helps to close the aspx page and its code file, sometimes closing Visual Studio and re-opening it works. Sometimes none of it works and I just try another way to get things done.
I don't know if this is what you're experiencing, but if so, it can definitely make you scratch your head and wonder what is going on.

Resources