'txtName' is not declared. It may be inaccessible due to its protection level
After putting a textbox on my page when I compile it I get the above error:
'txtName' is not declared. It may be inaccessible due to its protection level.
This is happening when I try to read the value of the textbox in the codebehind page.
I'm not sure what's causing this...any ideas?
In the aspx file I have:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="signup.aspx.vb" Inherits="signup" %>
In the codebehind aspx.vb file I have:
Imports System.Data.SqlClient
Partial Class signup
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sen开发者_如何学编程der As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Ok, I have this resolved now. It was becuase I had renamed a copy of the .aspx file and which still using the newer versions code behind file.
The code behind file was looking for txtName.text but on the older version of the .aspx file txtName didnt exist!
I have excluded the older version of the page from the project and it appears to run ok now.
Thanks everyone for you help.
Is the textbox directly on the page? Also what type of project is that? Is it a web application or web site opened from file system? If it's a Web Application, the designer may have failed to update YourPage.aspx.designer.cs file. Check that file to make sure you have a definition for txtName something like:
protected global::System.Web.UI.WebControls.TextBox txtName;
If its not there, delete .design.cs file, right click on the page and choose "Convert To Web Application. This will recreate .design.cs file correctly.
May be this is missing in the textbox declaration:
runat="server"
I had a different cause for the exact same error and it is similar to what the OP found but not quite. My page.aspx.designer.vb
got out of sync and had an error. One of my controls was being defined twice (I think because I copy/pasted an existing control without editing the ID before pasting). Once I deleted the 2nd declaration I could build and the errors were gone. Interestingly, VS 2010 SP1
didn't list THAT error along with the others resulting from the lack of that page building. Hope this helps someone else.
The way the classes are ordered is that your page's generated class inherits from your codebehind class - they're not partials, and it's not the other way around. This means that your codebehind class has no knowledge of the controls you've placed on the page. If you want access to a property that you declare in the page itself you need to declare it in the codebehind class first:
protected TextBox txtName;
You can then access this member in the codebehind file. This will get wired up correctly to match your control in the page.
I have had that problem before. Not sure what caused it. But by deleting the offending textbox and adding it again it fixed it.
I've gotten this error when the class in the code behind doesn't specify Public
. Try this:
Partial Public Class signup <-------------Make sure the Public keyword is there
Inherits System.Web.UI.Page
...
End Class
This is what happened at my case.
-I have declared the variable at behind code
Protected Friend sMessage As String = ""
-Then the aspx : <%=sMessage%>
this case appears (visual studio 2015 - asp webform project)
Then I try to :
-clean solution
-Rebuild
Then the problem is gone
If the clean solution and rebuild does not work
try to clean the obj directory
If the variable is asp component
make sure runat="server" at the aspx file
I got this issue, by removing runat="server" attribute from a div, even though I was not accessing the div on server side. The problem lied in the fact that on client side I was trying access the ClientID of the div like this $('#<%=divMultipleDaysOptions.ClientID%>').
I had the same problem when I changed my asp.net website from 3.5 to 4.0.
To solve the problem I recreated the entire website in 4.0 page by page, making sure all pages have the same names.
I copied page contents for each page leaving out the top line that starts with <%@
from the 3.5 website to the 4.0 website.
I copied all the code in the page class in the code behind for for each page from the 3.5 to the 4.0 website.
I got similar error but scenario was different. Scenario: I have added existing page to my solution in a folder so i had to change the values for some properties (Inherits) in Page tag of html file. When i changed the folder structure in Inherits property i made a mistake and that caused this error. When the folder structure is different the code behind file file will not be able to access the control in .aspx file. So friends, when you get this kind of error please have a look at Inherits property value (i.e, folder structure is correct).
Another possible scenario is if the class name in the Page.aspx.vb file differs from the class name in the Page.aspx.designer.vb file. These need to be identical.
I had a similar problem after adding controls to a web sete originally written in VS 2008 .Net 3.5 to VS 2010 .Net 4 After adding controls, I got the same error. When I looked at the .designer.vb file for the page, the added controls were not there. The problem was resolved by just adding the controls to the bottom of the file like: Protected WithEvents lblApprove As Global.System.Web.UI.WebControls.Label
精彩评论