Error: Document (get by id) is not declared - asp.net

Someone just quit at my job and I inherited the web project he was working on.
I have little experience with asp/vb and I might be missing something obvious here, but there is my problem:
I am experiencing this strange error where I'm trying to access the elements of my aspx page with Document.getElementByID() in but I only get the error " 'Document' is not declared. It may be inaccessible due to its protection level".
I get this error on all the pages I try to access Document to find the elements. There is no previous reference to "Document" in the code, so it is possible the problem has been there since the beginning but never got noticed.
I tried calling Document in different function/scope and the error persist. I also verified that all the pages inherit from System.Web.UI.Page. I have no other issue with the project, the pages render correctly and I can access the elements generated by the "Code Gen File".
I was wondering if there could be some references missing to the project or if there could be anything that I haven't checked that would explain this.

document.getElementById() is a client side JavaScript function.
Your aspx page is a server side web form.
You can't access client side functions in your code behind like that.
To reference server side controls (like those defined in you aspx page) you can simple use their 'id' property in the aspx

Related

ASPX Page 'Could not load type' Error

I have an .aspx page, we’ll call it Data.aspx, and it is going to use an ajax request to retrieve a response from a function called GenerateDocument in another .aspx page in the project, we’ll call this Document.aspx. It submits its ajax request, but the request fails, and I’m trying to decipher if it has to do with my page directives on Document.aspx. The entirety of the content on Document.aspx is this (it is not meant to generate a page, only generate documents based on data passed to it):
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Document.aspx.vb" Inherits="NameSpace.Document" %>
In Document.aspx.vb, we have:
Public Class Document
Inherits Generic.WebPage
<System.Web.Services.WebMethod()> _
Public Shared Function GenerateDocument
‘Process data and return as document
End Function
End Class
(There's more than just this; there's also a page load function, etc, but this is the relevant function I'm trying to hit.)
With this configuration, the error we get is complaining about ‘Could not load type NameSpace.Document’
I didn't think I should have to use a namespace that the class does not seem to be contained within, so I tried just saying Inherits="Document", but with the same error loading type message coming back.
I am building the project after I make fixes and getting no build errors, but if I try and change the CodeBehind to a CodeFile attribute, I get runtime compilation errors instead of type errors (I am tracking this through Fiddler).
This seems like it's probably an obvious issue where the function is just not being hit, but I can't seem to parse it. Any help? Thanks.
I figured out the issue. Thanks to all those who responded!
The problem was that while the files were all in the proper directory, one was not added to the project within the solution, and despite the references being in order (Inherits I left pointing to a namespace the class inherited from, plus the class), the program was not finding them at run time. This became apparent when I tried using CodeFile and got compilation errors and realized that it simply did not know what class to point to.
TL;DR - I added the .aspx file to the project and the whole thing worked fine.

Problems with ScriptManager, ASP.NET Web Forms (4.5), Visual Studio 2013 Prof., AJAX

I have the problem that I cannot use the ScriptManager in my ASP.NET Web Forms (4.5) anymore. It always throws the following critical JavaScript error messages (SCRIPT1002 is telling a syntax error) in Internet Explorer 11:
I tried to solve the problem with https://pavanarya.wordpress.com/2011/page/11/ and found out that WebForms.js shall cause the problem regarding to the decrypted WebResource.axd URL of the first error message (see above).
Furthermore, I found out that the error vanishes if I remove the line
from my Layout.Master file. All error messages vanish, but AJAX elements like TreeView (I also use UpdatePanel and Calendar) do not work anymore then, too.
I also tried the Script Resource Viewer from http://www.dotneti18n.com/Downloads.aspx via
http://www.guysmithferrier.com/post/2007/07/Script-Resource-Viewer.aspx. But the funny thing is that even the Script Resource Viewer implements the line
which exactly causes the same error as before, and so it is not a real help for me.
It must be some global problem in the whole project since it does not accepts any ScriptManager (no matter in which ASPX file) anymore. Does someone know that problem?
Many thanks in advance!!!
Remark: The Firefox Browser does not throw an error message, but it does not show the TreeView correctly, too. All images (ImageSet) of the TreeView are missing and the Web Browser displays text instead of the images. I also use a Chart (System.Web.DataVisualization) in the Web project, it was the last item which was added there. I use C#.
I am unable to put this as comment. So I will put it as Answer:
Use ToolScriptManager instead of ScriptManager.

variable is not declared it may be inaccessible due to its protection level

My VB skills are not the best, and this problem has had me stumped for a few days.
In the list of controls shown in Visual Studio that are not defined in the code behind, I can "mouseover" them and the tooltip text pops right up.
Similar questions:
This one had no solution - 'var_name'is not declared. It may be inaccessible due to its protection level.' in debug mode
This one said the solution was in the web.config, but I don't understand where/how - BC30451: 'MailValidation' is not declared. It may be inaccessible due to its protection level
This one was a misspelled word - vb.net error: inaccessible due to its protection level
Update - Here is another problem that might be a clue to what my problems are:
In my Tools.vb module, I have a class to access LDAP.
The namespace for the tools class is given in the login.aspx.vb code, yet login code does not recognize the tools class.
Pay close attention to the first part of the error: "variable is not declared"
Ignore the second part: "it may be inaccessible due to its protection level". It's a red herring.
Some questions... (the answers might be in that image you posted, but I can't seem to make it larger and my eyes don't read that small of print... Any chance you can post the code in a way these older eyes can read it? Makes it hard to know the total picture. In particular I am suspicious of your Page directives.)
We know that 1stReasonTypes is a listbox, but for some reason it seems like we don't know WHICH listbox. This is why I want to see your page directives.
But also, how are you calling the private method FormRefresh()? It's not an event handler, which makes me wonder if you are trying to reference a listbox in a form that is not handled properly in this code behind.
You may need to find the control 1stReasonTypes. Try maybe putting your listbox inside something like
<div id="MyFormDiv" runat="server">.....</div>
then in FormRefresh(), do a...
Dim 1stReasonTypesNew As listbox = MyFormDiv.FindControl("1stReasonTypes")
Or use an existing control, object, or page instead of a div. More info on FindControl:
http://msdn.microsoft.com/en-us/library/486wc64h(v=vs.110).aspx
But no matter how you slice it, there is something funky going here such that 1stReasonTypes doesn't know which exact listbox it's supposed to be.
I have suffered a similar problem, with a Sub not accessible in runtime, but absolutely legal in editor. It was solved by changing destination Framework from 4.5.1 to 4.5. It seems that my IIS only had 4.5 version.
:)
If I remember correctly, this is the default property for controls.
Can you try by going into Design-View for the admin_reasons that contains the specified Control, then changing the control's Modifiers property to Public or Internal.
I had a similar issue to this. I solved it by making all the projects within my solution target the same .NET Framework 4 Client Profile and then rebuilding the entire solution.
I got this error briefly after renaming the App_Code folder. Actually, I accidentally dragged the whole folder to the App_data folder. VS 2015 didn't complain it was difficult to spot what had gone wrong.
I have found that you have to comment out the namespace wrapping the the class at time when moving between version of Visual Studio:
'Namespace FormsAuth
'End Namespace
and at other times, I have to uncomment the namespace.
This happened to me several times when other developers edited the same solution using a different version of VS and/or I moved (copied) the solution to another location
This error occurred for me when I mistakenly added a comment following a line continuation character in VB.Net. I removed the comment and the problem went away.

'Sys' is undefined -- rendered markup is missing a reference to ScriptResource.axd

Yes, the famous "'Sys' is undefined" Microsoft JS issue.
I've already done about 4 hours of digging and trying every suggestion I can find about it, so before you immediately call this a duplicate, here me out please.
Ultimately, this question is exactly the same as this one, but the accepted answer isn't relevant to my situation, and the OP is no longer an active member.
Background
There are about a hundred pages in this application. Each of them ultimately inherits from the same base class. This base class overrides the Init method and dynamically adds a ScriptManager to it as the first Form control.
On one single page out of them all, I encounter the issue described in the post I linked. I mentioned that the accepted answer was relevant. Here's why:
I'm not making any Sys. calls
My page doesn't have any AJAX-enabled controls on it
My page doesn't have any JavaScript on it
My web.config is accurate, it includes the proper handler entries
The issue is reproducible on both IIS 6.0 and IIS 7+
If I explicitly add a ScriptManager to the page via <asp:ScriptManager />, the ScriptResource.axd include still doesn't render to the output page
I've tried clearing browser history, Temporary ASP.NET Files, rebooting, etc. with no change in behavior
An older version of the application in our UAT environment functions correctly; the base page code nor the web.config file have changed since then
I'm completely stumped. It's an ASP.NET 3.5 web site project running on Win Server 2003 with IIS 6.0 (both Prod and UAT). My developer environment is Win7 with IIS 7.5. Same behavior in both environments.
Question
Does anybody have any ideas? I'm starting to think it's a bug in the ASP.NET 3.5.1 framework...
I've just had a similar issue that I seem to have just resolved.
IF you are overriding any other page lifecyle events on your page other than PageLoad make sure to trigger the base version of the event. E.g. I was using OnPreRenderComplete
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e); //ADDING THIS LINE FIXED THE PROBLEM
//Add THEAD etc to Gridview
if (gvQueue.Rows.Count > 0)
{
gvQueue.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
Since you have tried everything else, and because it is reproducible, I can think of 3 possible causes that you can check:
1) The element not being defined correctly.
Make sure that the head element has runat="server" specified. We always provide an id as well, although I don't think that is strictly required:
<head id="HEAD1" runat="server">
2) The code that is causing the exception is being executed prior to the inclusion of the ScriptResource.axd.
To verify whether this is the case or not, I look at what has loaded in the page so far when the exception occurs. If I don't see the resource that is being reported as missing, I know I have an ordering problem.
I have seen this caused by two hooks of Page.Init (or other methods) in the inheritance tree and when this occurs, you cannot guarantee an order of execution.
3) An invalid page structure.
I have seen numerous cases where a misplaced, easily overlooked character, such as a single quote, double quote or < silently wrecks the page or javascript structure.
To verify that this is not the case, I first validate the page in design mode (Edit, Advanced, Validate Document) and correct any errors.
If there are no errors in design mode, I validate the rendered page by copying the source of the rendered page into an empty page within the project and then validating. This process has caught more than one subtle issue in the page structure.
4) If none of the above solve the problem, there could be an issue with the framework. If there is, it could possibly be caused by element names or order in your page. You can try removing or reordering items in your page until the problem goes away.
Hope this helps.

How can I check for code blocks in my controls collection in ASP.net?

I am working on a method in my base page that throws the "The Controls collection cannot be modified because the control contains code blocks" message (and it's true, some of my pages have code blocks in the control I am trying to modify).
Is there a way for me to programmatically determine if this is the case myself? I tried iterating through the controls collection, but it didn't seem to state anything that I could use.
As background: the method is adding an error message control whenever certain exceptions are thrown. What I am doing as a workaround is to add the error message to the parent of the previously targeted control, because the way that these pages are generally written. I'd like to keep the natural layout of the error message if possible, so I want to check for the trigger that indicates codeblocks are in play, and only then do my workaround.

Resources