Can't find asp control in codebehind of Master file - c#

This is driving me crazy, because I thought I had the runat=server stuff totally figured out.
I have an asp:Literal control in my markup (it's in a masterpage, but that doesn't matter, right?)
In MyMaster.Master:
<asp:Literal ID="myLiteral" runat="server" Text="Some Text"></asp:Literal>
Then, in the codebehind (MyMaster.Master.cs), I have:
myLiteral.Text = "Some different text";
No matter what I try, I get
The name 'myLiteral' does not exist in the current context
I've tried cleaning and rebuilding my whole solution, but it doesn't make any difference.
What am I doing wrong?
Edit: To clarify, I'm trying to change the text of the Literal control in the codebehind of the master page, not a content page. Specifically, I'm doing this in the Page_Load method of the master file.

Figured it out! I wish I knew why, but the Masterfile was missing it's corresponding .designer.cs file. I tried several things to get VS to regenerate the designer file, but I finally found the trick in one of the answers to this question.
The trick is to create an empty code file with the correct name (in my case MyMaster.Master.designer.cs. Then open the masterfile (MyMaster.Master) and make some small change to cause VS to resave the file. When that happens, it magically fills in the empty designer file!
So, this problem is solved. However, if anyone knows why a) my designer file disappeared in the first place, and b) if there's any better way of forcing VS to regenerate the designer files if they happen to disappear, that would fantastic.

that is some problem of VS, i have witness such issues in VS 2010 when the controls were not forming. Whenever such things happens i try the following things -
1. Cleaning and rebuilding solution.
2. Dragging and dropping the contacts from toolbox which is not creating.
3. Restarting computer.
This thing happens only sometimes. I would recommend you to use VS 2013 as it is mostly bug free(if not 100%).
Thanks

Related

How to compile Item Template Project for Form with substitute parameters

I am trying to make an ItemTemplate for a Form. I create a "c# Item Tamplate"-Project, make my Form, and create my Template by Building the project. That works fine.
Now I want to add Templateparameters but as soon as I do, the project wont compile any more. It throws many different exceptions because the code is no longer valid. How can I solve this problem?
Here is what I tried so far:
Replacing every occurrence of my Classname with $safeitemname$. This, as I already said, throws exception because now the code is invalid and the project can not be built.
Creating CustomParameters in my .vstemplate to change a valid class name to my desired parameter (Classname -> $safeitemname$). This does nothing, even though I set "Replaceparameters="True"".
Ignoring the compiler warnings and implementing the project in my .vsix project anyway. This does nothing either, because the .zip for my template still can not be.
Setting DependentUpon of my Classname.Designer.cs to my Classname.cs. This does nothing either.
Trying a simple ItemTemplate with one class that's divided in two partial classes that have one Property each. Same thing as with the Form. It can no longer be build.
As Reza Aghaei suggested in his comment, the problem was, that I didn't set the build action for the files in my project to 'None'.

New Designer1.cs created every time I try to add queries to my DataSet.xsd (VS2013)

I'm working on an application on VS2013 and have this bug right now which is every time I add an SQL query I have a new Designer.cs file created, which is called Designer1.cs. Then of course impossible to run the app cause there are conflicts between the 2 files.
Does anyone know why it's doing this and how to fix it? I can't work on my application anymore because of that.
I've read some posts about it, done the trick but still got some new Designer1.cs file being created after that.
Weird “Designer1.cs” files created
Dataset Designer.cs generate Designer1.cs after taking update from SVN in VS2013
I suspect something is wrong with my project settings but really don't know how to fix it. Is it possible to copy all the content of the application to a new project easily? I'm using VS2013 for school and don't know much about it, some help would be really appreciated, thank you!
I have had the same in three projects. Sometimes deleting the .sln-file helps, sometimes it is neccessary to copy all files to a new created project, excluding the sln-file.
I don't know why this bug comes up, I looked for help but nothing to find. I wrote to Microsofts develloper forum - no answer.
The description in your first link - the answer of zendar - is very good for the first time. The excluding is not nessecary, but deleting .designer1.cs after renaming in the explorer is reqired.

The name Repeater1 does not exist in the current content

Im using a masterpage on asp.net c# for the website. (And created an aspx page related to the master page, by hand, not automatically)
I put a repeater control to the page.
Then I couldn't reach to the control by name in codebehind.
The compiling error is : 'The name Repeater1 does not exist in the current content'
Can you please point out my mistake?
Sometimes the designers get out of sync with the aspx pages. When such a thing occurs, the code behind files become unable to access controls that you have added to your aspx pages and you'll get errors like the one you received.
You can get around this error by regenerating the designer file, as you said Project -> Convert to web application is one way to do it.
One last thing, not sure what version of VS you're using, or if this will help, but there was a hotfix released for VS2010 that is supposed to take care of some occurrences of this problem.
http://blogs.msdn.com/b/webdev/archive/2010/03/05/hotfix-for-issue-with-auto-generated-designer-files-not-adding-controls.aspx

InvalidCastException for Object of the same type - Custom Control Load

I have a very wired error, one of my custom controls seems that is create two compiled files, and when I try to load it dynamically with LoadControl() is just fail because can not cast the one to the other - even if they are exactly the same. I write the message to see that all is the same, is only change the compiled dll.
System.Web.HttpUnhandledException (0x80004005):
Exception of type 'System.Web.HttpUnhandledException' was thrown. --->
System.InvalidCastException:
[A]ASP.Modules_OneProduct_MedioumImage cannot be cast to
[B]ASP.Modules_OneProduct_MedioumImage.
Type A originates from 'App_Web_kg4bazz1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
in the context 'Default'
at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_Web_kg4bazz1.dll'.
Type B originates from 'App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
in the context 'Default'
at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b.dll'.
The code
This is the code as it is right now after I have follow exactly what is written on MSDN:
foreach (int OneProductID in TheProductIdArrays)
{
// here is the throw.
ASP.Modules_OneProduct_MedioumImage OneProduct =
(ASP.Modules_OneProduct_MedioumImage)LoadControl(#"~/mod/OneProduct_MediumImage.ascx");
// do some work with
//OneProduct
}
Previously I have Load the control without the ASP. but after this bug appears and looking for solution, I strictly follow what is on MSDN. The bug is still here no matter what I do.
I have also try both of this methods, each one alone, and together (again fail)
<%# Register src="~/mod/OneProduct_MediumImage.ascx" tagname="OneProduct_MediumImage" tagprefix="uc1" %>
<%# Reference Control="~/mod/OneProduct_MediumImage.ascx" %>
Config
My web.config, I have try with maxBatchSize 20, 100, 1000, also with optimizeCompilations true or false, but the bug is appears again.
<compilation debug="false" defaultLanguage="C#" batch="true"
maxBatchSize="800" batchTimeout="10800" optimizeCompilations="false"
targetFramework="4.0">
Now some details about
The error is random, in some compile appears, in some other not.
The project is a big one, the pages are live with a lot of people in every minute that ask to see something, but also appears when there is no one inside.
Is run on 64bit dot.net 4, Intergrated
Run as web garden but also tested and one pool alone (and get the same issue)
The session is off on the full project.
The pages are run from 2007 but this issue is appears the last month, unfortunately I can not find where and how is started, or what is trigger it because I late some days to see it.
Appears only one one custom control loads, the one that have heavy call.
I have change 4 times the code making small changes, or big changes and still there.
I have try with optimizeCompilations true and false and the same issue.
I have try also by stopping the web, delete all temporary files, reopening, and there was again.
I have try to place a mutex on global.asax when the application starts to lock only one compile at the time, but this fails also.
From the moment that works, then all is good, but if not works is not auto corrected.
The code that I load this custom control is exist and called in more than one places on the code, on different pages.
Other custom controls, with similar load did not have any problems.
ViewState is disabled for this custom control.
I have also try relocate some code, change the full function call with micro optimizes, no again fail.
Is work fine on development computer. I place batch="true" on web.config and the bug appears right away.
There are no other issues like that, like a bug that we can not fix no matter what. The system is run for days, the pool is NOT recycle at all, the memory is stable, and there is more free to use. The program is run for years now, but we change is almost every day with updates.
Under the same core code runs more than one sites (something like stackexchange) and all have the same random problem.
The AutoEventWireup is false
Its appears and on other custom control that I load the same way.
What I do now as workaround when this bug appears: I just force the project to recompile with a small change, and the error go away, until the next update.
I have a bug that try to solve the last tree weeks with out find the reason. I have try almost anything I can thing of, but all fails, and the bug appears again. So I post here maybe some can help me out and find a way out of this.
Last word: This bug is crazy, the custom control is the same, I do anything on it I only load it dynamically and boom, the compiler is have it two different times for some reason that only he knows - randomly.
Update 1
I been able to reproduce the bug on the developer machine. There I discover that the two dll modules that contains this custom control have a different.
The one was a bundle of 4 custom controls together. The other module was the custom control alone.
Workaround
After tree weeks trying to fix this bug I end up that this bug is appears when the compiler make batch compile of a directory, and bundle many different custom controls, in the same dll. So when I try to load it alone is throw this exception.
So I move the problematic custom control in a different directory alone and seems that I avoid it for now.
Update 2
Appears again, even after I move some files to a different directory. Is random and can not find a clear connection with what is triggers its.
Update 3
Because we have spot that the main issue here is the batch compile (batch="true") that compiles on the same dll many custom controls, one way to say to the compiler to NOT do that, is the maxBatchGeneratedFileSize parameter. I use it with a value of 100, and the issue appears again, now I have lower it to 40 and test it.
maxBatchGeneratedFileSize="40"
This can happen when you have batching turned on and have some form of circular references at the directory level.
Please see this answer to see exactly what I mean by 'circular references' in this context, as the meaning is quite subtle.
If you manage to break the cycle (e.g. by moving a user control elsewhere), you will not hit this issue.
Update 1
I would think that in theory, this can only be caused by a cycle, but sometimes they can be hard to detect.
I'll give you an alternative solution which I think will work and is very easy to try (even though it is a bit of a hack). In the user control(s) that is giving you problems, add the following attribute in the directive:
<%# Control Language="C#" [...] CompilerOptions="/define:dummy1" %>
If you see this with some other controls, you can add the same thing but with dummy2, dummy3, etc...
This will have the effect of not batching this one user control, since it has different compilation needs from the others. Technically, you can add any piece of C# command line as the CompilerOptions, but a dummy /define is the simplest and most harmless.
But unlike turning off batching globally, the perf impact will be minimal, since only a very small subset of pages will not be batched.
BTW, it goes without saying that what you're seeing is a bug in ASP.NET, and that bug has been there for probably 10+ years! Maybe at some point it should get addressed :)
In order to track the cause of the problem I believe that's important to know how your control was created. Please refer to this reading: Turning an .ascx User Control into a Redistributable Custom Control.
Step 1: Authoring the User Control
To author the user control, it is best to start with an empty app that
contains nothing other than the ascx. While the authoring of the user
control uses "standard" techniques, there are some restrictions that
you need to be aware of in order for it to be successfully turned into
a standalone custom control.
The main restriction is that the user control needs to be self-contained. That is, it cannot be dependent on app global things
like App_Code or global.asax. The reason for this is that since the
goal is to turn the UserControl into a standalone DLL, it would
break in other apps if it relied on code that is not part of that
DLL. One exception to this rule is that the UserControl can be
dependent on assemblies that live in the bin directory (or in the
GAC). You just have to make sure that the other assemblies are always
available when you use your custom control in other apps.
and
Step 3: Use the Publish Command to Precompile the Site
(...) Select "Use fixed naming and single page assemblies". This
will guarantee that your user control will be compiled into a single
assembly that will have a name based on the ascx file. If you don't
check this option, your user control could be compiled together with
other pages and user controls (if you had some), and the assembly
would receive a random name that would be more difficult to work with.
In my opinion it's very likely that you have the user control compiled and registered in GAC as a separate assembly and also included in your web application DLL.
Note: Maybe this should have been a comment, but I wanted to include the quotes from the forementioned link. I hope it's helpful.
After a lot of debug on a upgraded ASP.NET website, my last bug was this one on runtime.
I just checked the Build/Publish option "use fixed naming and single page assemblies" and it solved my case :)
Here some useful links: https://msdn.microsoft.com/en-us/library/hh475319(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/aa479044.aspx
http://forums.asp.net/t/960707.aspx
I've recently encountered similar problem when i was compiling a modified version of asp.net MVC 4 and importing the new DLL into the project.
Somehow i was referencing the old versions of the DLLs in the web.config (including the web.config in the views folder)
The error in my case was thrown because the two DLLs were different versions. 4.0.0 and 4.1.0.
Maybe you should look into that.
Maybe specify a version of the compiled files (i'm guessing DLLs)
I hope this helps you fix the problem.
other tips: I'm guessing you have some sort of version control system? if yes , revert all the changes back before this started and look carefully at the code and which models/controls change and how.
if you are not using VCS... there isn't much you can do to revert the changes. And you should start using a VCS.
I have noticed sometimes the designer creates a second CodeBehind designer file, eg you would have:
OneProduct_MediumImage.ascx
OneProduct_MediumImage.ascx.cs
OneProduct_MediumImage.ascx.designer.cs
OneProduct_MediumImage.ascx.designer1.cs
You wont notice if you dont have "Show All Files" option in the Solution Explorer set, but for a web project the compiler will compile all files in the folder, not just the ones included in the project.
Second, if your project is a "Website Project", there are no namespaces, which can lead to many weird errors. Look at this SO question: Namespace problem .net
Finally, I managed to solve seeming random UserControl errors by setting the ClassName attribute on the control file, eg:
<%# Control Language="cs"
AutoEventWireup="false"
CodeBehind="OneProduct_MediumImage.ascx.cs"
Inherits="ASP.Modules_OneProduct_MedioumImage"
ClassName="OneProduct_MediumImageControl" %>

Visual Studio auto opening collapsed properties

Here is my problem. I have a C# class file loaded in Visual Studio that I am working on. I made a class and created some properties inside of said class. I collapsed the properties like so:
I collapse the properties with the little +/- button on the side of each. I want to collapse each of them since I have a lot of other classes with properties and I don't want to see a lot of repetitive code (get, set).
Closing them works fine but after I work on some other classes in the same file the properties re-open so I can see the code:
This happens relatively often and is quite annoying. I have a lot of lines in the file and I sometimes have to work with a small screen. Is this a glitch/bug or something else? (I am using Visual Studio 2010 C# Express)
Thanks for your help.
There is no way to solve this so far, but this does the trick. Press CTRL-M and then O and it will auto collapse everything in the current file you are on.
An alternate tip to #DJKRAZE's excellent answer is Resharper. I use Resharper and it takes care of this automatically for me. For example if I write this code:
The 4 line property procedure causes a Expand/Collapse that is causing you grief.
Resharper makes it one line. When I delete the last curly brace (namespace) and enter it again, the code is automatically formatted (Edit > Advanced > Format Document doesn't do this):
You can use this:
#region some_name
//your variables or code whatever
#endregion
And then you can collapse the particular code, and the good thing it does not open like the way it happens in your case.
See if that worked.

Categories