'txtName' is not declared. It may be inaccessible due to its protection level

后端 未结 13 1581
暖寄归人
暖寄归人 2020-12-15 19:22

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.

相关标签:
13条回答
  • 2020-12-15 19:59

    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.

    0 讨论(0)
  • 2020-12-15 20:00

    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%>').

    0 讨论(0)
  • 2020-12-15 20:01

    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.

    0 讨论(0)
  • 2020-12-15 20:01

    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).

    0 讨论(0)
  • 2020-12-15 20:02

    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.

    0 讨论(0)
  • 2020-12-15 20:11

    I have had that problem before. Not sure what caused it. But by deleting the offending textbox and adding it again it fixed it.

    0 讨论(0)
提交回复
热议问题